标签:
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()标签:
原文地址:http://www.cnblogs.com/cbyzju/p/5486918.html