forked from qobi/ece57000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedoid_classifier.py
61 lines (51 loc) · 1.67 KB
/
medoid_classifier.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
from distances import *
import random
def random_labels(points, k):
return [random.randint(0, k-1) for point in points]
def points_with_label(label, points, labels):
result = []
for i in range(0, len(points)):
if labels[i]==label:
result.append(points[i])
return result
def medoid(distance):
def internal(points):
return point_with_minimum_average_distance(distance)(points, points)
return internal
def train(distance, points, labels):
k = 0
for i in range(0, len(points)):
if labels[i]>k:
k = labels[i]+1
return [medoid(distance)(points_with_label(j, points, labels))
for j in range(0, k)]
infinity = float("inf")
def classify(point, distance, medoids):
best_distance = infinity
best_label = -1
for j in range(0, len(medoids)):
d = distance(point, medoids[j])
if d<best_distance:
best_distance = d
best_label = j
return best_label
def reclassify_all(distance, points, medoids):
return [classify(point, distance, medoids) for point in points]
def cost(distance, points, labels, medoids):
return reduce(plus,
[reduce(plus,
[distance(point, medoids[j])
for point in points_with_label(j, points, labels)],
0)
for j in range(0, len(medoids))],
0)
def all_labeled(labels):
for label in labels:
if label==-1:
return False
return True
def all_labels(labels, k):
for j in range(0, k):
if len(points_with_label(j, labels, labels))==0:
return False
return True