-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexamples.py
78 lines (68 loc) · 2.17 KB
/
examples.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 17:54:54 2016-10-21
@author: heshenghuan ([email protected])
http://github.com/heshenghuan
"""
import kdtree
import knn
import Distance as ds
import math
def example_kdtree():
# An example of how to use kdtree
print "*" * 60
print "*" * 15, "An Example of kdtree's Usage", "*" * 15
print "*" * 60
point = [(2, 3), (5, 4), (9, 6), (4, 7), (8, 1), (7, 2), (8, 8)]
point1 = []
for i in point:
point1.append({1: i[0], 2: i[1]})
print "point list"
print point1
# Create a kdtree
root = kdtree.create(point1, dimensions=2)
# Visualize the kdtree
print "visualize the kd-tree: "
kdtree.visualize(root)
# Search for k-nearsest neighbor by given p-Minkowski distance
f = ds.EuclideanDistance
ans = root.search_knn(point={1: 7, 2: 3}, k=10, dist=f)
print "The 3 nearest nodes to point (7, 3) are:"
print ans
print "The nearest node to the point is:"
print ans[0][0].data
def example_knn():
# An example of how to use knn
print "*" * 60
print "*" * 16, "An Example of knn's Usage", "*" * 17
print "*" * 60
data1 = [(3, 5), (2, 3), (5, 4), (9, 6), (4, 7), (8, 1), (7, 2), (8, 8)]
data = []
for i in data1:
data.append({0: i[0], 1: i[1]})
label = [1, 1, 1, 0, 1, 0, 1, 0]
m = knn.KNN(data, label, dimensions=2)
print "Samples:", m.train_data
print "\nLabel prb:", m.class_prb
# print m.decision()
print "\n\nvisualize the kd-tree: "
m.visualize_kdtree()
f = ds.EuclideanDistance
print "the label of point", {0: 9, 1: 9}, "is",
print m.classify(point={0: 9, 1: 9}, k=3, dist=f, prbout=1)
print "the label of point", {0: 2, 1: 8}, "is",
print m.classify(point={0: 2, 1: 8}, k=3, dist=f, prbout=1)
knn.saveknn(m, 'testknn.pkl')
# Pickle test
print "*" * 60
print "Load knn model from file: 'testknn.pkl'"
n = knn.loadknn('testknn.pkl')
print "Samples:", n.train_data
print "\nLabel prb:", n.class_prb
# print n.decision()
print "\n\nvisualize the kd-tree: "
n.visualize_kdtree()
if __name__ == "__main__":
example_kdtree()
example_knn()