PCA principal component analysis Matlab code implementation

Principal component analysis is a data dimensionality reduction method.

When is dimensionality reduction required?

When the samples in a sample set need to be sorted, there may be multiple dimensions to evaluate these samples, but too many dimensions will cause the same, so we hope to use fewer dimensions to evaluate, and at the same time minimize the accuracy of evaluation degree loss.

Principal component analysis, in simple terms, is to directly select several dimensions that contribute more to the evaluation results, or directly remove several dimensions that contribute less to the evaluation results; ( formula reference )

PCA principal component analysis Matlab implementation

Data requirements:

mA matrix of n (excluding the header), the sample set size is n, and the number of attributes is m; or there are n targets to be evaluated, and the number of evaluation factors is m.

%读入数据
x=xlsread('data');

%求算标准差
sd=std(x);

%对原始数据标准化
n=size(x,1);    %读出x第一维的长度
jj=ones(n,1);
jj=jj*sd;
x=zscore(x);

%对x数据进行主成分分析
[t,score,r]=princomp(x);

%%以下为绘图示例2例
%%例1
x=score(:,1:2);%取前两个主成分(的得分)
idx=kmeans(x,5);%用k-means法聚类(分为5类)
%绘图
str=num2str([1:n]');
figure(1),clf
plot(x(:,1),x(:,2),'o')
text(x(:,1),x(:,2)+.5,str,'fontsize',12)
hold on
for i=1:n
    if idx(i)==1
        plot(x(i,1),x(i,2),'o','markerfacecolor','b')
    elseif idx(i)==2
        plot(x(i,1),x(i,2),'o','markerfacecolor','g')
    elseif idx(i)==3
        plot(x(i,1),x(i,2),'o','markerfacecolor','c')
    elseif idx(i)==4
        plot(x(i,1),x(i,2),'o','markerfacecolor','r')
    else
        plot(x(i,1),x(i,2),'o','markerfacecolor','m')
    end
end

%%例2
x=score(:,1:3);%取前三个主成分(的得分)
x(:,3)=x(:,3)-min(x(:,3));
idx=kmeans(x,4);
%绘图(3D)
str=num2str([1:n]');
figure(1),clf
plot3(x(:,1),x(:,2),x(:,3),'o')
stem3(x(:,1),x(:,2),x(:,3))
text(x(:,1),x(:,2),x(:,3)+.5,str,'fontsize',12)
hold on
for i=1:n
    if idx(i)==1
        plot3(x(i,1),x(i,2),x(i,3),'o','markerfacecolor','b')
    elseif idx(i)==2
        plot3(x(i,1),x(i,2),x(i,3),'o','markerfacecolor','g')
    elseif idx(i)==3
        plot3(x(i,1),x(i,2),x(i,3),'o','markerfacecolor','c')    
    else
        plot3(x(i,1),x(i,2),x(i,3),'o','markerfacecolor','m')
    end
end
xlabel('PC1'),ylabel('PC2'),zlabel('PC3')  
This article is reproduced from: Click to open the link

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325815783&siteId=291194637