IMP logo
IMP Reference Guide  2.19.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 import sys
9 
10 IMP.setup_from_argv(sys.argv, "kmeans example")
11 
12 
13 def add_points(km):
14  # add data
15  km.add_data_pt([1, 2])
16  km.add_data_pt([10, 12])
17  km.add_data_pt([0, -1])
18  km.add_data_pt([11, 13])
19  km.add_data_pt([0, 0])
20  km.add_data_pt([1, 0])
21  km.add_data_pt([9.4, 10])
22  km.add_data_pt([9.2, 11])
23  km.add_data_pt([0, 0])
24 
25 
26 IMP.set_log_level(IMP.PROGRESS)
27 km = IMP.kmeans.KMeans()
28 add_points(km)
29 # cluster data
30 k = 2
31 km.execute(k)
32 # print centers one by one
33 print("\nCenters:")
34 nCenters = km.get_number_of_centers() # this should actually be equal to k
35 print("Number of centers (k): ", nCenters)
36 for i in range(0, nCenters):
37  print("Center ", i, " ", km.get_center(i))
38 
39 # print data point assignments
40 n = km.get_number_of_data_points()
41 assignments = km.get_assignments()
42 sqrdist = km.get_squared_distance_to_centers()
43 print("Number of points: ", n)
44 for i in range(0, n):
45  print("Point ", i, ": ")
46  print(km.get_data_point(i))
47  print("Cluster center: ", assignments[i],
48  " sqrt-dist: ", sqrdist[i])
Strings setup_from_argv(const Strings &argv, std::string description, std::string positional_description, int num_positional)
void set_log_level(LogLevel l)
Set the current global log level.
Interface to the GPL k-means clustering library