-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKBRAIN.py
More file actions
288 lines (219 loc) · 9.38 KB
/
KBRAIN.py
File metadata and controls
288 lines (219 loc) · 9.38 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
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
import math
from random import randint
import numpy as np
import pandas as pd
from sklearn import datasets
import matplotlib.pyplot as plt
# ***************************************************************
# Function: run_kbrain
# Variables/input: int: k number of clusters to find
# string: algorithm to perform
# objects.dataset: dataset to work on
# Output: pandas dataframe: cluster assignments
# Usage/Purpose: Function calls k-means or k-medoid and
# returns the cluster assignment for each
# datapoint.
# ***************************************************************
def run_kbrain(k, algorithm, data):
return_df = pd.DataFrame()
# if data == None:
# data = generate_random_dataset()
initial_centers = generate_random_centers(k, data.df)
label, centers = generate_clusters(k, data, initial_centers, algorithm)
# autoplot(k, data.df, centers, label, algorithm)
return_df["clusters"] = label
return pd.DataFrame(label, columns=["cluster"])
# ***************************************************************
# Function: autoplot
# Variables/input: int: k number of clusters to find
# pandas dataframe: data to plot
# float: coordinates of cluster centers
# labels
# Output: generates plot of data clusterings
# Usage/Purpose: Function generates plots of data clusterings
# ***************************************************************
def autoplot(k, df, centers, labels, alg):
# NOTE: Only works for two dimensions currently
for n in range(0, k):
plt.scatter(df.x1[labels == n], df.x2[labels == n])
plt.scatter(centers[n][0], centers[n][1], c="k")
title = alg + " with K = " + str(k)
plt.title(title)
plt.show()
plt.clf()
# ***************************************************************
# Function: euclidean_distance
# Variables/input: list[float]: x, y coordinates of point 1
# list[float]: x, y coordinates of point 2
# Output: float: distance between points
# Usage/Purpose: Function calculates the euclidean distance
# between two points.
# ***************************************************************
def euclidean_distance(pointA, pointB):
# Function returns the euclidean distance between two points
return_dist = 0
# Dynamically calculate the euclidean distance for all dimensions
for n in range(0, len(pointA)):
return_dist += (pointA[n] - pointB[n]) ** 2
# Return the distance
return math.sqrt(return_dist)
# ***************************************************************
# Function: kmean
# Variables/input: int: k number of clusters to find
# list[float]: cluster points
# labels
# Output: list[float]: centroids
# Usage/Purpose: Function calculates centroids.
# ***************************************************************
def kmean(k, clusters, labels):
# Function returns new centroids based on the means of the clusters
return_centroids = []
# For cluster n of k...
for n in range(0, k):
# Calculate the mean
temp = np.mean(clusters.df[labels == n])
# Append the new centroid to the return array
return_centroids.append(temp.values)
# Return the new centroids
return return_centroids
# ***************************************************************
# Function: kmedoid
# Variables/input: int: k number of clusters to find
# list[float]: cluster points
# medoids
# labels
# Output: list[float]: centroids
# Usage/Purpose: Function calculates centroids.
# ***************************************************************
def kmedoid(k, clusters, medoids, labels):
# Function returns new medoids by calculating which point of the cluster
# has the lowest entropy
return_medoids = []
# Generate arrays for data
init = np.empty(k)
distances = np.ones(k) * np.inf
# For cluster n of k...
for n in range(0, k):
length = len(clusters.df[labels == n])
# Iterate through every point of each cluster
i = length - 1
# Save the cluster once, use it a lot
cluster = clusters.df[labels == n]
while i >= 0:
temp = 0
# For point m in cluster n
for m in range(0, length):
# Calculate the total distance from all points to the
# prospective medoid
temp += clusters.distanceArray[cluster.index[m], cluster.index[i]]
# If the prospective medoid has lower entropy than the current
if temp < distances[n]:
# Replace the old medoid with the new medoid and save the distance
distances[n] = temp
init[n] = i
i -= 1
# Append the new medoid to the return array
return_medoids.append(cluster.iloc[int(init[n])].values)
# Return the new medoids
return return_medoids
# ***************************************************************
# Function: generate_random_dataset
# Variables/input: none
# Output: pandas dataframe: dataset points
# Usage/Purpose: Function generates a random dataset.
# ***************************************************************
def generate_random_dataset():
# Function returns a dataframe with X/Y pairs and a column for cluster labels
return_df = pd.DataFrame(columns=["x1", "x2"])
# Generate a blob set to my liking for now
X, y = datasets.make_blobs(n_samples=50, n_features=4, center_box=(-3.0, 3.0))
# Convert the blobs into two arrays aka X and Y coords
A = np.append(X[:, 0], X[:, 2])
B = np.append(X[:, 1], X[:, 3])
# Save the X and Y coords in the return Dataframe
return_df.x1 = A
return_df.x2 = B
# Return the dataset
return return_df
# ***************************************************************
# Function: generate_random_centers
# Variables/input: int: k number of clusters to find
# pandas dataframe: dataset
# Output: list[float]: centers of clusters
# Usage/Purpose: Function generates a random center.
# ***************************************************************
def generate_random_centers(k, df):
# Function returns random centers to begin clustering
return_centers = []
init = []
# Save the number of points once, use it k times
numPoints = len(df) - 1
# For each center generation n...
for n in range(0, k):
# Roll for an index from the data
rnd = randint(0, numPoints)
# If the index has already been chosen, reroll
while rnd in init:
rnd = randint(0, numPoints)
# Append the new index to the init array
init.append(rnd)
# Append the new center to the return array
return_centers.append(df.iloc[init[n]].values)
# Return the centers
return return_centers
# ***************************************************************
# Function: generate_clusters
# Variables/input: int: k number of clusters to find
# list[float]: points
# list[float]: centers
# string: algorithm name
# Output: pandas dataframe: cluster assignments
# cluster centers
# Usage/Purpose: Function generates clusters according to
# k-means or k-medoid algorithm.
# ***************************************************************
def generate_clusters(k, points, centers, algorithm):
# Function returns a list of cluster labels
length = len(points.df)
return_labels = np.empty(length)
# Base cases
if k == 0:
print("I don't know what you expected...")
elif k == 1:
return_labels = np.zeros(length)
elif k == length:
return_labels = np.arange(0, length)
# The meat of it
else:
while 1:
# For point n...
for n in range(0, length):
# Reset the current distance
currentDist = np.inf
# For cluster m...
for m in range(0, k):
# Calculate the euclidean distance between point n
# and center m
newDist = euclidean_distance(points.df.iloc[n], centers[m])
# If the new distance is less than the current distance...
if newDist < currentDist:
# Save the new distance
currentDist = newDist
# Set the cluster label as m for point n
return_labels[n] = m
# Algorithm pseudo-switch statement
if algorithm == "k-means":
new_centers = kmean(k, points, return_labels)
elif algorithm == "k-medoids":
new_centers = kmedoid(k, points, centers, return_labels)
else:
print("Error: invalid algorithm")
return None
# Check if the new centers are the same as the old centers
if np.array_equal(new_centers, centers):
# If so, break
break
else:
centers = new_centers
# Return the cluster labels and the final centers
return return_labels, centers