KMeans聚类算法

1、算法具体思想请参考ng讲义

__author__ = 'hxw'
#-*- coding=utf-8-*-
import numpy as np
import matplotlib.pyplot as plot
class KMeans():
    def __init__(self,train_x,k=5):
        self.train_x=train_x
        self.k=k
        self.m,self.n=train_x.shape
        self.cluster_index=np.zeros((self.m,1),dtype="int32")
        self.cluster=np.random.randn(self.k,self.n)
    def fit(self):
        cluster_change=True
        while cluster_change:
            cluster_change=False
            #step 1: assign the data to cluster center
            for i in range(self.m):
                min=np.inf
                min_index=-1
                for j in range(self.k):
                    distance=np.sum((self.train_x[i]-self.cluster[j])**2)**0.5
                    if distance<min:
                        min=distance
                        min_index=j
                if self.cluster_index[i,0]!=min_index:
                    cluster_change=True
                    self.cluster_index[i,0]=min_index
            #step 2:calcuate the cluster center
            for i in range(self.k):
                a=self.train_x[self.cluster_index[:,0]==i]
                if (len(a)>0):
                    self.cluster[i]=a.mean(axis=0)

    def show(self):
        color=["bo","go","co","mo","yo","ko","wo"]
        for i in range(self.m):
            plot.plot(self.train_x[i,0],self.train_x[i,1],color[self.cluster_index[i,0]%7])
        plot.plot(self.cluster[:,0],self.cluster[:,1],"r*")
        print len(self.cluster)
        print len(np.unique(self.cluster_index))
        plot.show()
def loaddata(path):
    data=np.loadtxt(path)
    return data
path="D:\\pycharm_project\\KMeans\\Kmeansdata"
data=loaddata(path)
kmeans=KMeans(data,k=5)
kmeans.fit()
kmeans.show()

实验结果:
这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值