K-means算法

K-Means算法主要解决的问题如下图所示。我们可以看到,在图的左边有一些点,我们用肉眼可以看出来有四个点群,但是我们怎么通过计算机程序找出这几个点群来呢?于是就出现了我们的K-Means算法

k-means 要解决的问题

算法概要


A,B,C,D,E是五个在图中点。而灰色的点是我们的种子点,也就是我们用来找点群的点。有两个种子点,所以K=2。

步骤如下:

  1. 随机在图中取K(这里K=2)个种子点
  2. 然后对图中的所有点求到这K个种子点的距离,假如点Pi离种子点Si最近,那么Pi属于Si点群。(上图中,我们可以看到A,B属于上面的种子点,C,D,E属于下面中部的种子点)
  3. 接下来,我们要移动种子点到属于他的“点群”的中心。(见图上的第三步)
  4. 然后重复第2)和第3)步,直到,种子点没有移动(我们可以看到图中的第四步上面的种子点聚合了A,B,C,下面的种子点聚合了D,E)。

K-means ++

k-means++算法选择初始seeds的基本思想就是:初始的聚类中心之间的相互距离要尽可能的远,弥补了初始的k-means算法的种子选择的缺陷。该算法的描述是如下:

  1. 从输入的数据点集合中随机选择一个点作为第一个聚类中心
  2. 对于数据集中的每一个点x,计算它与最近聚类中心(指已选择的聚类中心)的距离D(x)
  3. 选择一个新的数据点作为新的聚类中心,选择的原则是:D(x)较大的点,被选取作为聚类中心的概率较大
  4. 重复2和3直到k个聚类中心被选出来
  5. 利用这k个初始的聚类中心来运行标准的k-means算法
import random as rand
import math as math

from Point import Point
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

class clustering:
    def __init__(self, geo_locs_ , k_):
        self.geo_locations = geo_locs_
        self.k = k_
        self.clusters = []
        self.means = []
        self.debug = False

    #this method returns the next random node
    def next_random(self , index , points , clusters):
        #pick next node that has the maximum distance from other nodes
        dist = {}
        for point_1 in points:
            if self.debug:
                print('point_1: %f %f' %(point_1.latit , point_1.longit))

            for cluster in clusters.values():
                point_2 = cluster[0]
                if self.debug:
                    print('point_2:%f %f' % (point_2.latit , point_2.longit))
                if point_1 not in dist:
                    dist[point_1] = math.sqrt(math.pow(point_1.latit - point_2.latit , 2.0) + math.pow(point_1.longit - point_2.longit,2.0))
                else:
                    dist[point_1] += math.sqrt(math.pow(point_1.latit - point_2.latit , 2.0) + math.pow(point_1.longit - point_2.longit , 2.0))

        if self.debug:
            for key , value in dist.items():
                print('(%f , %f) ==> %f' % (key.latit , key.longit , value))
        #now let's return the point that has the maxmium distance from previous nodes
        count_ = 0
        max_ = 0
        for key , value in dist.items():
            if count_ == 0:
                max_ = value
                max_point = key
                count_ += 1
            else:
                if value > max_:
                    max_ = value
                    max_point = key
        return max_point

    #this method computes the initial means
    def initial_means(self , points):
        #pick the first node at random 
        point_ = rand.choice(points)
        if self.debug:
            print('poing#0: %f %f' % (point_.latit , point_.longit))
        clusters = dict()
        clusters.setdefault( 0 , []).append(point_)
        points.remove(point_)
        #now let's pick k-1 more random points
        for i in range(1,self.k):
            point_ = self.next_random(i , points , clusters)
            if self.debug:
                print('point#%d: %f %f' % (i , point_.latit , point_.longit))

            clusters.setdefault(i , []).append(point_)
            points.remove(point_)

        #compute mean of clusters
        #self.print_clusters(clusters)
        self.means = self.compute_mean(clusters)
        if self.debug:
            print("initial means:")
            self.print_means(self.means)

    def compute_mean(self , clusters):
        means = []
        for cluster in clusters.values():
            mean_point = Point( 0.0 , 0.0)
            cnt = 0.0
            for point in cluster:
                mean_point.latit += point.latit
                mean_point.longit += point.longit
                cnt += 1.0

            mean_point.latit = mean_point.latit/cnt
            mean_point.longit = mean_point.longit/cnt
            means.append(mean_point)
        return means

    #this method assign nodes to the cluster with the smallest mean
    def assign_points(self  , points):
        if self.debug:
            print("assign points")
        clusters = dict()

        for point in points:
            dist = []
            if self.debug:
                print("point(%f %f)"%(point.latit , point.longit))
            #find the best cluster for this node
            for mean in self.means:
                dist.append(math.sqrt(math.pow(point.latit - mean.latit, 2.0) + math.pow(point.longit - mean.longit , 2.0)))
            #let's find the smallest mean
            if self.debug:
                print(dist)
            cnt_ = 0
            index = 0
            min_ = dist[0]
            for d in dist:
                if d < min_:
                    min_ = d
                    index = cnt_
                cnt_ += 1

            if self.debug:
                print("index:%d" % index)

            clusters.setdefault(index , []).append(point)
        return clusters
    def update_means(self , means, threshold):
        #check the curren mean with previous one to see if we should stop
        for i in range(len(self.means)):
            mean_1 = self.means[i]
            mean_2 = means[i]
            if self.debug:
                print("mean_1(%f , %f)" % (mean_1.latit , mean_1.longit))
                print("mean_2(%f , %f)" % (mean_2.latit , mean_2.longit))

            if math.sqrt(math.pow(mean_1.latit - mean_2.latit , 2.0) + math.pow(mean_1.longit - mean_2.longit,2.0)) > threshold:
                return False

        return True

    def print_clusters(self , clusters):
        cluster_cnt = 1
        for cluster in clusers.values():
            print("nodes in cluster #%d" % cluster_cnt)
            cluster_cnt += 1
            for point in cluster:
                print("point(%f, %f" % (point.latit , point.longit))

    def print_means(self , means):
        for point in means:
            print("%f %f" % (point.latit , point.longit))

    def k_means(self , plot_flag):
        if len(self.geo_locations) < self.k:
            return -1

        points_ = [point for point in self.geo_locations]
        #compute the initial means
        self.initial_means(points_)
        stop = False
        while not stop:
            #assignment step:assign each node to the cluster with the cloest mean
            points_ = [point for point in self.geo_locations]
            clusters = self.assign_points(points_)
            if self.debug:
                self.print_clusters(clusters)
            means = self.compute_mean(clusters)
            if self.debug:
                print("means:")
                print(self.print_means(means))
                print("update mean:")
            stop = self.update_means(means , 0.01)
            if not stop:
                self.means = []
                self.means = means
        self.clusters = clusters
        if plot_flag:
            fig = plt.figure()
            ax = fig.add_subplot(111)
            markers = ['o','d','x','h',7,4,5,6,'8','p',',','+',',','s','*',3,0,1,2]
            colors = ['r','k','b',[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]]
            cnt = 0
            for cluster in clusters.values():
                latits = []
                longits = []
                for point in cluster:
                    latits.append(point.latit)
                    longits.append(point.longit)
                ax.scatter(longits , latits , s = 60 , c = colors[cnt] , marker = markers[cnt])
                cnt += 1
            plt.show()
        return 0
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容