-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKmeansTrace_standalone.py
executable file
·104 lines (88 loc) · 3.38 KB
/
KmeansTrace_standalone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/python
import numpy as np
# From http://flothesof.github.io/k-means-numpy.html
import matplotlib.pyplot as plt
import numpy as np
class KmeansTrace_standalone:
def __init__(self, Data2D, K, c, grid):
self.__Data2D = Data2D
self.__K = K
nx, ny = grid.get_nxny()
x, y = grid.get_xy()
self.__Centres_init = np.ndarray(shape=(K, 2), dtype=float)
for k in range(K):
nb, reste = divmod(c[k], ny)
self.__Centres_init[k,:] = [x[nb], y[reste]]
# def initialize_centroids(self.__Data2D, k):
# """returns k centroids from the initial points"""
# centroids = points.copy()
# np.random.shuffle(centroids)
# return centroids[:k]
def closest_centroid(self.__Data2D, self.__Centres_init):
"""returns an array containing the index to the nearest centroid for each point"""
distances = np.sqrt(((points - centroids[:, np.newaxis])**2).sum(axis=2))
return np.argmin(distances, axis=0)
def move_centroids(self.__Data2D, closest, centroids):
"""returns the new centroids assigned from the points closest to them"""
return np.array([points[closest==k].mean(axis=0) for k in range(centroids.shape[0])])
def getAll(self):
return self.__labels_, self.__cluster_centers_, self.__inertia_, self.__best_n_iter_
def getLabels(self):
return self.__labels_
def getCenters(self):
return self.__cluster_centers_
def getInertia(self):
return self.self.__inertia_
def getIter(self):
return self.__best_n_iter_
def getCenter_Init(self):
return self.__Centres_init
#from sklearn.cluster import KMeans
#from sklearn.cluster import k_means
#
#class KmeansTrace:
# def __init__(self, Data2D, K, c, grid):
#
# self.__Data2D = Data2D
# self.__K = K
#
# nx, ny = grid.get_nxny()
# x, y = grid.get_xy()
#
# self.__Centres_init = np.ndarray(shape=(K, 2), dtype=float)
# for k in range(K):
# nb, reste = divmod(c[k], ny)
# self.__Centres_init[k,:] = [x[nb], y[reste]]
#
# self.__cluster_centers_, self.__labels_, self.__inertia_, self.__best_n_iter_ = k_means(self.__Data2D, n_clusters=K, max_iter = 1000, n_init = 1, init=self.__Centres_init, tol= 1e-10, return_n_iter=True)
#
#
# def getAll(self):
# return self.__labels_, self.__cluster_centers_, self.__inertia_, self.__best_n_iter_
# def getLabels(self):
# return self.__labels_
# def getCenters(self):
# return self.__cluster_centers_
# def getInertia(self):
# return self.self.__inertia_
# def getIter(self):
# return self.__best_n_iter_
#
# def getCenter_Init(self):
# return self.__Centres_init
#
# SAFE BACKUP
# def initialize_centroids(points, k):
# """returns k centroids from the initial points"""
# centroids = points.copy()
# np.random.shuffle(centroids)
# return centroids[:k]
#
# def closest_centroid(points, centroids):
# """returns an array containing the index to the nearest centroid for each point"""
# distances = np.sqrt(((points - centroids[:, np.newaxis])**2).sum(axis=2))
# return np.argmin(distances, axis=0)
#
# def move_centroids(points, closest, centroids):
# """returns the new centroids assigned from the points closest to them"""
# return np.array([points[closest==k].mean(axis=0) for k in range(centroids.shape[0])])