. python (scipy), , , , python, C/fortran.
>>> import numpy as np
>>> from scipy.spatial.distance import cdist
>>> a = np.array([[9, 2]])
>>> others = np.array([[0, 0], [10, 0], [10, 10]])
>>> def closest_point(pt, others):
... distances = cdist(pt, others)
... return others[distances.argmin()]
...
>>> closest_point(a, others)
array([10, 0])
:
>>> distances = cdist(a, others)
>>> distances
array([[ 9.21954446, 2.23606798, 8.06225775]])
>>> distances.mean()
6.5059567276970753
, , " ", , , :
>>> all_nodes = np.array([[0,0], [10,0], [0,10], [10, 10], [5,4]])
>>> from scipy.spatial.distance import pdist, squareform
>>> avg_dists = squareform(pdist(all_nodes)).mean(axis=1)
>>> avg_dists
array([ 8.10905197, 8.10905197, 8.39047706, 8.39047706, 5.68534957])
>>> all_nodes[avg_dists.argmin()]
array([5, 4])