draw first principal component line 1 in the raw data
7 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
Josh
2024 年 8 月 16 日
編集済み: Josh
2024 年 8 月 16 日
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:
Principal component analysis : https://in.mathworks.com/help/stats/principal-component-analysis-pca.html
Eigenvalues and eigenvectors : https://in.mathworks.com/help/matlab/ref/eig.html
plot function : https://in.mathworks.com/help/matlab/ref/plot.html
Hope it helps!
参考
カテゴリ
Help Center および File Exchange で Delaunay Triangulation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!