码迷,mamicode.com
首页 > 编程语言 > 详细

如何使用Kmeans聚类算法分类数据

时间:2016-05-13 11:26:53      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:

python环境下的测试程序 import numpy as np import cv2 import matplotlib.pyplot as plt #in this example, we test Kmeans clutering algorithm under python. ‘‘‘ #first example: one dimension, one character. # here we create two clusters x and y on purpose. and test this algorithm whether it can tell x from y or not. x = np.random.randint(25, 100, 50) y = np.random.randint(175, 250, 50) z = np.hstack((x, y)) z = z.reshape((100, 1)) z = np.float32(z) #set cluters K = 2 #set bestLabels bestLabels= None #Define criteria = (type, max_iter = 10, epsilon =1.0) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) #set attempts attempts = 10 #set flags flags = cv2.KMEANS_RANDOM_CENTERS #Apply Kmeans compactness, labels, centers = cv2.kmeans(z, K, bestLabels, criteria, attempts, flags, None) # display A = z[labels==0] B = z[labels==1] plt.hist(A, 256, [0, 256], color = ‘r‘) plt.hist(B, 256, [0, 256], color = ‘b‘) plt.hist(centers, 32, [0, 256], color = ‘y‘) plt.show() ‘‘‘ #two dimensions, two characters, and even mutiply dimensions and mutiply characters. x = np.random.randint(150, 180, (2000, 1)) # height y = np.random.randint(50, 80, (2000, 1)) #weight z = np.hstack((x, y)) #Stack arrays in sequence vertically(row wise) #ex: a=np.array([1,2,3]) b=np.array([2,3,4]) c=np.vstack((a,b))=array([[1,2,3],[2,3,4]]) c[:,1]=array([2,3]) #z format: array([x.height, x.weight]), x denotes someone. #Convert to np.float32 z=np.float32(z) #define criteria and apply kmeans #set cluters K = 3 #set bestLabels bestLabels= None #Define criteria = (type, max_iter = 10, epsilon =1.0) criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) #set attempts attempts = 10 #set flags flags = cv2.KMEANS_RANDOM_CENTERS #Apply Kmeans compactness, labels, centers = cv2.kmeans(z, K, bestLabels, criteria, attempts, flags, None) #display A = z[labels.ravel()==0] B = z[labels.ravel()==1] C = z[labels.ravel()==2] plt.scatter(A[:, 0], A[:, 1], color=‘r‘) plt.scatter(B[:, 0], B[:, 1], color=‘g‘) plt.scatter(C[:, 0], C[:, 1], color=‘b‘) plt.scatter(centers[:, 0], centers[:, 1], s=80, color=‘y‘, marker=‘s‘) plt.xlabel(‘Height‘) plt.ylabel(‘Weight‘) plt.show()

如何使用Kmeans聚类算法分类数据

标签:

原文地址:http://www.cnblogs.com/cbyzju/p/5486918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!