栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

机器学习 K-means,聚类算法(简版) 分为两个聚类(两个簇)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

机器学习 K-means,聚类算法(简版) 分为两个聚类(两个簇)

@K-means,聚类算法(简) 分为两个聚类

用的是Spyder编辑器
用到绘图库
和numpy

import numpy as np
import matplotlib.pyplot as plt

x=np.array([[1,2],[1.5,1.8],[5,8],[8,8],[1,0.6],[9,10]])
np.random.seed(5)
x=np.random.rand(10000).reshape(-1,2)

k=2
medoids=[x[1],x[2]]
max_iter=100
for t in range(max_iter): #迭代次数
    
    cluster1=[]
    cluster2=[]
    
    #计算所有点与中心点的距离,根据就近原则将点分配到最近的那个中心所对应的簇
    for i in range(len(x)): 
        d1=np.sum(np.square(x[i]-medoids[0]))
        d2=np.sum(np.square(x[i]-medoids[1]))
        if d1<=d2:
            cluster1.append(x[i])
        else:
            cluster2.append(x[i])
            
    
    medoids=[np.mean(cluster1,axis=0),np.mean(cluster2,axis=0)]    
    print(medoids)
    
cluster1=np.array(cluster1)
cluster2=np.array(cluster2)
plt.plot(cluster1[:,0],cluster1[:,1],'r*')
plt.plot(cluster2[:,0],cluster2[:,1],'g*')

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/350637.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号