# libpysal.cg.distance_matrix¶

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

Calculate a distance matrix

XXX Needs optimization/integration with other weights in pysal

Parameters
Xnumpy.ndarray

An n by k array where n is the number of observations and k is the number of dimensions (2 for x,y).

pfloat

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

thresholdpositive integer

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

Returns
Dnumpy.ndarray

An n by m p-norm distance matrix.

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.        ]])