当前位置:网站首页>介绍汉明距离及计算示例
介绍汉明距离及计算示例
2022-07-05 04:31:00 【梦想画家】
汉明距离(Hamming distance)是计算两个向量之间不同对应元素数量之和。本文介绍R、Python语言的计算过程。
汉明距离概述
汉明距离是以美国数学家理查德·汉明的名字命名的,他在1950年关于汉明码的论文中提出了该举例度量指标。它被广泛用于多个学科,如信息论、编码理论和密码学。
举例:
“karolin” 和 “kathrin” 两个字符串汉明距离为s 3 (差异字符有: r-t, o-h, l-r),
1011100 和 1001000 汉明距离为 2 (不同二进制数为: 1-0 和 1-0)
31738 和 32337 汉明距离为 3 (不同整数位: 1-2, 7-3, 和 8-7).
汉明距离在解决很多问题中被证明是非常有用。例如,在编码理论中它被用于错误检测和纠错码;在遗传学中它被用来衡量基因差异;在机器学习算法中用来寻找相似点或区别。
实现函数
假设有两个向量 x = [1, 2, 3, 4] ;y = [1, 2, 5, 7] .
它们之间的汉明距离为2,因为最后两个对应元素不同。
R语言的计算逻辑为:
sum(x != y)
Python的计算过程为:
# 自定义函数实现
def hammingDistance(s1, s2):
"""返回等长序列之间的汉明距离"""
if len(s1) != len(s2):
raise ValueError("Undefined for sequences of unequal length")
return sum(el1 != el2 for el1, el2 in zip(s1, s2))
当然也可以使用 scipy.spatial.distance 包 hamming 函数直接计算。
示例1(二值类型)
下面示例计算两个包括二值向量之间的距离:
# R语言实现
# 创建向量
x <- c(0, 0, 1, 1, 1)
y <- c(0, 1, 1, 1, 0)
sum(x != y)
# [1] 2
Python代码实现:
from scipy.spatial.distance import hamming
x = [0, 1, 1, 1, 0, 1]
y = [0, 0, 1, 1, 0, 0]
hamming(x, y) * len(x)
# 2.0
示例2(数值类型)
数值类型向量的汉明距离:
# 创建向量
x <- c(7, 12, 14, 19, 22)
y <- c(7, 12, 16, 26, 27)
# 计算汉明距离
sum(x != y)
# [1] 3
Python代码实现:
from scipy.spatial.distance import hamming
x = [7, 12, 14, 19, 22]
y = [7, 12, 16, 26, 27]
hamming(x, y) * len(x)
# 3.0
示例3(字符串类型)
计算字符串类型汉明距离:
# 创建向量
x <- c('a', 'b', 'c', 'd')
y <- c('a', 'b', 'c', 'r')
# 计算汉明距离
sum(x != y)
# [1] 1
Python语言实现:
from scipy.spatial.distance import hamming
# 定义数组
x = ['a', 'b', 'c', 'd']
y = ['a', 'b', 'c', 'r']
hamming(x, y) * len(x)
# 1.0
总结
本文介绍了汉明距离,并通过示例介绍R和Python语言的实现过程。
边栏推荐
- 10 programming habits that web developers should develop
- 3 minutes learn to create Google account and email detailed tutorial!
- All in one 1413: determine base
- 蛇形矩阵
- 防护电路中的元器件
- Un réveil de l'application B devrait être rapide
- Longyuan war "epidemic" 2021 network security competition web easyjaba
- 【虚幻引擎UE】运行和启动的区别,常见问题分析
- Basic analysis of IIC SPI protocol
- windows下Redis-cluster集群搭建
猜你喜欢

windows下Redis-cluster集群搭建

【虚幻引擎UE】实现背景模糊下近景旋转操作物体的方法及踩坑记录

Longyuan war "epidemic" 2021 network security competition web easyjaba

MacBook installation postgresql+postgis

【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)

WeNet:面向工业落地的E2E语音识别工具
![[uniapp] system hot update implementation ideas](/img/1e/77ee9d9f0e08fa2a7734a54e0c5020.png)
[uniapp] system hot update implementation ideas

函数(基本:参数,返回值)

Label exchange experiment

Web开发人员应该养成的10个编程习惯
随机推荐
windows下Redis-cluster集群搭建
Looking back on 2021, looking forward to 2022 | a year between CSDN and me
Judge whether the stack order is reasonable according to the stack order
Matplotlib draws three-dimensional scatter and surface graphs
Mxnet imports various libcudarts * so、 libcuda*. So not found
10 programming habits that web developers should develop
Convert Boolean to integer value PHP - Convert Boolean to integer value PHP
TPG x AIDU|AI领军人才招募计划进行中!
Mixed compilation of C and CC
PHP读取ini文件并修改内容写入
Managed service network: application architecture evolution in the cloud native Era
Machine learning decision tree
假设检验——《概率论与数理统计》第八章学习笔记
电源管理总线 (PMBus)
The scale of computing power in China ranks second in the world: computing is leaping forward in Intelligent Computing
How to remove installed elpa package
MacBook installation postgresql+postgis
如何进行「小步重构」?
OWASP top 10 vulnerability Guide (2021)
How can CIOs use business analysis to build business value?