# Projecting data points onto eigenvector space

조회 수: 55(최근 30일)
Hello,
I am trying to code a principal component analysis (PCA) on a dataset (8 samples , 2 features) and I can not plot the datapoints' projections on the eigenvector which provide the largest variace (eigenvector of the 1st principal component). The code is as following:
x=[1 1 2 0 5 4 5 3; 3 2 3 3 4 5 5 4]';
X=mean(x);
m=mean(x')';
x_m=x-X;
D=cov(x_m)
[eigenVector,lamda]=eig(D);
lamdasort=sort(lamda);
w2=eigenVector(:,2)'.*x;
robustness=lamda(2,2)/(lamda(1,1)+lamda(2,2))
figure(1)
hold on
scatter(x(:,1),x(:,2),'o')
scatter(x(:,1),x(:,2),'.k')
plot(X(1,1),X(1,2),'.g')
xlabel('x1')
ylabel('x2')
xlim([-2 6])
ylim([-2 6])
figure(2)
hold on
scatter(x(:,1),x(:,2),'o')
scatter(w2(:,1),w2(:,2),'.k')
So I would like w2 to be the projections of the data set (hence eigenVector(:,2)*x) to the eigenvector of the highest-value eigenvalue. I think smth is wrong with this approach, I get somthing like inverse of the dataset (figure (2)). I multiply the k=1 dimension (eigenvector) with the dataset (w2=eigenVector(:,2)'.*x;).
Thank you
Edit: This is the result that I cannot code  This is what i get when multiplying the eigenvector with the dataset.

댓글을 달려면 로그인하십시오.

### 답변(1개)

Christine Tobler 28 Jan 2021
The lambda here is a diagonal matrix, so SORT will sort each of its columns, not the eigenvalues on the diagonal among themselves. Also, after sorting the eigenvalues, make sure that you also permute the eigenvectors in the same way:
[eigenVector,lamda]=eig(D, 'vector'); % returns eigenvalues as a column vector lambda
[lamdasort,ind]=sort(lamda);
eigenVector = eigenVector(:, ind);
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Konstantinos Bampilis 28 Jan 2021
I used the largest eigenvalue, and the corresponding eigenvector.

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!