第七次作业

import numpy as np
x=np.random.randint(0,12,20)
x

array([ 5, 1, 7, 2, 3, 10, 9, 9, 5, 8, 9, 0, 8, 9, 2, 6, 6, 5, 8, 9])

k=3
y=np.zeros(20)
y

array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

def initcent(x,k):#初始聚类中心数组    
    return x[0:k].reshape(k)

kc=initcent(x,k)
kc

array([5, 1, 7])

def nearset(kc,i):#距离绝对值
    d=(abs(kc-i))
    w=np.where(d==np.min(d))
    return w[0][0]

def xclassify(x,y,kc):
    for i in range(x.shape[0]):#对数组的每个值进行分类
        y[i]=nearset(kc,x[i])
    return y
kc=initcent(x,k)
y=xclassify(x,y,kc)
print(kc,y)

[5 1 7] [0. 1. 2. 1. 0. 2. 2. 2. 0. 2. 2. 1. 2. 2. 1. 0. 0. 0. 2. 2.]

import numpy as np
from sklearn.datasets import load_iris    
iris=load_iris()
x=iris.data[:,1]
y=np.zeros(150)

def initcenter(x,k):    
    return x[0:k].reshape(k)

def nearest(kc,i):       
    d=(abs(kc-i))
    w=np.where(d == np.min(d))
    return w[0][0]

def xclassify(x,y,kc):
    for i in range(x.shape[0]):       
        y[i]=nearest(kc,x[i])
    return y

def kcmean(x,y,kc,k):     
    l=list(kc)
    flag=False
    for c in range(k):
        print(c)
        m=np.where(y == c)
        n=np.mean(x[m])
        if l[c] !=n:
            l[c]=n
            flag=True     
            print(l,flag)
    return (np.array(l),flag)


k=3
kc=initcenter(x,k)

flag=True
print(x,y,kc,flag)

#判断聚类中心和目标函数的值是否发生改变,若不变,则输出结果,若改变,则返回2
while flag:
    y=xclassify(x,y,kc)
    kc,flag = kcmean(x,y,kc,k)
    print(y,kc,type(kc))
    
print(x,y)
import matplotlib.pyplot as plt
plt.scatter(x,x,c=y,s=50,cmap="rainbow");
plt.show()
[3.5 3.  3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 3.7 3.4 3.  3.  4.  4.4 3.9 3.5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421052631576, 3.2] True
2
[3.638888888888889, 2.7968421052631576, 3.2315789473684213] True
[0. 1. 2. 1. 0. 0. 0. 0. 1. 1. 0. 0. 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 2.
 0. 1. 0. 0. 0. 2. 1. 0. 0. 0. 1. 2. 0. 1. 1. 0. 0. 1. 2. 0. 0. 1. 0. 2.
 0. 2. 2. 2. 1. 1. 1. 1. 2. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 2. 1.
 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
 1. 1. 1. 1. 2. 1. 1. 1. 1. 1. 1. 1. 1. 0. 2. 1. 1. 1. 1. 2. 1. 0. 1. 1.
 2. 1. 1. 1. 2. 2. 1. 1. 1. 1. 1. 0. 1. 1. 1. 1. 0. 1. 1. 1. 1. 1. 1. 2.
 2. 1. 1. 1. 0. 1.] [3.63888889 2.79684211 3.23157895] <class 'numpy.ndarray'>
0
[3.7583333333333333, 2.7968421052631576, 3.2315789473684213] True
1
[3.7583333333333333, 2.753012048192771, 3.2315789473684213] True
2
[3.7583333333333333, 2.753012048192771, 3.2418604651162792] True
[0. 1. 2. 2. 0. 0. 2. 2. 1. 2. 0. 2. 1. 1. 0. 0. 0. 0. 0. 0. 2. 0. 0. 2.
 2. 1. 2. 0. 2. 2. 2. 2. 0. 0. 2. 2. 0. 2. 1. 2. 0. 1. 2. 0. 0. 1. 0. 2.
 0. 2. 2. 2. 2. 1. 1. 1. 2. 1. 1. 1. 1. 1. 1. 1. 1. 2. 1. 1. 1. 1. 2. 1.
 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 2. 2. 1. 1. 1. 1. 1. 1. 1. 1. 1.
 1. 1. 1. 1. 2. 1. 1. 1. 1. 1. 1. 1. 1. 0. 2. 1. 1. 1. 1. 2. 1. 0. 1. 1.
 2. 1. 1. 1. 2. 2. 1. 1. 1. 1. 1. 0. 1. 1. 1. 1. 2. 2. 1. 2. 2. 2. 1. 2.
 2. 1. 1. 1. 2. 1.] [3.75833333 2.75301205 3.24186047] <class 'numpy.ndarray'>
0
[3.8444444444444437, 2.753012048192771, 3.2418604651162792] True
1
[3.8444444444444437, 2.6403508771929824, 3.2418604651162792] True
2
[3.8444444444444437, 2.6403508771929824, 3.1786666666666665] True
[2. 2. 2. 2. 0. 0. 2. 2. 1. 2. 0. 2. 2. 2. 0. 0. 0. 2. 0. 0. 2. 0. 0. 2.
 2. 2. 2. 2. 2. 2. 2. 2. 0. 0. 2. 2. 2. 2. 2. 2. 2. 1. 2. 2. 0. 2. 0. 2.
 0. 2. 2. 2. 2. 1. 1. 1. 2. 1. 1. 1. 1. 2. 1. 1. 1. 2. 2. 1. 1. 1. 2. 1.
 1. 1. 1. 2. 1. 2. 1. 1. 1. 1. 1. 1. 2. 2. 2. 1. 2. 1. 1. 2. 1. 1. 1. 2.
 1. 1. 1. 1. 2. 1. 2. 1. 2. 2. 1. 1. 1. 0. 2. 1. 2. 1. 1. 2. 2. 0. 1. 1.
 2. 1. 1. 1. 2. 2. 1. 2. 1. 2. 1. 0. 1. 1. 1. 2. 2. 2. 2. 2. 2. 2. 1. 2.
 2. 2. 1. 2. 2. 2.] [3.84444444 2.64035088 3.17866667] <class 'numpy.ndarray'>
0
1
2
[2. 2. 2. 2. 0. 0. 2. 2. 1. 2. 0. 2. 2. 2. 0. 0. 0. 2. 0. 0. 2. 0. 0. 2.
 2. 2. 2. 2. 2. 2. 2. 2. 0. 0. 2. 2. 2. 2. 2. 2. 2. 1. 2. 2. 0. 2. 0. 2.
 0. 2. 2. 2. 2. 1. 1. 1. 2. 1. 1. 1. 1. 2. 1. 1. 1. 2. 2. 1. 1. 1. 2. 1.
 1. 1. 1. 2. 1. 2. 1. 1. 1. 1. 1. 1. 2. 2. 2. 1. 2. 1. 1. 2. 1. 1. 1. 2.
 1. 1. 1. 1. 2. 1. 2. 1. 2. 2. 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1. 2. 1. 1. 1. 2.
 1. 1. 1. 1. 2. 1. 2. 1. 2. 2. 1. 1. 1. 0. 2. 1. 2. 1. 1. 2. 2. 0. 1. 1.
 2. 1. 1. 1. 2. 2. 1. 2. 1. 2. 1. 0. 1. 1. 1. 2. 2. 2. 2. 2. 2. 2. 1. 2.
 2. 2. 1. 2. 2. 2.]
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import load_iris

iris=load_iris()
X=iris.data
X

from sklearn.cluster import KMeans

est = KMeans(n_clusters = 3)
est.fit(X)
kc = est.cluster_centers_
y_kmeans = est.predict(X)   
print(y_kmeans,kc)
print(kc.shape,y_kmeans.shape,np.shape)

plt.scatter(X[:,0],X[:,1],c=y_kmeans,s=50,cmap='rainbow');
plt.show()
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 2 2 2 1 2 2 2 2
 2 2 1 1 2 2 2 2 1 2 1 2 1 2 2 1 1 2 2 2 2 2 1 2 2 2 2 1 2 2 2 1 2 2 2 1 2
 2 1] [[5.006      3.418      1.464      0.244     ]
 [5.9016129  2.7483871  4.39354839 1.43387097]
 [6.85       3.07368421 5.74210526 2.07105263]]
(3, 4) (150,) <function shape at 0x00000249C0DE21E0>
posted @ 2018-11-10 16:11  梁柏钧  阅读(125)  评论(0编辑  收藏  举报