In [9]:
%matplotlib inline
# pairwise distances looks at the distances between each
# record and finds closest members of the matrix.

In [10]:
from sklearn.metrics import pairwise
from sklearn.datasets import make_blobs
import numpy as np

In [11]:
points, labels = make_blobs()

In [12]:
distances = pairwise.pairwise_distances(points)

In [13]:
np.diag(distances)[:5]


Out[13]:
array([ 0.,  0.,  0.,  0.,  0.])

In [14]:
ranks = np.argsort(distances[0])

In [15]:
ranks[:5]


Out[15]:
array([ 0, 10, 68, 62, 15])

In [26]:
print points.shape
points[ranks][:5]


(100, 2)
Out[26]:
array([[ 0.39130467, -5.19677887],
       [ 0.49318055, -5.52736458],
       [ 0.71912691, -4.70887537],
       [ 0.23485245, -4.60406434],
       [ 0.76108915, -4.69760094]])

In [17]:
import matplotlib.pyplot as plt

In [25]:
f,ax = plt.subplots(figsize=(7,5))
ax.scatter(points[ranks][0], points[ranks][1])
ax.legend(loc='best')



In [ ]: