本文簡要介紹 python 語言中 scipy.spatial.cKDTree.sparse_distance_matrix
的用法。
用法:
cKDTree.sparse_distance_matrix(self, other, max_distance, p=2.)#
計算稀疏距離矩陣
計算兩個 cKDTree 之間的距離矩陣,任何大於 max_distance 的距離都為零。
- other: cKD樹
- max_distance: 正浮點數
- p: 浮點數,1<=p<=無窮大
使用哪個 Minkowski p-norm。如果可能發生溢出,有限的大 p 可能會導致 ValueError。
- output_type: 字符串,可選
使用哪個容器來輸出數據。選項:‘dok_matrix’, ‘coo_matrix’、‘dict’ 或‘ndarray’。默認值:‘dok_matrix’。
- result: dok_matrix、coo_matrix、dict 或 ndarray
表示結果的稀疏矩陣,采用 “dictionary of keys” 格式。如果返回字典,則鍵是索引的 (i,j) 元組。如果output_type是‘ndarray’,包含字段‘i’, ‘j’的記錄數組,並且返回‘v’,
參數 ::
返回 ::
例子:
您可以計算兩個kd-trees 之間的稀疏距離矩陣:
>>> import numpy as np >>> from scipy.spatial import cKDTree >>> rng = np.random.default_rng() >>> points1 = rng.random((5, 2)) >>> points2 = rng.random((5, 2)) >>> kd_tree1 = cKDTree(points1) >>> kd_tree2 = cKDTree(points2) >>> sdm = kd_tree1.sparse_distance_matrix(kd_tree2, 0.3) >>> sdm.toarray() array([[0. , 0. , 0.12295571, 0. , 0. ], [0. , 0. , 0. , 0. , 0. ], [0.28942611, 0. , 0. , 0.2333084 , 0. ], [0. , 0. , 0. , 0. , 0. ], [0.24617575, 0.29571802, 0.26836782, 0. , 0. ]])
您可以檢查 max_distance 以上的距離是否為零:
>>> from scipy.spatial import distance_matrix >>> distance_matrix(points1, points2) array([[0.56906522, 0.39923701, 0.12295571, 0.8658745 , 0.79428925], [0.37327919, 0.7225693 , 0.87665969, 0.32580855, 0.75679479], [0.28942611, 0.30088013, 0.6395831 , 0.2333084 , 0.33630734], [0.31994999, 0.72658602, 0.71124834, 0.55396483, 0.90785663], [0.24617575, 0.29571802, 0.26836782, 0.57714465, 0.6473269 ]])
相關用法
- Python SciPy cKDTree.query_ball_tree用法及代碼示例
- Python SciPy cKDTree.query_ball_point用法及代碼示例
- Python SciPy cKDTree.query用法及代碼示例
- Python SciPy cKDTree.query_pairs用法及代碼示例
- Python SciPy cKDTree.count_neighbors用法及代碼示例
- Python SciPy csc_array.diagonal用法及代碼示例
- Python SciPy csgraph.csgraph_to_dense用法及代碼示例
- Python SciPy coo_array.tocsr用法及代碼示例
- Python SciPy csgraph.min_weight_full_bipartite_matching用法及代碼示例
- Python SciPy csc_matrix.nonzero用法及代碼示例
- Python SciPy contingency.relative_risk用法及代碼示例
- Python SciPy csgraph.minimum_spanning_tree用法及代碼示例
- Python SciPy constants.nu2lambda用法及代碼示例
- Python SciPy csr_array.diagonal用法及代碼示例
- Python SciPy csgraph.breadth_first_order用法及代碼示例
- Python SciPy csc_matrix.diagonal用法及代碼示例
- Python SciPy coo_matrix.tocsr用法及代碼示例
- Python SciPy coo_matrix.tocsc用法及代碼示例
- Python SciPy coo_array.diagonal用法及代碼示例
- Python SciPy constants.convert_temperature用法及代碼示例
- Python SciPy csr_matrix.nonzero用法及代碼示例
- Python SciPy csr_matrix.dot用法及代碼示例
- Python SciPy csgraph.connected_components用法及代碼示例
- Python SciPy csgraph.dijkstra用法及代碼示例
- Python SciPy coo_array.dot用法及代碼示例
注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.spatial.cKDTree.sparse_distance_matrix。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。