-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest.py
86 lines (77 loc) · 2.56 KB
/
test.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
86
# Copyright (c) 2017 Manzil Zaheer All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time
import numpy as np
import scipy as sc
from covertree import CoverTree
from sklearn.neighbors import NearestNeighbors
gt = time.time
np.random.seed(seed=3)
print('Building cover tree')
x = np.random.rand(500000,128)
with open('train_data.bin', 'wb') as f:
np.array(x.shape, dtype='int32').tofile(f)
x.tofile(f)
print(x[0,0], x[0,1], x[1,0])
mx = np.mean(x,0)
dists = np.array([np.sqrt(np.dot(xv-mx,xv-mx)) for xv in x])
idx = np.argsort(-dists)
xs = x[idx]
#print sc.spatial.distance.squareform(sc.spatial.distance.pdist(x, 'euclidean'))
t = gt()
ct = CoverTree.from_matrix(x)
b_t = gt() - t
#ct.display()
print("Building time:", b_t, "seconds")
print("Test covering: ", ct.test_covering())
print('Generate random points')
y = np.random.rand(5000,128)
with open('test_data.bin', 'wb') as f:
np.array(y.shape, dtype='int32').tofile(f)
y.tofile(f)
print('Test Nearest Neighbour: ')
t = gt()
a = ct.NearestNeighbour(y)
b_t = gt() - t
print("Query time:", b_t, "seconds")
nbrs = NearestNeighbors(n_neighbors=1, algorithm='brute').fit(xs)
distances, indices = nbrs.kneighbors(y)
b = np.squeeze(xs[indices])
if np.all(a==b):
print("Test for Nearest Neighbour passed")
else:
print("Test for Nearest Neighbour failed")
print('Test k-Nearest Neighbours (k=2): ')
t = gt()
a = ct.kNearestNeighbours(y,2)
b_t = gt() - t
print("Query time:", b_t, "seconds")
nbrs = NearestNeighbors(n_neighbors=2, algorithm='brute').fit(xs)
distances, indices = nbrs.kneighbors(y)
if np.all(a==xs[indices]):
print("Test for k-Nearest Neighbours passed")
else:
print("Test for k-Nearest Neighbours failed")
print('Test delete: ')
x2 = np.vstack((xs[:indices[0,0]], xs[indices[0,0]+1:]))
dels = ct.remove(xs[indices[0,0]])
print('Point deleted: ', dels)
a = ct.NearestNeighbour(y)
nbrs = NearestNeighbors(n_neighbors=1, algorithm='brute').fit(x2)
distances, indices = nbrs.kneighbors(y)
b = np.squeeze(x2[indices])
if np.all(a==b):
print("Test for delete passed")
else:
print("Test for delete failed")