-
Notifications
You must be signed in to change notification settings - Fork 0
/
curse.py
59 lines (48 loc) · 1.64 KB
/
curse.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
import numpy as np
import time
class dist_point:
def __init__(self, dist, index):
self.dist = dist
self.index = index
def random_array(dimensions):
return 2*np.random.rand(1000, dimensions) - 1
def distance_from_origin(vector):
b = np.zeros(vector.shape)
return np.linalg.norm(vector-b)
def fractional_distance(array):
count = 0
for row in array:
if distance_from_origin(row) < 1:
count += 1
return float(count)/(array.shape[0])
def knn_distance(matrix, query, knum):
dists = []
index = 0
for row in matrix:
dists.append( dist_point( distance( row, query) , index ) )
index += 1
dists = sorted( dists, key=lambda dist_point: dist_point.dist)
return dists[0].dist
def distance(a_point, b_point):
return np.linalg.norm(a_point-b_point)
def random_query_pts(dimensions):
return 2*np.random.rand(100, dimensions) - 1
def average_distance(rand_arr, rand_points):
distance = 0
for row in rand_points:
distance += knn_distance(rand_arr, row, 1)
return float(distance) / 100
if __name__ == "__main__":
start_time = time.time()
dimensions = 1
while dimensions < 16:
rand_arr = random_array( dimensions)
y = fractional_distance( rand_arr)
rand_pts = random_query_pts( dimensions)
avg_dist = average_distance( rand_arr, rand_pts)
print "Dimension - "+ str(dimensions)
print "Average fractional distance from origin - " + str(y)
print "Average distance from nearest neighbor - "+ str(avg_dist)
print ""
dimensions += 1
print time.time() - start_time, "seconds"