-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclustering_algorithms.py
198 lines (177 loc) · 7.17 KB
/
clustering_algorithms.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
import sys
import random
import math
from ast import literal_eval
import time
# DMindYR
xk,yk=0,0
# Kmeans Implementation
# Input : Array List (vectors), K value (kVal)
# Output : Clustered vector (clusteredVectors)
def kmeans(vectors: list, kVal:int) -> list:
#choose k initial random unequal points
start = time.time()
centerPoints = []
while len(centerPoints) != kVal:
candidate = random.choice(random.choice(vectors))
if candidate not in centerPoints:
centerPoints.append(candidate)
#clustering
testPrev = []
iterationCounter = 0
while True:
clusteringTable = []
for y in range(len(vectors)):
clusteringTable.append([])
for x in range(len(vectors[0])):
temp = []
temp.append(vectors[y][x])
for k in range(kVal):
#distance from center point
distance = 0
for l in range(len(centerPoints[0])):
distance = distance + abs(centerPoints[k][l]-vectors[y][x][l])
temp.append(distance)
#assign cluster centroid with min distance
temp.append(temp[1:].index(min(temp[1:])))
clusteringTable[y].append(temp)
#check if cluster values changed, exit otherwise
test = []
for k in range(kVal):
test.append(0)
for itemY in clusteringTable:
for itemX in itemY:
if itemX[-1] == k:
test[k] = test[k] + 1
if testPrev == test:
break
testPrev = test
#update centroids
for k in range(kVal):
n = 0
vectorTemps = [0]*len(clusteringTable[0][0][0])
for itemY in clusteringTable:
for itemX in itemY:
if itemX[-1] == k:
for valCounter in range(len(itemX[0])):
vectorTemps[valCounter] = vectorTemps[valCounter] + itemX[0][valCounter]
n = n + 1
#check for 0 division
for valCounter in range(len(vectorTemps)):
if vectorTemps[valCounter] != 0:
vectorTemps[valCounter] = vectorTemps[valCounter]/n
centerPoints[k] = vectorTemps
iterationCounter = iterationCounter + 1
#Build clustered array
clusteredVectors = []
for y in range(len(clusteringTable)):
clusteredVectors.append([])
for x in range(len(clusteringTable[0])):
clusteredVectors[y].append(centerPoints[clusteringTable[y][x][-1]])
end = time.time()
#Log info
with open('logger.txt', 'a+') as out:
out.write(f"""**K-MEANS RESULTS**\n""")
out.write(f"""Array size: {len(vectors)}x{len(vectors[0])}x{len(vectors[0][0])}\n""")
out.write(f"""Parameters: kVal: {kVal}\n""")
out.write(f"""Centroid averages: {str(centerPoints)}\n""")
out.write(f"""Time taken: {round(end - start)} seconds\n""")
return clusteredVectors
#DBScan Implementation
#Distance functions
def EuclideanDistance(P,Q):
intermediateValues = []
for i in range(len(P[2])):
intermediateValues.append(math.pow(Q[2][i]-P[2][i],2))
return math.sqrt(sum(intermediateValues))
"""
#If using this then correct dbscan and FindNeighbours to have a param to differentiate the distance methods.
def MaximumDistance(P,Q):
intermediateValues = []
for i in range(len(P[2])):
intermediateValues.append(abs(Q[2][i]-P[2][i]))
return max(intermediateValues)
"""
mp = {}
#Finds all neighbor points for a chosen point
def FindNeighbours(Point, Points, eps):
tempNeighbours = []
for y in range(len(Points)):
for x in range(len(Points[0])):
if EuclideanDistance(Point, Points[y][x]) <= eps:
tempNeighbours.append(Points[y][x])
#Note: use Max Distance if required
return tempNeighbours
#reads vector array, performs dbscan and outputs vector array
def dbscan(vectors: list, minpts: int, epsilon: int) -> list:
#Initialization
start = time.time()
mp = {}
pointsArray = []
for y in range(len(vectors)):
pointsArray.append([])
for x in range(len(vectors[0])):
pointsArray[y].append([y,x,vectors[y][x],"Undefined"])
#DBSCAN clustering
clusterCounter = 0
for y in range(len(vectors)):
for x in range(len(vectors[0])):
if pointsArray[y][x][-1] != "Undefined":
continue
xk,yk=x,y
if (xk,yk) in mp.keys():
Neighbours=mp[(yk,xk)]
else:
Neighbours = FindNeighbours(pointsArray[y][x], pointsArray, epsilon)
mp[(yk,xk)]=Neighbours
if len(Neighbours) < minpts:
pointsArray[y][x][-1] = "Noise"
continue
clusterCounter = clusterCounter + 1
pointsArray[y][x][-1] = str(clusterCounter)
if pointsArray[y][x] in Neighbours:
Neighbours.remove(pointsArray[y][x])
for innerPoint in Neighbours:
if innerPoint[-1] == "Noise":
pointsArray[innerPoint[0]][innerPoint[1]][-1] = str(clusterCounter)
if innerPoint[-1] != "Undefined":
continue
pointsArray[innerPoint[0]][innerPoint[1]][-1] = str(clusterCounter)
#Get distinct clusters
clusterNumbers = []
for y in range(len(vectors)):
for x in range(len(vectors[0])):
if pointsArray[y][x][-1] not in clusterNumbers:
clusterNumbers.append(pointsArray[y][x][-1])
#Map cluster's averages
averagesForClusters = []
for item in clusterNumbers:
n = 0
vectorTemps = [0]*len(pointsArray[0][0][2])
for y in range(len(vectors)):
for x in range(len(vectors[0])):
if pointsArray[y][x][-1] == item:
for i in range(len(pointsArray[y][x][2])):
vectorTemps[i] = vectorTemps[i] + pointsArray[y][x][2][i]
n = n + 1
#Check Zero division
for i in range(len(vectorTemps)):
if vectorTemps[i] != 0:
vectorTemps[i] = vectorTemps[i]/n
averagesForClusters.append(vectorTemps)
#Build clustered array and change cluster averages with initial values
clusteredVectors = []
for y in range(len(pointsArray)):
clusteredVectors.append([])
for x in range(len(pointsArray[0])):
clusteredVectors[y].append(averagesForClusters[clusterNumbers.index(pointsArray[y][x][-1])])
end = time.time()
#Log info
with open('logger.txt', 'a+') as out:
out.write(f"""**DBSCAN RESULTS**\n""")
out.write(f"""Array size: {len(vectors)}x{len(vectors[0])}x{len(vectors[0][0])}\n""")
out.write(f"""Parameters: Eps: {epsilon}, minPts: {minpts}\n""")
out.write(f"""Number of identified clusters: {len(clusterNumbers)}\n""")
out.write(f"""Cluster averages: {str(averagesForClusters)}\n""")
out.write(f"""Time taken: {round(end - start)} seconds\n""")
return clusteredVectors