Happy Shrimp
http://blog.csdn.net/lights_joy/
Welcome reprint, but please keep the author information
k python
The random coordinate values of each of the ten points are generated at the center point ( -1.5, -1.5) and (1.5, 1.5) , and we want to use a the K -mean algorithm classifies them correctly.
# to create a test data point, Class 2 # with (-1.5,-1.5) as the center Rand1 = np.ones ((10,2)) * ( -2) + Np.random.rand (ten, 2) print (RAND1) # with (1.5, 1.5) as Center Rand2 = n P.ones ((10,2)) + Np.random.rand (2) print (rand2) # merge random points data = Np.vstack ((rand1, Rand2))
Next Kmeans appearances.
of this function python The prototypes are:
in [+]: Help (Cv2.kmeans)
Help on built-in function Kmeans:
Kmeans (...)
Kmeans (data, k,bestlabels, criteria, attempts, flags[, centers]), retval, bestlabels,centers >
Call it in our script:
# Kmeans (RET, label, center) = Cv2.kmeans (data, 2, None, (CV2). Term_criteria_eps | Cv2. Term_criteria_max_iter, ten, 0.1), Cv2. Kmeans_random_centers)
with this function we get the 2 The center point of a category and the category to which each point belongs.
Finally by category display:
# categorized by label idx = Np.hstack ((label, label)) for I in range (0, 2): type_data = Data[idx = i] type_data = Np.resh Ape (Type_data, (Type_data.shape[0]/2, 2)) Plt.plot (type_data[:,0], type_data[:,1], ' O ') plt.show ()
A very good result:
??
Python image processing (one): K-Mean value