-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
294 lines (216 loc) · 7.63 KB
/
main.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans, AffinityPropagation, Birch
from sklearn.datasets import load_iris, load_digits, load_breast_cancer, load_wine
from sklearn.mixture import GaussianMixture
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import PCA
plt.style.use('seaborn-whitegrid')
# Preprocessing functions:
def preprocess_example_data(path):
"""Small function to preprocess example data
There is probably an easier way
Parameters:
_______
path: str
link to txt
Return:
_______
data: numpy array
data in form [[x,y], ... ,[x,y]]
"""
# Extract data from txt file
with open(path, "r") as lines:
raw_data = lines.readlines()
x = []
y = []
for element in raw_data:
element = element.split(" ")
element = [x for x in element if x] # remove whitespaces
x.append(float(element[0]))
y.append(float(element[1].rstrip("\n")))
# Turn data into numpy array
data = []
for i in range(len(x)):
data.append([x[i], y[i]])
data = np.array(data)
return data
# Clustering Functions
def kmeans(data, amount_clusters):
"""K Means algorithm implementation
Parameters
______
data: numpy array
array of datapoints
clusters: int
number of clusters we want to create
Return
______
centers: numpy array
cluster centers
labels: numpy array
array that assigns each datapoint to a cluster"""
# Fit data to kmeans model class
kmeans = KMeans(n_clusters=amount_clusters, random_state=0).fit(data)
# retrieve cluster centers and class labels to data
centers = kmeans.cluster_centers_
labels = kmeans.labels_
# return back to API
return centers, labels
def affinity(data):
"""Affinity Propagation implementation. This one does not require the amount of clusters
Parameters
______
data: numpy array
array of datapoints
Return
______
centers: numpy array
cluster centers
labels: numpy array
array that assigns each datapoint to a cluster"""
# Initiate AfProp
afprop = AffinityPropagation(random_state=5).fit(data)
centers = afprop.cluster_centers_
# predict clusters for data
labels = afprop.predict(data)
# return back to API
return centers, labels
def gaussian_mixture_model(data, amount_clusters):
""" Gaussian mixture model implementation. Requires the amount of clusters.
Parameters
______
data: numpy array
array of datapoints
Return
______
gmm: gaussian mixture object
contains information about gaussian distributions in dataset
labels: numpy array
array that assigns each datapoint to a cluster"""
# Create gaussian mixture model
gmm = GaussianMixture(n_components=amount_clusters).fit(data)
# predict clusters for data
labels = gmm.predict(data)
return data, False, labels
def birch(data, amount_clusters):
"""Birch implementation.
amount_clusters: int
number of clusters we want to create
Return
______
centers: numpy array
cluster centers
labels: numpy array
array that assigns each datapoint to a cluster"""
# Initiate BIRCH
birch_fit = Birch(threshold=0.01, n_clusters=amount_clusters).fit(data)
centers = birch_fit.subcluster_centers_
# predict clusters for data
labels = birch_fit.predict(data)
return centers, labels
def plotting(data, labels):
"""Central plotting function that takes in all the relevant data to plot something
Parameters:
______
data: numpy array
original datapoints
centers: numpy array
computed cluster centers
labeles: numpy array
computed classification to clusters"""
# Ploting the data
plt.scatter(data[:, 0], data[:, 1], c=labels,
s=50, cmap='prism')
#plt.scatter(centers[:, 0], centers[:, 1], marker="+", color='blue')
plt.show()
def centralAPI(algorithm, dataset, amount_clusters):
"""Central API that controls which algorithm we want to use and how we wish to configure them
Parameters
______
algorithm: str
name of the cluster algorithm
dataset: str
name of dataset
amount_clusters: int
amount of clusters that will be used e.g for k-means"""
# Select and load dataset
if dataset == "example":
datapath = "./example_data.txt"
data = preprocess_example_data(datapath)
elif dataset == "IRIS":
data_obj = load_iris()
elif dataset == "Digits":
data_obj = load_digits()
elif dataset == "Breast Cancer":
data_obj = load_breast_cancer()
elif dataset == "Wine":
data_obj = load_wine()
else:
print("Invalid input!")
return
data = data_obj.data
target_labels = data_obj.target
# Select Algorithm
if algorithm == "K-Means":
centers, labels = kmeans(data, amount_clusters=amount_clusters)
elif algorithm == "Affinity Propagation":
centers, labels = affinity(data)
elif algorithm == "Gaussian mixture model":
data, gmm, labels = gaussian_mixture_model(data, amount_clusters=amount_clusters)
elif algorithm == "BIRCH":
centers, labels = birch(data, amount_clusters=amount_clusters)
else:
print("Invalid Input!")
return
# TODO: Guckt dass eure Algorithmen immer "centers" und "labels" returnen
# Calculate purity before plotting
pur_val = purity(labels, target_labels)
# Plot the data
# Use PCA to enable plotting high dimensional data in 2d
pca = PCA(n_components=2)
data = pca.fit_transform(data)
# One plot with calculated labels and one with true labels to compare
plotting(data, labels)
#plotting(data, centers, target_labels)
return data, labels, pur_val, target_labels
def purity(labels, targets):
""""Central function that calculates the external validation factor, done with "Purity"
Parameters
______
labels:
predicted labels calculated by k-means
targets:
target labels provided by the dataset
"""
# calculate amount of clusters
amount = len(set(labels))
# Calculate confusion Matrix which shows which points are in each cluster
# (predicted and should be)
# normalizing over all clusters, therefore we do not need to multiply with 1/N
# mat_norm is a matrix with i-th row = true label and j-th column = predicted label
mat_norm = confusion_matrix(targets, labels, normalize='all')
# Calculate which predicted label matches to the true label
# e.g. predicted label 1 is true label 9 if [_,9,_,...]
mapping_norm = np.array([np.argmax(mat_norm[:, i]) for i in range(amount)])
# Calculate Purity
purity_value = 0
for i in range(amount):
# mapping_norm[i] gives true label and i gives what was predicted
purity_value += mat_norm[mapping_norm[i],i]
print("Purity is: ", purity_value)
return purity_value
# Choose from "example", "iris", beast_cancer
datasets = ["IRIS", "Wine", "Digits", "Breast_cancer"]
nr_clusters = [3,3,10,2]
clusters = 5
algorithms = ["K-Means", "Affinity Propagation", "Gaussian mixture model", "BIRCH"]
# For testinf purposes, frontend does not need this
#centralAPI(algorithm=algorithms[1], dataset=datasets[0], amount_clusters=3)
# Code for the plots we used in the paper
"""
for i in range(4):
for j in range(4):
print(algorithms[i], " ", datasets[j])
centralAPI(algorithm=algorithms[i], dataset=datasets[j], amount_clusters=nr_clusters[j])
"""