.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python_packages\scipy\demo_nearest_neighbors.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_packages_scipy_demo_nearest_neighbors.py: Nearest neighbors ================= .. GENERATED FROM PYTHON SOURCE LINES 5-9 .. code-block:: Python import numpy as np from scipy import spatial .. GENERATED FROM PYTHON SOURCE LINES 11-12 Make a grid of 0:9 in X and 0:20 in Y. .. GENERATED FROM PYTHON SOURCE LINES 12-14 .. code-block:: Python [X, Y] = np.meshgrid(range(0, 10), range(0, 20)) .. GENERATED FROM PYTHON SOURCE LINES 15-16 Make one XY array [10*20 x 2] where each line is the x, y position of one point. .. GENERATED FROM PYTHON SOURCE LINES 16-18 .. code-block:: Python XY = np.vstack([X.flatten(), Y.flatten()]).transpose() .. GENERATED FROM PYTHON SOURCE LINES 19-20 Make the fast search tree. .. GENERATED FROM PYTHON SOURCE LINES 20-22 .. code-block:: Python kdtree = spatial.cKDTree(XY, leafsize=4) .. GENERATED FROM PYTHON SOURCE LINES 23-24 Choose some random points in the grid and get the 8 nearest neighbours. .. GENERATED FROM PYTHON SOURCE LINES 24-35 .. code-block:: Python rand = np.random.randint(0, XY.shape[1], size=(20,)) for r in rand: # random point: point = XY[r, :] # query the 8 nearest neighbours with an euclidean distance [dists, indices] = kdtree.query(point, k=8, p=2) print("Nearest points to point:", point) print("------------------------------------------") for idx in range(0, len(indices)): print(" ", idx, ".:", XY[indices[idx], :], " -> dist:", dists[idx]) .. rst-class:: sphx-glr-script-out .. code-block:: none Nearest points to point: [1 0] ------------------------------------------ 0 .: [1 0] -> dist: 0.0 1 .: [0 0] -> dist: 1.0 2 .: [1 1] -> dist: 1.0 3 .: [2 0] -> dist: 1.0 4 .: [0 1] -> dist: 1.4142135623730951 5 .: [2 1] -> dist: 1.4142135623730951 6 .: [3 0] -> dist: 2.0 7 .: [1 2] -> dist: 2.0 Nearest points to point: [1 0] ------------------------------------------ 0 .: [1 0] -> dist: 0.0 1 .: [0 0] -> dist: 1.0 2 .: [1 1] -> dist: 1.0 3 .: [2 0] -> dist: 1.0 4 .: [0 1] -> dist: 1.4142135623730951 5 .: [2 1] -> dist: 1.4142135623730951 6 .: [3 0] -> dist: 2.0 7 .: [1 2] -> dist: 2.0 Nearest points to point: [0 0] ------------------------------------------ 0 .: [0 0] -> dist: 0.0 1 .: [1 0] -> dist: 1.0 2 .: [0 1] -> dist: 1.0 3 .: [1 1] -> dist: 1.4142135623730951 4 .: [2 0] -> dist: 2.0 5 .: [0 2] -> dist: 2.0 6 .: [1 2] -> dist: 2.23606797749979 7 .: [2 1] -> dist: 2.23606797749979 Nearest points to point: [1 0] ------------------------------------------ 0 .: [1 0] -> dist: 0.0 1 .: [0 0] -> dist: 1.0 2 .: [1 1] -> dist: 1.0 3 .: [2 0] -> dist: 1.0 4 .: [0 1] -> dist: 1.4142135623730951 5 .: [2 1] -> dist: 1.4142135623730951 6 .: [3 0] -> dist: 2.0 7 .: [1 2] -> dist: 2.0 Nearest points to point: [1 0] ------------------------------------------ 0 .: [1 0] -> dist: 0.0 1 .: [0 0] -> dist: 1.0 2 .: [1 1] -> dist: 1.0 3 .: [2 0] -> dist: 1.0 4 .: [0 1] -> dist: 1.4142135623730951 5 .: [2 1] -> dist: 1.4142135623730951 6 .: [3 0] -> dist: 2.0 7 .: [1 2] -> dist: 2.0 Nearest points to point: [1 0] ------------------------------------------ 0 .: [1 0] -> dist: 0.0 1 .: [0 0] -> dist: 1.0 2 .: [1 1] -> dist: 1.0 3 .: [2 0] -> dist: 1.0 4 .: [0 1] -> dist: 1.4142135623730951 5 .: [2 1] -> dist: 1.4142135623730951 6 .: [3 0] -> dist: 2.0 7 .: [1 2] -> dist: 2.0 Nearest points to point: [0 0] ------------------------------------------ 0 .: [0 0] -> dist: 0.0 1 .: [1 0] -> dist: 1.0 2 .: [0 1] -> dist: 1.0 3 .: [1 1] -> dist: 1.4142135623730951 4 .: [2 0] -> dist: 2.0 5 .: [0 2] -> dist: 2.0 6 .: [1 2] -> dist: 2.23606797749979 7 .: [2 1] -> dist: 2.23606797749979 Nearest points to point: [1 0] ------------------------------------------ 0 .: [1 0] -> dist: 0.0 1 .: [0 0] -> dist: 1.0 2 .: [1 1] -> dist: 1.0 3 .: [2 0] -> dist: 1.0 4 .: [0 1] -> dist: 1.4142135623730951 5 .: [2 1] -> dist: 1.4142135623730951 6 .: [3 0] -> dist: 2.0 7 .: [1 2] -> dist: 2.0 Nearest points to point: [0 0] ------------------------------------------ 0 .: [0 0] -> dist: 0.0 1 .: [1 0] -> dist: 1.0 2 .: [0 1] -> dist: 1.0 3 .: [1 1] -> dist: 1.4142135623730951 4 .: [2 0] -> dist: 2.0 5 .: [0 2] -> dist: 2.0 6 .: [1 2] -> dist: 2.23606797749979 7 .: [2 1] -> dist: 2.23606797749979 Nearest points to point: [1 0] ------------------------------------------ 0 .: [1 0] -> dist: 0.0 1 .: [0 0] -> dist: 1.0 2 .: [1 1] -> dist: 1.0 3 .: [2 0] -> dist: 1.0 4 .: [0 1] -> dist: 1.4142135623730951 5 .: [2 1] -> dist: 1.4142135623730951 6 .: [3 0] -> dist: 2.0 7 .: [1 2] -> dist: 2.0 Nearest points to point: [0 0] ------------------------------------------ 0 .: [0 0] -> dist: 0.0 1 .: [1 0] -> dist: 1.0 2 .: [0 1] -> dist: 1.0 3 .: [1 1] -> dist: 1.4142135623730951 4 .: [2 0] -> dist: 2.0 5 .: [0 2] -> dist: 2.0 6 .: [1 2] -> dist: 2.23606797749979 7 .: [2 1] -> dist: 2.23606797749979 Nearest points to point: [0 0] ------------------------------------------ 0 .: [0 0] -> dist: 0.0 1 .: [1 0] -> dist: 1.0 2 .: [0 1] -> dist: 1.0 3 .: [1 1] -> dist: 1.4142135623730951 4 .: [2 0] -> dist: 2.0 5 .: [0 2] -> dist: 2.0 6 .: [1 2] -> dist: 2.23606797749979 7 .: [2 1] -> dist: 2.23606797749979 Nearest points to point: [0 0] ------------------------------------------ 0 .: [0 0] -> dist: 0.0 1 .: [1 0] -> dist: 1.0 2 .: [0 1] -> dist: 1.0 3 .: [1 1] -> dist: 1.4142135623730951 4 .: [2 0] -> dist: 2.0 5 .: [0 2] -> dist: 2.0 6 .: [1 2] -> dist: 2.23606797749979 7 .: [2 1] -> dist: 2.23606797749979 Nearest points to point: [1 0] ------------------------------------------ 0 .: [1 0] -> dist: 0.0 1 .: [0 0] -> dist: 1.0 2 .: [1 1] -> dist: 1.0 3 .: [2 0] -> dist: 1.0 4 .: [0 1] -> dist: 1.4142135623730951 5 .: [2 1] -> dist: 1.4142135623730951 6 .: [3 0] -> dist: 2.0 7 .: [1 2] -> dist: 2.0 Nearest points to point: [1 0] ------------------------------------------ 0 .: [1 0] -> dist: 0.0 1 .: [0 0] -> dist: 1.0 2 .: [1 1] -> dist: 1.0 3 .: [2 0] -> dist: 1.0 4 .: [0 1] -> dist: 1.4142135623730951 5 .: [2 1] -> dist: 1.4142135623730951 6 .: [3 0] -> dist: 2.0 7 .: [1 2] -> dist: 2.0 Nearest points to point: [0 0] ------------------------------------------ 0 .: [0 0] -> dist: 0.0 1 .: [1 0] -> dist: 1.0 2 .: [0 1] -> dist: 1.0 3 .: [1 1] -> dist: 1.4142135623730951 4 .: [2 0] -> dist: 2.0 5 .: [0 2] -> dist: 2.0 6 .: [1 2] -> dist: 2.23606797749979 7 .: [2 1] -> dist: 2.23606797749979 Nearest points to point: [1 0] ------------------------------------------ 0 .: [1 0] -> dist: 0.0 1 .: [0 0] -> dist: 1.0 2 .: [1 1] -> dist: 1.0 3 .: [2 0] -> dist: 1.0 4 .: [0 1] -> dist: 1.4142135623730951 5 .: [2 1] -> dist: 1.4142135623730951 6 .: [3 0] -> dist: 2.0 7 .: [1 2] -> dist: 2.0 Nearest points to point: [1 0] ------------------------------------------ 0 .: [1 0] -> dist: 0.0 1 .: [0 0] -> dist: 1.0 2 .: [1 1] -> dist: 1.0 3 .: [2 0] -> dist: 1.0 4 .: [0 1] -> dist: 1.4142135623730951 5 .: [2 1] -> dist: 1.4142135623730951 6 .: [3 0] -> dist: 2.0 7 .: [1 2] -> dist: 2.0 Nearest points to point: [1 0] ------------------------------------------ 0 .: [1 0] -> dist: 0.0 1 .: [0 0] -> dist: 1.0 2 .: [1 1] -> dist: 1.0 3 .: [2 0] -> dist: 1.0 4 .: [0 1] -> dist: 1.4142135623730951 5 .: [2 1] -> dist: 1.4142135623730951 6 .: [3 0] -> dist: 2.0 7 .: [1 2] -> dist: 2.0 Nearest points to point: [1 0] ------------------------------------------ 0 .: [1 0] -> dist: 0.0 1 .: [0 0] -> dist: 1.0 2 .: [1 1] -> dist: 1.0 3 .: [2 0] -> dist: 1.0 4 .: [0 1] -> dist: 1.4142135623730951 5 .: [2 1] -> dist: 1.4142135623730951 6 .: [3 0] -> dist: 2.0 7 .: [1 2] -> dist: 2.0 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.011 seconds) .. _sphx_glr_download_11_demos_python_packages_scipy_demo_nearest_neighbors.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_nearest_neighbors.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_nearest_neighbors.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_nearest_neighbors.zip `