Python clustering algorithm-aggregated hierarchical clustering instance analysis, python Clustering

Source: Internet
Author: User

Python clustering algorithm-aggregated hierarchical clustering instance analysis, python Clustering

This example describes the clustering of Python clustering algorithms. We will share this with you for your reference. The details are as follows:

Hierarchical Clustering:The so-called Consortium refers to the algorithm that uses each vertex as a cluster at the beginning, and merges two closest clusters at each step. In addition, even at the end, noise points or outlier points are usually in a cluster, unless excessively merged. There are three definitions for the "closest" here. I used MIN in the implementation. When merging this method, we only need to take the closest point in sequence. If this point is not in the same cluster, we can merge the two clusters:

Single-chain (MIN): defines the closeness of a cluster to the distance between two closest points of different clusters.
Full chain (MAX): defines the closeness of a cluster to the distance between the two Farthest Points of different clusters.
Group average: defines the degree of closeness of a cluster as the average value of the degree of closeness of all points in two different clusters.

# Scoding = UTF-8 # Agglomerative Hierarchical Clustering (AHC) import pylab as plfrom operator import itemgetterfrom collections import OrderedDict, Counterpoints = [[int (eachpoint. split ('#') [0]), int (eachpoint. split ('#') [1])] for eachpoint in open ("points", "r")] # Initially, each vertex is assigned a single cluster groups = [idx for idx in range (len (points)] # calculate the distance between each vertex disP2P ={} for idx1, point1 in enumerate (points): for idx2, point2 in enu Merate (points): if (idx1 <idx2): distance = pow (abs (point1 [0]-point2 [0]), 2) + pow (abs (point1 [1]-point2 [1]), 2) disP2P [str (idx1) + "#" + str (idx2)] = distance # Sort each point by distance in descending order. disP2P = OrderedDict (sorted (disP2P. iteritems (), key = itemgetter (1), reverse = True) # current number of clusters groupNum = len (groups) # excessive merging will affect noise, when the number of clusters is reduced to finalGroupNum, stop merging finalGroupNum = int (groupNum * 0.1) while groupNum> finalGroupNum: # select the next closest point to t. Wopoins, distance = disP2P. popitem () pointA = int (twopoins. split ('#') [0]) pointB = int (twopoins. split ('#') [1]) pointAGroup = groups [pointA] pointBGroup = groups [pointB] # If the last two points are not in the same cluster, merge all vertices in the cluster where point B is located to the cluster where point A is located. At this time, the number of current clusters is reduced by 1 if (pointAGroup! = PointBGroup): for idx in range (len (groups): if groups [idx] = pointBGroup: groups [idx] = pointAGroup groupNum-= 1 # select the three largest clusters. Other clusters are classified as noise points wantGroupNum = 3 finalGroup = Counter (groups ). most_common (wantGroupNum) finalGroup = [onecount [0] for onecount in finalGroup] dropPoints = [points [idx] for idx in range (len (points )) if groups [idx] not in finalGroup] # print the point group1 = [points [idx] for idx in xrange (len (points) in the three largest clusters )) if groups [idx] = finalGroup [0] group2 = [points [idx] for idx in xrange (len (points )) if groups [idx] = finalGroup [1] group3 = [points [idx] for idx in xrange (len (points )) if groups [idx] = finalGroup [2] pl. plot ([eachpoint [0] for eachpoint in group1], [eachpoint [1] for eachpoint in group1], 'or') pl. plot ([eachpoint [0] for eachpoint in group2], [eachpoint [1] for eachpoint in group2], 'oy') pl. plot ([eachpoint [0] for eachpoint in group3], [eachpoint [1] for eachpoint in group3], 'og ') # print noise point, black pl. plot ([eachpoint [0] for eachpoint in dropPoints], [eachpoint [1] for eachpoint in dropPoints], 'OK') pl. show ()

The running effect is as follows:

I hope this article will help you with Python programming.

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.