-
Notifications
You must be signed in to change notification settings - Fork 0
/
a5.py
85 lines (74 loc) · 2.07 KB
/
a5.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
from math import sqrt
# Inputs
classA = [[1, 1], [1, 2]]
classB = [[2, 1], [3, 1]]
# classA = [[1, 1], [1, 2], [2, 2]]
# classB = [[8, 8], [9, 9]]
# Minimum distance
def dMin(classA, classB):
min = 9999
lenA = len(classA)
lenB = len(classB)
for i in range(lenA):
for j in range(lenB):
calc = 0
for m in range(2):
calc += (classB[j][m] - classA[i][m]) ** 2
calc = sqrt(calc)
if calc <= min:
min = calc
return min
# Maximum distance
def dMax(classA, classB):
max = -9999
lenA = len(classA)
lenB = len(classB)
for i in range(lenA):
for j in range(lenB):
calc = 0
for m in range(2):
calc += (classB[j][m] - classA[i][m]) ** 2
calc = sqrt(calc)
if calc >= max:
max = calc
return max
# Average distance
def dAvg(classA, classB):
avg = 0
lenA = len(classA)
lenB = len(classB)
for i in range(lenA):
for j in range(lenB):
calc = 0
for m in range(2):
calc += (classB[j][m] - classA[i][m]) ** 2
calc = sqrt(calc)
avg += calc
avg = avg / (lenA * lenB)
return avg
# Mean distance
def dMean(classA, classB):
mean = 0
lenA = len(classA)
lenB = len(classB)
xMean1 = 0
yMean1 = 0
xMean2 = 0
yMean2 = 0
for i in range(lenA):
xMean1 += classA[i][0]
yMean1 += classA[i][1]
xMean1 = xMean1 / lenA
yMean1 = yMean1 / lenA
for i in range(lenB):
xMean2 += classB[i][0]
yMean2 += classB[i][1]
xMean2 = xMean2 / lenB
yMean2 = yMean2 / lenB
mean = sqrt((xMean2 - xMean1) ** 2 + (yMean2 - yMean1) ** 2)
return mean
# Print/function calls
print("dMin: {:.2f}".format(dMin(classA, classB)))
print("dMax: {:.2f}".format(dMax(classA, classB)))
print("dAvg: {:.2f}".format(dAvg(classA, classB)))
print("dMean: {:.2f}".format(dMean(classA, classB)))