zoukankan      html  css  js  c++  java
  • python k-means聚类实例

    port  sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    
    import matplotlib.pyplot as plt
    import numpy as np
    
    culster1 = np.random.uniform(0.5, 1.5, (2, 20))
    culster2 = np.random.uniform(1.5, 2.5, (2, 20))
    culster3 = np.random.uniform(1.5, 3.5, (2, 20))
    culster4 = np.random.uniform(3.5, 4.5, (2, 20))
    
    x1 = np.hstack((culster1,culster2))
    x2 = np.hstack((culster2,culster3))
    x = np.hstack((x1,x2)).T
    
    plt.figure()
    plt.axis([0, 5, 0, 5])
    plt.xlabel('x')
    plt.ylabel('y')
    plt.grid(True)
    plt.plot(x[:,0],x[:,1], 'k.', markersize = 12)
    
    from sklearn.cluster import KMeans
    from scipy.spatial.distance import cdist
    
    kmeans = KMeans(n_clusters = 2)
    kmeans.fit(x)
    plt.plot(kmeans.cluster_centers_[:,0],kmeans.cluster_centers_[:,1],'ro')
    
    K = range(1, 10)
    meandistortions = []
    for k in K:
        kmeans = KMeans(n_clusters=k)
        kmeans.fit(x)
        meandistortions.append(sum(np.min(cdist(x, kmeans.cluster_centers_,'euclidean'), axis=1)) / x.shape[0])#选择每行最小距离求和
    plt.figure()
    plt.grid(True)
    plt1 = plt.subplot(2,1,1)
    plt1.plot(x[:,0], x[:,1], 'k.')
    plt2 = plt.subplot(2,1,2)
    plt2.plot(K, meandistortions)

  • 相关阅读:
    poj 2253
    POJ 3273
    python基本运算符
    python基本数据类型以及常量变量
    pycharm的快捷键
    计算机基础
    day100 scrapy请求传参 中间件 去重规则 分布式爬虫
    day99 爬虫 scrapy介绍 结构介绍
    day98 爬虫 selenium
    day97 爬虫bs4
  • 原文地址:https://www.cnblogs.com/Kermit-Li/p/6720271.html
Copyright © 2011-2022 走看看