Kmeans的二类的画图代码clear all;
clc;
D= load('Pima-training-set-non.txt');
predict_label=kmeans(D,2,'dist','sqEuclidean','rep',4);
%predict_label
[S1,S2,S3,S4,S5,S6,S7,S8,classity]=textread('Pima-training-set.txt','%f %f %f %f %f %f %f %f %d'); %Pima-training-set.txt
tranin_label=classity;
length_tranin_label=length(tranin_label);
fprintf('length_tranin_label is: %d \n',length_tranin_label);
fprintf('Accuracy of prediction is: %d \n',length(find(predict_label==tranin_label))/length_tranin_label*100);
三类画图代码:clear all;
clc;
X = load('Pima-training-set-non.txt');
opts = statset('Display','final');
[idx,ctrs] = kmeans(X,2,...
'Distance','city',...
'Replicates',7,...
'Options',opts);
plot(X(idx==1,1),X(idx==1,2),'r+','MarkerSize',6)
hold on
plot(X(idx==2,1),X(idx==2,2),'b.','MarkerSize',6)
plot(ctrs(:,1),ctrs(:,2),'kx',...
'MarkerSize',12,'LineWidth',2)
plot(ctrs(:,1),ctrs(:,2),'ko',...
'MarkerSize',12,'LineWidth',2)
legend('Cluster 1','Cluster 2','Centroids',...
'Location','NW',4)
clear all;
clc;
X = load('totalData.txt');
opts = statset('Display','final');
[idx,ctrs] = kmeans(X,3,...
'Distance','city',...
'Replicates',7,...
'Options',opts);
plot(X(idx==1,1),X(idx==1,2),'r+','MarkerSize',6)
hold on
plot(X(idx==2,1),X(idx==2,2),'b.','MarkerSize',6)
hold on
plot(X(idx==3,1),X(idx==3,2),'gp','MarkerSize',6)
%
plot(ctrs(:,1),ctrs(:,2),'kx',...
'MarkerSize',12,'LineWidth',2)
plot(ctrs(:,1),ctrs(:,2),'ko',...
'MarkerSize',12,'LineWidth',2)
plot(ctrs(:,1),ctrs(:,2),'bo',...
'MarkerSize',12,'LineWidth',2)
legend('Cluster 1','Cluster 2','Cluster 3','Centroids',...
'Location','NW',4)