draw first principal component line 1 in the raw data

7 visualizaciones (últimos 30 días)
N/A
N/A el 16 de Ag. de 2024
Comentada: N/A el 16 de Ag. de 2024
I have the data
WireLength 1 4 5 6 8 10 12 14 13 18 19 21
DieHeight 125 110 287 200 350 280 400 371 480 420 540 518
where Wirelength is the x variable and DieHeight is the y variable
I want to draw first and second principal component line in the raw data also draw the orthogonal lines from the point to the line.

Respuestas (1)

Josh
Josh el 16 de Ag. de 2024
Editada: Josh el 16 de Ag. de 2024
Hi Nafisa,
Here's the MATLAB code for getting first and second principal component line and also draw the orthogonal lines fom point to the line.
% Data
X = [1, 4, 5, 6, 8, 10, 12, 14, 13, 18, 19, 21]'; % WireLength
Y = [125, 110, 287, 200, 350, 280, 400, 371, 480, 420, 540, 518]'; % DieHeight
data = [X, Y];
mean_data = mean(data);
centered_data = data - mean_data;
% Covariance matrix
cov_matrix = cov(centered_data);
% Eigendecomposition
[eigvecs, eigvals] = eig(cov_matrix);
% Getting the principal components
pc1 = eigvecs(:,2); % Principal Component 1 (largest eigenvalue)
pc2 = eigvecs(:,1); % Principal Component 2 (second largest eigenvalue)
proj_data_pc1 = centered_data * pc1;
proj_data_pc2 = centered_data * pc2;
% Plots
figure;
scatter(X, Y, 'b', 'DisplayName', 'Original Data');
hold on;
% Plot Principal Component 1
line_x = linspace(min(X), max(X), 100);
line_y_pc1 = mean_data(2) + (line_x - mean_data(1)) * (pc1(2) / pc1(1));
plot(line_x, line_y_pc1, 'r', 'LineWidth', 2, 'DisplayName', 'Principal Component 1');
% Plot Principal Component 2
line_y_pc2 = mean_data(2) + (line_x - mean_data(1)) * (pc2(2) / pc2(1));
plot(line_x, line_y_pc2, 'g', 'LineWidth', 2, 'DisplayName', 'Principal Component 2');
% Plot orthogonal projections
for i = 1:length(X)
proj_pc1 = mean_data + proj_data_pc1(i) * pc1';
proj_pc2 = mean_data + proj_data_pc2(i) * pc2';
plot([X(i), proj_pc1(1)], [Y(i), proj_pc1(2)], 'k--');
end
xlabel('WireLength');
ylabel('DieHeight');
title('PCA of WireLength and DieHeight');
legend('show');
grid on;
hold off;
To read more about the principal component analysis, you can refer to the documentation here:
Hope it helps!

Categorías

Más información sobre Delaunay Triangulation en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by