IMP logo
IMP Reference Guide  develop.d97d4ead1f,2024/11/21
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 import IMP
6 import IMP.kmeans
7 import sys
8 
9 IMP.setup_from_argv(sys.argv, "kmeans example")
10 
11 
12 def add_points(km):
13  # add data
14  km.add_data_pt([1, 2])
15  km.add_data_pt([10, 12])
16  km.add_data_pt([0, -1])
17  km.add_data_pt([11, 13])
18  km.add_data_pt([0, 0])
19  km.add_data_pt([1, 0])
20  km.add_data_pt([9.4, 10])
21  km.add_data_pt([9.2, 11])
22  km.add_data_pt([0, 0])
23 
24 
25 IMP.set_log_level(IMP.PROGRESS)
26 km = IMP.kmeans.KMeans()
27 add_points(km)
28 # cluster data
29 k = 2
30 km.execute(k)
31 # print centers one by one
32 print("\nCenters:")
33 nCenters = km.get_number_of_centers() # this should actually be equal to k
34 print("Number of centers (k): ", nCenters)
35 for i in range(0, nCenters):
36  print("Center ", i, " ", km.get_center(i))
37 
38 # print data point assignments
39 n = km.get_number_of_data_points()
40 assignments = km.get_assignments()
41 sqrdist = km.get_squared_distance_to_centers()
42 print("Number of points: ", n)
43 for i in range(0, n):
44  print("Point ", i, ": ")
45  print(km.get_data_point(i))
46  print("Cluster center: ", assignments[i],
47  " 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