机器学习中的常用距离

简介: If x1,x2∈Rnx_{1}, x_{2}\in\mathbb{R}^{n}, then: 闵可夫斯基距离 Minkowski Distance d12=∑k=1n(x1k−x2k)p−−−−−−−−−−−−√p,p>0d_{12}=\sqrt[\uproot{16}p]{\sum_{k=1}^{n}(x_{1k}-x_{2k})^{p}},\quad p>0欧

If x1,x2Rn, then:
闵可夫斯基距离 Minkowski Distance

d12=k=1n(x1kx2k)pp,p>0

欧氏距离 Enclidean Distance
L2 norm

d12=k=1n(x1kx2k)2 or d12=(x1x2)T(x1x2)

标准化欧式距离/加权欧式距离 Weighted Euclidean Distance

d12=k=1n(x1kx2kSk)2

where Sk is the standard deviation.
from numpy import *
vectormat=mat([[1,2,3],[4,5,6]])
v12=vectormat[0]-vectormat[1]
varmat=std(vectormat.T, axis=0)
normmat=(vectormat-mean(vectormat))/varmat.T
normv12=normmat[0]-normmat[1]
print(sqrt(normv12*normv12.T))

曼哈顿距离 Manhattan Distance
L1 norm

d12=k=1n|x1kx2k|

切比雪夫距离 Chebyshev Distance
L norm

d12=maxi(|x1ix2i|)
from numpy import *
vector1=mat([1,2,3])
vector2=mat([4,5,7])
print(abs(vector1-vector2).max())

夹角余弦 Cosine

cosθ=nk=1x1kx2knk=1x21knk=1x22k

汉明距离 Hamming Distance
In information theory, the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different. In other words, it measures the minimum number of substitutions required to change one string into the other. (referred from Wikipedia)

from numpy import *
matV=mat([[1,1,0,1,0,1,0,0,1],[0,1,1,0,0,0,1,1,1]])
smstr=nonzero(matV [0]-matV[1])
print(shape(smstr[0])[0])

杰卡德相似系数 Jaccard Similarity Coefficient
Given two sets, A and B, the Jaccard similarity coefficient is defined as

J(A,B)=|AB||AB|

杰卡德距离 Jaccard Distance

Jδ(A,B)=1J(A,B)=|AB||AB||AB|
from numpy import *
import scipy.spatial.distance as dist
matV=mat([[1,1,0,1,0,1,0,0,1],[0,1,1,0,0,0,1,1,1]])
print(dist.pdist(matV,'jaccard'))

马氏距离 Mahalanobis Distance
Given m sample vectors X1,,Xm whose mean value is μ and covariance matrix is S, then the Mahalanobis distance of sample vector X and μ is defined as

D(X)=(Xμ)TS1(Xμ)

that of sample vector Xi and Xj is
D(X)=(XiXj)TS1(XiXj)
相关文章
|
Linux Shell 开发工具
10分钟让你的Linux Shell终端变得更优雅,更高效——【Linux服务器下OhMyZsh+P10k安装实践】
10分钟让你的Linux Shell终端变得更优雅,更高效——【Linux服务器下OhMyZsh+P10k安装实践】
494 1
|
6月前
|
自然语言处理 监控 PyTorch
nanoVLM: 简洁、轻量的纯 PyTorch 视觉-语言模型训练代码库
nanoVLM 是一个基于 PyTorch 的轻量级工具包,专为训练视觉语言模型(VLM)设计。它结构简洁、易于理解,适合初学者快速上手。支持在免费 Colab Notebook 上训练,结合视觉 Transformer 与语言模型,实现图像理解和文本生成。项目受 nanoGPT 启发,注重代码可读性与实现效率。
311 122
|
算法 数据处理 开发者
超越传统:Python二分查找的变种策略,让搜索效率再上新台阶!
【7月更文挑战第11天】二分查找算法在有序数组中以O(log n)效率搜索元素。本文扩展了这一概念,介绍了3种Python变种:1) 查找第一个匹配值的位置,2) 找到最后一个匹配值,3) 在旋转有序数组中搜索。通过调整边界条件,这些变种增强了二分查找的适用性。代码示例展示了如何实现这些策略,以优化不同场景下的搜索效率。
154 0
|
1天前
|
数据采集 人工智能 安全
|
10天前
|
云安全 监控 安全
|
2天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
930 150
|
2天前
|
编解码 人工智能 机器人
通义万相2.6,模型使用指南
智能分镜 | 多镜头叙事 | 支持15秒视频生成 | 高品质声音生成 | 多人稳定对话