IMP  2.4.0
The Integrative Modeling Platform
kmeans_example.py
1 ## \example kmeans/kmeans_example.py
2 # Create a set and points and cluster it, printing the resulting cluster
3 # centers
4 
5 from __future__ import print_function
6 import IMP
7 import IMP.kmeans
8 
9 
10 def add_points(km):
11  # add data
12  km.add_data_pt([1, 2])
13  km.add_data_pt([10, 12])
14  km.add_data_pt([0, -1])
15  km.add_data_pt([11, 13])
16  km.add_data_pt([0, 0])
17  km.add_data_pt([1, 0])
18  km.add_data_pt([9.4, 10])
19  km.add_data_pt([9.2, 11])
20  km.add_data_pt([0, 0])
21 
22 IMP.base.set_log_level(IMP.base.PROGRESS)
23 km = IMP.kmeans.KMeans()
24 add_points(km)
25 # cluster data
26 k = 2
27 km.execute(k)
28 # print centers one by one
29 print("\nCenters:")
30 nCenters = km.get_number_of_centers() # this should actually be equal to k
31 print("Number of centers (k): ", nCenters)
32 for i in range(0, nCenters):
33  print("Center ", i, " ", km.get_center(i))
34 
35 # print data point assignments
36 n = km.get_number_of_data_points()
37 assignments = km.get_assignments()
38 sqrdist = km.get_squared_distance_to_centers()
39 print("Number of points: ", n)
40 for i in range(0, n):
41  print("Point ", i, ": ")
42  print(km.get_data_point(i))
43  print("Cluster center: ", assignments[i], \
44  " sqrt-dist: ", sqrdist[i])
void set_log_level(LogLevel l)
Set the current global log level.
Interface to the GPL k-means clustering library