zoukankan      html  css  js  c++  java
  • 数据处理之聚类分析

    # -*- coding: utf-8 -*-
    from sklearn.cluster import KMeans
    from sklearn.externals import joblib
    import numpy ,time,pdb
    import matplotlib.pyplot as plt
    from sklearn.cluster import MeanShift, estimate_bandwidth
    import numpy as np

    if __name__ == '__main__':
    # step 1: 加载数据
    print "step 1: load data..."

    dataSet = []
    fileIn = open('./MOC_X1000_20170811110600_MHAIL_00.txt')
    for line in fileIn.readlines()[1:]:
    lineArr = line.strip().split(',')
    dataSet.append([float(lineArr[0]), float(lineArr[1])])

    numSamples = len(dataSet)
    X = np.array(dataSet) #列表类型转换成array数组类型
    # bandwidth = estimate_bandwidth(X, quantile=0.2, n_samples=500)
    bandwidth = estimate_bandwidth(X, quantile=0.5, n_samples=numSamples)
    clf = MeanShift(bandwidth=bandwidth, bin_seeding=True,cluster_all=True).fit(X)

    centroids = clf.labels_
    print centroids,type(centroids) #显示每一个点的聚类归属
    # 计算其自动生成的k,并将聚类数量小于3的排除
    arr_flag = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    for i in clf.labels_:
    arr_flag[i]+=1
    k = 0
    # pdb.set_trace()
    for i in arr_flag:
    if(i > 45):
    k +=1
    print k

    mark = ['or', 'ob', 'og', 'ok', '^r', '+r', 'sr', 'dr', '<r', 'pr']
    #画出所有样例点 属于同一分类的绘制同样的颜色
    for i in xrange(numSamples):
    plt.plot(dataSet[i][0], dataSet[i][1], mark[clf.labels_[i]]) #mark[markIndex])

    mark = ['Dr', 'Db', 'Dg', 'Dk', '^b', '+b', 'sb', 'db', '<b', 'pb']
    # 画出质点,用特殊图型
    centroids =clf.cluster_centers_
    for i in range(k):
    plt.plot(centroids[i][0], centroids[i][1], mark[i], markersize = 12)
    # plt.show()
    print centroids #显示中心点坐标
    plt.show()

  • 相关阅读:
    sublime
    C++两种字符串传参构造函数
    Navicat 导入Excel与增加主键
    5,做一个用户登录之后查看学员信息的小例子
    4,Flask 中的 request
    3,Flask 中的模板语言 Jinja2 及 render_template 的深度用法
    2,Flask 中的 Render Redirect HttpResponse
    1,flask简介
    11,手动绘制散点图的背景颜色
    10,knn手写数字识别
  • 原文地址:https://www.cnblogs.com/xiaoxiaoshuaishuai0219/p/8353648.html
Copyright © 2011-2022 走看看