Kmeans的matlab的实现

计算二类精度的Kmeans 的matlab代码

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);


Kmeans的二类的画图代码

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)


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值