bundles / scipy 1.17.1 / scipy / spatial / _kdtree / KDTree / sparse_distance_matrix
function
scipy.spatial._kdtree:KDTree.sparse_distance_matrix
source: /scipy/spatial/_kdtree.py :810
Signature
def sparse_distance_matrix ( self , other , max_distance , p = 2.0 , output_type = dok_matrix ) Summary
Compute a sparse distance matrix.
Extended Summary
Computes a distance matrix between two KDTrees, leaving as zero any distance greater than max_distance.
Parameters
other: KDTreemax_distance: positive floatp: float, 1<=p<=infinityWhich Minkowski p-norm to use. A finite large p may cause a ValueError if overflow can occur.
output_type: string, optionalWhich container to use for output data. Options: 'dok_matrix', 'coo_matrix', 'dict', or 'ndarray'. Default: 'dok_matrix'.
Returns
result: dok_matrix, coo_matrix, dict or ndarraySparse matrix representing the results in "dictionary of keys" format. If a dict is returned the keys are (i,j) tuples of indices. If output_type is 'ndarray' a record array with fields 'i', 'j', and 'v' is returned,
Examples
You can compute a sparse distance matrix between two kd-trees:import numpy as np from scipy.spatial import KDTree rng = np.random.default_rng() points1 = rng.random((5, 2)) points2 = rng.random((5, 2)) kd_tree1 = KDTree(points1) kd_tree2 = KDTree(points2) sdm = kd_tree1.sparse_distance_matrix(kd_tree2, 0.3)✓
sdm.toarray()
✗from scipy.spatial import distance_matrix
✓distance_matrix(points1, points2)
✗Aliases
-
scipy.spatial.KDTree.sparse_distance_matrix