In [4]:
""" This is just a quick test, using the nearest neighbors functionality
that is built into scikit-learn.
"""
import numpy as np
from sklearn.neighbors import NearestNeighbors


""" SUPER SIMPLE TEST # 1
Taken from: 
http://scikit-learn.org/stable/modules/generated/sklearn.neighbors.NearestNeighbors.html
"""
# just a few data points
samples = [[0, 0, 2], [1, 0, 0], [0, 0, 1]]

""" NearestNeighbors is an unsupervised learner for implementing
nearest neighbor searches. Here we start off with:
default value for k_neighbors = 2
radius (range of parameter space to use by default) = 0.4
"""
neigh = NearestNeighbors(2, 0.4)
# fit the model using "samples" as the training data
neigh_desc = neigh.fit(samples)
# find the K-neighbors of the point (0, 0, 1.3), n neighbors = 2
neigh_dist = neigh.kneighbors([[0, 0, 1.3]], 2, return_distance=False)

# find the neighbors withing 0.4 of the given point
rng = neigh.radius_neighbors([0, 0, 1.3], 0.4, return_distance=False)

# displaying some results
print(neigh_desc)
print(neigh_dist)
print(np.asarray(rng[0][0]))
print(rng)


NearestNeighbors(algorithm='auto', leaf_size=30, metric='minkowski',
         metric_params=None, n_neighbors=2, p=2, radius=0.4)
[[2 0]]
2
[array([2])]

In [11]:
""" Test #2
I generate a bunch of points in (hopefully) three clusters in 3-space,
and try to group them.
"""
from random import randint


data = []
for _ in range(20):
    data.append([randint(1, 10), randint(1, 10), randint(1, 10)])
for _ in range(40):
    data.append([randint(-10, 0), randint(1, 10), randint(1, 10)])
for _ in range(60):
    data.append([randint(-10, 0), randint(-30, -20), randint(-30, -20)])

neigh = NearestNeighbors(5, 4)
# fit the model using "samples" as the training data
neigh_desc = neigh.fit(data)
# find the K-neighbors of the point (0, 0, 1.3), n neighbors = 5
neigh_dist = neigh.kneighbors([[0, 0, 1.3]], 5, return_distance=False)

# find the neighbors withing 4 of the given point
rng = neigh.radius_neighbors([0, 0, 1.3], 4, return_distance=False)

# displaying some results
print(neigh_desc)
print(neigh_dist)
#print(np.asarray(rng[0][0]))
print(rng)


NearestNeighbors(algorithm='auto', leaf_size=30, metric='minkowski',
         metric_params=None, n_neighbors=5, p=2, radius=4)
[[26 40 29 43 46]]
[array([26, 29, 40])]

In [ ]: