python scipy.spatial.distance 距离计算函数
作者:是梦吧,是你吧! 时间:2023-01-25 08:55:23
1 scipy.spatial
from scipy import spatial
在scipy.spatial中最重要的模块应该就是距离计算模块distance了。
2 scipy.spatial.distance.cdist
2.1 语法
scipy.spatial.distance.cdist(XA, XB, metric='euclidean',
p=None, V=None, VI=None, w=None)
该函数用于计算两个输入集合的距离,通过metric参数指定计算距离的不同方式得到不同的距离度量值。
2.2 metric的取值
braycurtis
canberra
chebyshev:切比雪夫距离
cityblock
correlation:相关系数
cosine:余弦夹角
dice
euclidean:欧式距离
hamming:汉明距离
jaccard:杰卡德相似系数
kulsinski
mahalanobis:马氏距离
matching
minkowski:闵可夫斯基距离
rogerstanimoto
russellrao
seuclidean:标准化欧式距离
sokalmichener
sokalsneath
sqeuclidean
wminkowski
yule
2.3 常用欧氏距离计算
from scipy.spatial.distance import cdist
import numpy as np
x1 =np.array([(1,3),(2,4),(5,6)])
x2 =[(3,7),(4,8),(6,9)]
cdist(x1,x2,metric='euclidean')
#=================结果=================
array([[ 4.47213595, 5.83095189, 7.81024968],
[ 3.16227766, 4.47213595, 6.40312424],
[ 2.23606798, 2.23606798, 3.16227766]])
解析上述计算过程:结果数组中的第一行数据表示的是x1数组中第一个元素点与x2数组中各个元素点的距离,计算两点之间的距离,以点(1,3)与(3,7)点的距离为例:
np.power((1-3)**2 +(3-7)**2,1/2)
#=================结果=================
4.4721359549995796
来源:https://blog.csdn.net/weixin_46039719/article/details/123036425
标签:python,scipy.spatial.distance
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python绘制汉诺塔
2023-07-09 23:13:19
![](https://img.aspxhome.com/file/2023/1/60511_0s.gif)
Python爬虫之用Xpath获取关键标签实现自动评论盖楼抽奖(二)
2021-02-11 00:58:03
![](https://img.aspxhome.com/file/2023/2/72082_0s.png)
操作Dom节点实现间歇滚动新闻
2009-10-16 20:51:00
python使用nibabel和sitk读取保存nii.gz文件实例
2021-03-11 16:12:53
![](https://img.aspxhome.com/file/2023/8/64708_0s.jpg)
Python读取xlsx文件报错:xlrd.biffh.XLRDError: Excel xlsx file;not supported问题解决
2021-02-02 16:08:59
![](https://img.aspxhome.com/file/2023/2/72192_0s.png)
利用PyInstaller将python程序.py转为.exe的方法详解
2021-07-09 16:41:51
python异常处理try的实例小结
2022-01-25 06:06:51
Jenkins使用publish html report插件展示HTML报告的方法
2022-03-31 19:30:54
![](https://img.aspxhome.com/file/2023/0/132300_0s.png)
Golang中Gin框架的使用入门教程
2024-05-09 09:32:27
Python根据输入参数计算结果的实例方法
2021-12-15 08:30:45
python的django写页面上传文件及遇到的问题小结
2022-02-28 23:21:10
![](https://img.aspxhome.com/file/2023/7/109327_0s.png)
深度解析Django REST Framework 批量操作
2021-05-18 03:03:21
![](https://img.aspxhome.com/file/2023/2/76002_0s.png)
使用FormData实现上传多个文件
2024-05-11 09:30:20
![](https://img.aspxhome.com/file/2023/9/126179_0s.png)
使用Python中tkinter库简单gui界面制作及打包成exe的操作方法(二)
2023-04-25 19:32:40
如何获取浏览器的更多信息?
2009-11-23 20:48:00
Javascript中Eval函数的使用
2024-03-24 19:55:23
深刻理解Oracle数据库的启动和关闭
2010-07-26 13:08:00
python opencv之SIFT算法示例
2023-12-27 21:42:33
![](https://img.aspxhome.com/file/2023/0/65950_0s.png)
python+opencv实现高斯平滑滤波
2023-04-02 15:56:55
![](https://img.aspxhome.com/file/2023/1/82531_0s.jpg)
K-means聚类算法介绍与利用python实现的代码示例
2023-07-29 11:08:55
![](https://img.aspxhome.com/file/2023/6/120606_0s.png)