pysal.lib.cg.distance_matrix

pysal.lib.cg.distance_matrix(X, p=2.0, threshold=50000000.0)[source]

Distance Matrices

XXX Needs optimization/integration with other weights in pysal

Parameters:
X : An, n by k numpy.ndarray

Where n is number of observations k is number of dimmensions (2 for x,y)

p : float

Minkowski p-norm distance metric parameter: 1<=p<=infinity 2: Euclidean distance 1: Manhattan distance

threshold : positive integer

If (n**2)*32 > threshold use scipy.spatial.distance_matrix instead of working in ram, this is roughly the ammount of ram (in bytes) that will be used.

Examples

>>> x,y=[r.flatten() for r in np.indices((3,3))]
>>> data = np.array([x,y]).T
>>> d=distance_matrix(data)
>>> np.array(d)
array([[0.        , 1.        , 2.        , 1.        , 1.41421356,
        2.23606798, 2.        , 2.23606798, 2.82842712],
       [1.        , 0.        , 1.        , 1.41421356, 1.        ,
        1.41421356, 2.23606798, 2.        , 2.23606798],
       [2.        , 1.        , 0.        , 2.23606798, 1.41421356,
        1.        , 2.82842712, 2.23606798, 2.        ],
       [1.        , 1.41421356, 2.23606798, 0.        , 1.        ,
        2.        , 1.        , 1.41421356, 2.23606798],
       [1.41421356, 1.        , 1.41421356, 1.        , 0.        ,
        1.        , 1.41421356, 1.        , 1.41421356],
       [2.23606798, 1.41421356, 1.        , 2.        , 1.        ,
        0.        , 2.23606798, 1.41421356, 1.        ],
       [2.        , 2.23606798, 2.82842712, 1.        , 1.41421356,
        2.23606798, 0.        , 1.        , 2.        ],
       [2.23606798, 2.        , 2.23606798, 1.41421356, 1.        ,
        1.41421356, 1.        , 0.        , 1.        ],
       [2.82842712, 2.23606798, 2.        , 2.23606798, 1.41421356,
        1.        , 2.        , 1.        , 0.        ]])