美文网首页大作业
K-means报告(模式识别3)

K-means报告(模式识别3)

作者: 小火伴 | 来源:发表于2018-01-17 17:18 被阅读59次
K-Means-1.png K-Means-2.png K-Means-3.png K-Means-4.png

程序



# coding: utf-8

# # 第三次模式识别作业


# In[1]:

get_ipython().magic('matplotlib inline')


# In[2]:

from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import numpy as np


# In[ ]:

K=3
iris = load_iris()
X = iris.data
Y = iris.target


# # 随机洗牌数据

# In[13]:

shuffle_para=np.arange(Y.shape[0])
np.random.shuffle(shuffle_para)
X,Y=X[shuffle_para],Y[shuffle_para]


# # 每次随机一样

# In[ ]:

np.random.seed(980406)


# # 分类

# In[ ]:

cla=[]
for i in range(K):
    cla.append(np.where(Y==i))


# # 初始点

# In[14]:

initial_point=X[np.random.randint(0,X.shape[0],(3,))]
initial_point


# In[15]:

mean_point=initial_point


# In[16]:

print(X.shape)


# # 开始迭代

# In[17]:

accu=[]
n=0
while True:
    # 计算到k个中心的欧氏距离
    distances=[]
    for p in mean_point:
        distances.append(np.linalg.norm((X-p),axis=1))
        pass
    distances=np.array(distances)
    y=np.argmin(distances,0)
    y=np.array(y,dtype=int)
    # 保存上次点
    last_point=mean_point
    # 生成新点
    mean_point=[]
    for i in range(K):
        mean_point.append(np.mean(X[(y==i),:],axis=0))
    mean_point=np.array(mean_point)
    J=np.linalg.norm(last_point-mean_point,axis=1)
    # 每一个都是<0.01
    if False not in list(J<0.001):
        break
        pass
    if(n==20):
        print('到达最大迭代次数')
        break
    
    # 看把原始数据的每一类还保留多少个为一类
    corr=0
    for c in cla:
        corr+=(max(np.bincount(y[c])))
    accu.append(corr/Y.shape[0])
    print(accu[-1])
    n+=1
    pass


# # 画图

# In[18]:

plt.ylim([0.6,1])
plt.xticks(list(range(n)), rotation=20)
plt.xlabel('Interations')
plt.ylabel('Accuracy')
plt.plot(np.arange(n),accu)


# In[19]:

mean_point.shape


# In[20]:

label=(('Sepal length','Sepal width'),('Petal length','Petal width'))
def scat(i):
    plt.scatter(X[:, i*2], X[:,2*(i+1)-1], c=y,marker='+')
    plt.scatter(mean_point[:,i*2],mean_point[:,(i+1)*2-1],c=np.arange(K),marker='o')
    plt.xlabel(label[i][0])
    plt.ylabel(label[i][1])
i=0
scat(i)


# In[21]:

scat(1)


相关文章

  • K-means报告(模式识别3)

    程序

  • K-means

    这个K-means是我的模式识别课程的一次实验,初始的k个向量是给定的,所以我并没有用random库来随机生成。此...

  • 2019-01-10[Stay Sharp]k-means cl

    what is k-means clustering? K-means clustering is a metho...

  • 聚类、分类

    clustering k-means法 API 批量 K-means 法 MiniBatchKMeans API ...

  • k-means算法总结

    目录 一、k-means算法原理 二、k-means算法目标函数是什么 三、总结 一、k-means算法原理 k-...

  • 05 聚类算法 - 二分K-Means、K-Means++、K-

    03 聚类算法 - K-means聚类04 聚类算法 - 代码案例一 - K-means聚类 三、K-Means算...

  • K-Means聚类算法衍生

    1. 二分K-Means算法(bisecting K-means) 为解决K-Means算法簇中心敏感问题,二分K...

  • 笔记

    机器学习->模式识别->模式信息表现(相关疾病)。 大脑皮层: 1. the Gordon atlas 3...

  • 2020 机器学习之Kmeans

    K-means k-means 是一种搜寻中心的无监督的算法。K-means 是一种迭代的不确定方法,所谓迭代,是...

  • 认知方法论笔记(十四)

    第十四天 直觉与数据主义 认知中的模式识别与机器学习: “模式识别”是演绎,“机器学习”是归纳。 模式识别——锤子...

网友评论

    本文标题:K-means报告(模式识别3)

    本文链接:https://www.haomeiwen.com/subject/kqpuoxtx.html