Python Image Processing (11): k-means

Source: Internet
Author: User

Python Image Processing (11): k-means

 

 

K-means is a classic clustering algorithm. We try to use it in python.

First, the random coordinate values of 10 points are generated at the centers (-1.5,-1.5) and (1.5, 1.5). We hope to use the K-means algorithm to classify them correctly.

 

 

# Create a test data point. There are two types. # Use (-1.5,-1.5) as the center rand1 = np. ones (10, 2) * (-2) + np. random. rand (10, 2) print (rand1) # centered on (1.5, 1.5) rand2 = np. ones (10, 2) + np. random. rand (10, 2) print (rand2) # merge random vertex data = np. vstack (rand1, rand2 ))

 

Next, kmeans will appear.

The python prototype of this function is:

In [14]: 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, 10, 0.1), 10, cv2.KMEANS_RANDOM_CENTERS )

 

Through this function, we get the center of two classes and the category of each vertex.

Display by category:

 

# Display idx = np by label. hstack (label, label) for I in range (0, 2): type_data = data [idx = I] type_data = np. reshape (type_data, (type_data.shape [0]/2, 2) plt. plot (type_data [:, 0], type_data [:, 1], 'O') plt. show ()

 

A good result:


 

 

 

 

 

 

 

 

 

 

Related Article

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.