-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algo2.py
64 lines (57 loc) · 2.09 KB
/
Algo2.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
"""
The implemention of Algorithm2
"""
import os
import numpy as np
import math
class Algo2(object):
def __init__(self):
print("use Algo2")
# read and combine the csv file in `data_path`
def _get_original_data(self, data_path):
original_data = np.genfromtxt(data_path, delimiter=',')
print("get original data")
return original_data
# return the ultimate data
def get_data(self, data_path):
return self._get_original_data(data_path)
# for Algo2, calculate the distance should consider the theta
def calculate_distance(self, pointA, pointB):
diff_theta = pointA[0] - pointB[0]
diff_r = pointA[1] - pointB[1]
# the distance should be in (0,pi)
if abs(diff_theta) > math.pi:
diff_theta = 2 * math.pi - abs(diff_theta)
distance = math.sqrt(diff_theta ** 2 + diff_r ** 2)
return distance
# alg2 compute the mean value of the points
def calculate_mean_point(self, points):
cos_sum = float(0)
sin_sum = float(0)
r_sum = float(0)
mean_theta = float(0)
mean_r = float(0)
N = len(points)
for point in points:
# [theta, r]
cos_sum += math.cos(point[0])
sin_sum += math.sin(point[0])
r_sum += point[1]
# the return of `atan2` is [-pi, pi], if the return < 0
# plus 2pi to the positive
mean_theta = math.atan2(sin_sum, cos_sum)
if mean_theta < 0 :
mean_theta += 2 * math.pi
mean_r = r_sum / N
mean_point = [mean_theta, mean_r]
# compute the variance
# use the way of calculating distance in algorithm 2
var_sum = float(0)
variance = float(0)
for point in points:
var_sum += (self.calculate_distance(point, mean_point)) ** 2
print("N=", N)
variance = var_sum / (N - 1)
# return the table of mean and variance of the cluster
# the table key is the index of the cluster, the value is corresponding value
return mean_point, variance