Repository navigation
Expand file tree
/
Copy pathExample.py
More file actions
172 lines (144 loc) · 4.6 KB
/
Copy pathExample.py
File metadata and controls
172 lines (144 loc) · 4.6 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import numpy as np
import random
import matplotlib.pyplot as plt
import matplotlib
import os
import pandas as pd
COLORS = [
'tab:blue',
'tab:orange',
'tab:green',
'tab:red',
'tab:purple',
'tab:brown',
'tab:pink',
'tab:gray',
'tab:olive',
'tab:cyan',
]
def show_clusters_centroids(clusters,centroids,title,x_var_indx=0,y_var_indx=1,x_var_name='Variable 1',y_var_name="Variable 2",keep=False):
"""
Show the current clustering for 1 second and save the plot
Input:
clusters (list of lists of lists): A List of Clusters. Each cluster
is also a list of points in the cluster. SEE: k_means.get_clusters()
centroids (list of lists): A list with the current centroids
title (string): The title for the plot.
"""
for i, cluster in enumerate(clusters):
cluster = np.array(cluster)
plt.scatter(
cluster[:,x_var_indx],
cluster[:,y_var_indx],
c = COLORS[i],
label="Cluster {}".format(i)
)
for i, centroid in enumerate(centroids):
plt.scatter(
centroid[x_var_indx],
centroid[y_var_indx],
c = COLORS[i],
marker='x',
s=100
)
plt.title(title)
plt.xlabel(x_var_name)
plt.ylabel(y_var_name)
plt.legend()
if not keep:
plt.show(block=False)
plt.pause(1)
plt.close()
else:
plt.show()
def load_data(filename):
"""
Reads a csv file and returns a list of lists
"""
with open(filename,'r') as fp:
data = fp.read().split('\n')
data_new = [f.split(',') for f in data if f != ""]
data_formatted = []
for instance in data_new:
instance_new = []
for value in instance:
try:
instance_new.append(float(value))
except ValueError:
instance_new.append(value)
data_formatted.append(instance_new)
return data_formatted
def distance(a,b):
"""
Compute Euclidean Distance Between Two Points
Input:
a (list): an n-dimensional list or array
b (list): an n-dimensional list or array
Output:
The Euclidean Distance between vectors a and b
"""
return np.sqrt(np.sum((np.array(b)-np.array(a))**2))
def get_clusters(points,centroids):
"""
Returns a list of clusters given all the points in the dataset and
the current centroids.
Input:
points (list of lists): A list with every point in the dataset
centroids (list of lists): A list with the current centroids
Output:
clusters (list of lists of lists): A List of Clusters. Each cluster
is also a list of points in the cluster.
"""
clusters = [[] for f in centroids]
for i, point in enumerate(points):
point_to_centroids = []
for j, centroid in enumerate(centroids):
point_to_centroids.append(distance(point,centroid))
closest_idx = np.argmin(point_to_centroids)
clusters[closest_idx].append(point)
return clusters
def update_centroids(clusters):
"""
Given a list of clusters (as prepared by get_clusters) get the new centroids
Input:
clusters (list of lists of lists): A List of Clusters. Each cluster
is also a list of points in the cluster.
Output:
A (list of lists): The new centroids.
"""
new_centroids = []
for cluster in clusters:
new_centroids.append(np.mean(cluster,axis = 0))
return new_centroids
def k_means(points, k, iterations=10):
"""
K Means Unsupervised ML Algorithm Implementation with Forgy Initialization
Input:
points (numpy array): a 2D Array with the data to cluster.
k (int): The number of clusters to find
"""
idx = np.random.randint(len(points),size=k)
centroids = points[idx,:]
clusters = get_clusters(points,centroids)
for i in range(iterations):
if i % 1 == 0:
if i == 0:
title = "Initialization"
else:
title = "Iteration {}".format(i+1)
show_clusters_centroids(
clusters,
centroids,
title,
)
clusters = get_clusters(points,centroids)
centroids = update_centroids(clusters)
return clusters,centroids
if __name__ == "__main__":
data = load_data('./data/datasets_14701_19663_CC GENERAL.csv')
k = 3
X = np.array([f[:-1] for f in data])
y = np.array([f[-1] for f in data])
clusters,centroids = k_means(X,3)
show_clusters_centroids(clusters,centroids,"Result", keep=True)
plt.show()