-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnearest_centroid.py
47 lines (42 loc) · 1.39 KB
/
nearest_centroid.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
#################################################################################
# Implementation of the Euclidean distance from a list of centroids and profiles
# - Emmanuel Romero (https://github.com/romeroqe)
#
# This file contains code used to develop the methodology
# of a publication that is currently under review.
#
import math, operator
###############################
# Calculate Euclidean distance
###############################
def distance(X, y, N):
distance = 0
for x in range(N):
# Euclidean distance
distance += (X[x] - y[x])**2
return math.sqrt(distance)
###############################
# Get nearest centroid
###############################
def getNearestCentroid(Xi, centroids):
N = len(Xi)
labels = list(range(len(centroids)))
distances = []
for key in labels:
distances.append((key, distance(Xi, centroids[key], N)))
distances.sort(key=operator.itemgetter(1))
return distances[0][0]
###############################
# Iterate profiles
###############################
def NearestCentroid(X, centroids):
#############################################################
# parameters:
# X: 2-D list, shape: [num_profiles, 1451 (depth)]
# centroids: 2-D list, shape: [num_clusters, 1451 (depth)]
predictions = []
for i in range(len(X)):
predictions.append(getNearestCentroid(X[i], centroids))
print(f"{i}/{len(X)}", end="\r")
print(f"{i}/{len(X)}")
return predictions