Python image processing (one): K-Mean value

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.