MATLAB-K均值聚类实现瓶子分类 模式识别

一、实验三-K均值聚类实现瓶子分类 

1、用29组训练样本训练生成聚类中心,然后对30组测试样本进行分类;

2、将59组数据的类别信息都去掉,直接进行聚类(k=4),分析聚类结果;

3、比较两种方法的异同。

二、算法思路

三、代码

要求一代码

p1=[864.45	1647.31	2665.9;
877.88	2031.66	3071.18;
1418.79	1775.89	2772.9;
1449.58	1641.58	3045.12];

p3=[1739.94	1675.15	2395.96;
1756.77	1652	1514.98;
1803.58	1583.12	2163.05;
1571.17	1731.04	1735.33;
1845.59	1918.81	2226.49;
1692.62	1867.5	2108.97;
1680.67	1575.78	1725.1;
1651.52	1713.28	1570.38;];

p2=[
2352.12	2557.04	1411.53;
2297.28	3340.14	535.62;
2092.62	3177.21	584.32;
2205.36	3243.74	1202.69;
2949.16	3244.44	662.42;
2802.88	3017.11	1984.98;
2063.54	3199.76	1257.21];

p4=[373.3	3087.05	2429.47;
222.85	3059.54	2002.33;
401.3	3259.94	2150.98;
363.34	3477.95	2462.86;
104.8	3389.83	2421.83;
104.8	3389.83	2196.22;
172.78	3084.49	2328.65;
341.59	3076.62	2438.63;
291.02	3095.68	2088.95;
237.63	3077.78	2251.96];

k=mean(p1);
k22=mean(p2);
k3=mean(p3);
k4=mean(p4);
data=[
1702.8,1639.79,2068.74;
1877.93,1860.96,1975.3;
867.81,2334.68,2535.1;
1831.49,1713.11,1604.68;
460.69,3274.77,2172.99;
2374.98	,3346.98,	975.31;
2271.89,	3482.97	,946.7;
1783.64,	1597.99	,2261.31;
198.83,	3250.45	,2445.08;
1494.63	,2072.59	,2550.51;
1597.03,	1921.52	,2126.76;
1598.93,	1921.08	,1623.33;
1243.13,	1814.07	,3441.07;
2336.31,	2640.26,	1599.63;
354	,3300.12,	2373.61;
2144.47,	2501.62	,591.51;
426.31,	3105.29,	2057.8;
1507.13	,1556.89	,1954.51;
343.07	,3271.72,	2036.94;
2201.94	,3196.22,	935.53;
2232.43	,3077.87,	1298.87;
1580.1,	1752.07,	2463.04;
1962.4	,1594.97,	1835.95;
1495.18	,1957.44,	3498.02;
1125.17,	1594.39,	2937.73;
24.22	,3447.31,	2145.01;
1269.07	,1910.72	,2701.97;
1802.07,	1725.81,	1966.35;
1817.35,	1927.4	,2328.79;
1860.45,	1782.88	,1875.13;
];
index1=zeros(30,3);
index2=zeros(30,3);
index3=zeros(30,3);
index4=zeros(30,3);
t1=1;t2=1;t3=1;t4=1;
    for i=1:30
        d1=(data(i,1)-k(1))^2+(data(i,2)-k(2))^2+(data(i,3)-k(3))^2;
        d2=(data(i,1)-k22(1))^2+(data(i,2)-k22(2))^2+(data(i,3)-k22(3))^2;
        d3=(data(i,1)-k3(1))^2+(data(i,2)-k3(2))^2+(data(i,3)-k3(3))^2;
        d4=(data(i,1)-k4(1))^2+(data(i,2)-k4(2))^2+(data(i,3)-k4(3))^2;
        d=[d1,d2,d3,d4];
        mind=min(d);
        if mind==d1
            index1(t1,:)=data(i,:);
            t1=t1+1;
        elseif mind==d2
            index2(t2,:)=data(i,:);
            t2=t2+1;
        elseif mind ==d3
            index3(t3,:)=data(i,:);
            t3=t3+1;
        elseif mind==d4
            index4(t4,:)=data(i,:);
            t4=t4+1;
        end
        j=d1+d2+d3+d4+j;
    end
plot3(p1(:,1),p1(:,2),p1(:,3),'r*');hold on;
plot3(p2(:,1),p2(:,2),p2(:,3),'bo');
plot3(p3(:,1),p3(:,2),p3(:,3),'go');
plot3(p4(:,1),p4(:,2),p4(:,3),'k+');
h(1)=plot3(index1(:,1),index1(:,2),index1(:,3),'ro');
hold on;

h(2)=plot3(index2(:,1),index2(:,2),index2(:,3),'b+');

h(3)=plot3(index3(:,1),index3(:,2),index3(:,3),'g*');

h(4)=plot3(index4(:,1),index4(:,2),index4(:,3),'kx');

plot3(k(1),k(2),k(3),'co');
plot3(k22(1),k22(2),k22(3),'co');
plot3(k3(1),k3(2),k3(3),'co');
plot3(k4(1),k4(2),k4(3),'co');
legend([h(1),h(2),h(3),h(4)],'一','二','三','四');
title('酒瓶测试结果');
xlabel('A类特征值');
ylabel('B类特征值');
zlabel('C类特征值');



 使用kmeans算法代码

clear all
data=[864.45	1647.31	2665.9;
877.88	2031.66	3071.18;
1418.79	1775.89	2772.9;
1449.58	1641.58	3045.12;
1739.94	1675.15	2395.96;
1756.77	1652	1514.98;
1803.58	1583.12	2163.05;
1571.17	1731.04	1735.33;
1845.59	1918.81	2226.49;
1692.62	1867.5	2108.97;
1680.67	1575.78	1725.1;
1651.52	1713.28	1570.38;
2352.12	2557.04	1411.53;
2297.28	3340.14	535.62;
2092.62	3177.21	584.32;
2205.36	3243.74	1202.69;
2949.16	3244.44	662.42;
2802.88	3017.11	1984.98;
2063.54	3199.76	1257.21;
373.3	3087.05	2429.47;
222.85	3059.54	2002.33;
401.3	3259.94	2150.98;
363.34	3477.95	2462.86;
104.8	3389.83	2421.83;
104.8	3389.83	2196.22;
172.78	3084.49	2328.65;
341.59	3076.62	2438.63;
291.02	3095.68	2088.95;
237.63	3077.78	2251.96;
1702.8,1639.79,2068.74;
1877.93,1860.96,1975.3;
867.81,2334.68,2535.1;
1831.49,1713.11,1604.68;
460.69,3274.77,2172.99;
2374.98	,3346.98,	975.31;
2271.89,	3482.97	,946.7;
1783.64,	1597.99	,2261.31;
198.83,	3250.45	,2445.08;
1494.63	,2072.59	,2550.51;
1597.03,	1921.52	,2126.76;
1598.93,	1921.08	,1623.33;
1243.13,	1814.07	,3441.07;
2336.31,	2640.26,	1599.63;
354	,3300.12,	2373.61;
2144.47,	2501.62	,591.51;
426.31,	3105.29,	2057.8;
1507.13	,1556.89	,1954.51;
343.07	,3271.72,	2036.94;
2201.94	,3196.22,	935.53;
2232.43	,3077.87,	1298.87;
1580.1,	1752.07,	2463.04;
1962.4	,1594.97,	1835.95;
1495.18	,1957.44,	3498.02;
1125.17,	1594.39,	2937.73;
24.22	,3447.31,	2145.01;
1269.07	,1910.72	,2701.97;
1802.07,	1725.81,	1966.35;
1817.35,	1927.4	,2328.79;
1860.45,	1782.88	,1875.13;
];

k=[864.45 1647.31 2665.9];
k22=[2352.12	2557.04	1411.53];
k3=[1571.17	1731.04	1735.33];
k4=[373.3	3087.05	2429.47];
p=1;
while true
    t1=1;t2=1;t3=1;t4=1;
    index1=zeros(60,3);
    index2=zeros(60,3);
    index3=zeros(60,3);
    index4=zeros(60,3);
    i1=[];
    i2=[];
    i3=[];
    i4=[];    
    for i=1:59
        d1=(data(i,1)-k(1))^2+(data(i,2)-k(2))^2+(data(i,3)-k(3))^2;
        d2=(data(i,1)-k22(1))^2+(data(i,2)-k22(2))^2+(data(i,3)-k22(3))^2;
        d3=(data(i,1)-k3(1))^2+(data(i,2)-k3(2))^2+(data(i,3)-k3(3))^2;
        d4=(data(i,1)-k4(1))^2+(data(i,2)-k4(2))^2+(data(i,3)-k4(3))^2;
        d=[d1,d2,d3,d4];
        mind=min(d);
        if mind==d1
            index1(t1,:)=data(i,:);
            i1(t1)=i;
            t1=t1+1;
        elseif mind==d2
            index2(t2,:)=data(i,:);
           i2(t2)=i;
            t2=t2+1;
        elseif mind ==d3
            index3(t3,:)=data(i,:);
           i3(t3)=i;
            t3=t3+1;
        elseif mind==d4
            index4(t4,:)=data(i,:);
            i4(t4)=i;
            t4=t4+1;
        end
    end
    k=mean(data(i1,:));
    k22=mean(data(i2,:));
    k3=mean(data(i3,:));
    k4=mean(data(i4,:));
    p=p+1;
    if(p==100)
        true=false;
    end
end
plot3(k(1),k(2),k(3),'co');
hold on;
plot3(k22(1),k22(2),k22(3),'co');
plot3(k3(1),k3(2),k3(3),'co');
plot3(k4(1),k4(2),k4(3),'co');
h(1)=plot3(index1(:,1),index1(:,2),index1(:,3),'ro');
h(2)=plot3(index2(:,1),index2(:,2),index2(:,3),'b+');
h(3)=plot3(index3(:,1),index3(:,2),index3(:,3),'g*');
h(4)=plot3(index4(:,1),index4(:,2),index4(:,3),'kx');
legend([h(1),h(2),h(3),h(4)],'一','二','三','四');

title('酒瓶测试结果');
xlabel('A类特征值');
ylabel('B类特征值');
zlabel('C类特征值');
 四、结果

结果自己写啦!^-^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值