-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgo1.py
62 lines (52 loc) · 1.78 KB
/
Algo1.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
"""
The implemention of Algorithm1
"""
import os
import numpy as np
import math
class Algo1(object):
def __init__(self):
print("use Algo1")
# 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
# convert the data to the (theta,r)-->(x,y)
def _get_processed_data(self, original_data):
processed_data = []
for data in original_data:
theta = data[0]
r = data[1]
processed_data.append([r * math.cos(theta), r * math.sin(theta)])
print("get processed data")
return processed_data
# return the ultimate data
def get_data(self, data_path):
return self._get_processed_data(self._get_original_data(data_path))
# for Algo1, calculate the distance is compute the Euclidean distance
def calculate_distance(self, pointA, pointB):
res = float(0)
for i in range(len(pointA)):
diff = pointA[i] - pointB[i]
res += diff ** 2
return math.sqrt(res)
# calculate the mean and variance of the points in Euclidean space
def calculate_mean_point(self, points):
x_sum = float(0)
y_sum = float(0)
x_mean = float(0)
y_mean = float(0)
N = len(points)
for point in points:
x_sum += point[0]
y_sum += point[1]
x_mean = x_sum / N
y_mean = y_sum / N
mean_point = [x_mean, y_mean]
var_sum = float(0)
variance = float(0)
for point in points:
var_sum += (self.calculate_distance(point, mean_point)) ** 2
variance = var_sum / (N - 1)
return mean_point, variance