Configuring biplot in Matlab to distinguish in scatter

心不动则不痛 提交于 2019-12-03 16:22:38

The principal component scores (red points) in a biplot are not the ones returned by the pca function. As the help states,

biplot scales the scores so that they fit on the plot: It divides each score by the maximum absolute value of all scores, and multiplies by the maximum coefficient length of coefs. Then biplot changes the sign of score coordinates according to the sign convention for the coefs.

You therefore can't easily use the (X,Y) information to find out which point belong to a category.

Here is a workaround using the ObsLabels option of biplot. ObsLabels assigns some user-defined data to each observation: for each point, we will assign the index corresponding to a status variable (a simple incrementing value). With this, you can easily modify the red points of a biplot - here marker set to square and red/green color.

The following figure

is produced by this code

%some data
load carsmall
x = [Acceleration Displacement Horsepower MPG Weight]; x = x(all(~isnan(x),2),:);
[coefs,score] = pca(zscore(x));

%the status vector (here zero or one)
class_pt = round(rand(size(score,1),1));

vbls = {'Accel','Disp','HP','MPG','Wgt'};

figure('Color', 'w');
hbi = biplot(coefs(:,1:2),'scores',score(:,1:2),'varlabels',vbls,...
    'ObsLabels',num2str((1:size(score,1))'));

for ii = 1:length(hbi)
    userdata = get(hbi(ii), 'UserData');
    if ~isempty(userdata)
        if class_pt(userdata) == 0
            set(hbi(ii), 'Color', 'g', 'Marker', 's');

        elseif class_pt(userdata) == 1
            set(hbi(ii), 'Color', 'r', 'Marker', 's');
        end

    end
end
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!