当前位置:网站首页>Introduce Hamming distance and calculation examples
Introduce Hamming distance and calculation examples
2022-07-05 04:38:00 【Dream painter】
Hamming distance (Hamming distance) It is to calculate the sum of the number of different corresponding elements between two vectors . In this paper, R、Python The calculation process of language .
Overview of Hamming distance
Hamming distance is based on American mathematician Richard · It's named after Han Ming , He was in 1950 The example measurement index was proposed in the paper on Hamming code in . It is widely used in many disciplines , Such as information theory 、 Coding theory and cryptography .
give an example :
“karolin” and “kathrin” The Hamming distance between two strings is s 3 ( The difference characters are : r-t, o-h, l-r),
1011100 and 1001000 The distance between Han and Ming is 2 ( Different binary numbers are : 1-0 and 1-0)
31738 and 32337 The distance between Han and Ming is 3 ( Different integer digits : 1-2, 7-3, and 8-7).
Hamming distance has proved to be very useful in solving many problems . for example , In coding theory, it is used for error detection and error correction codes ; In genetics, it is used to measure genetic differences ; Used to find similarities or differences in machine learning algorithms .
Implementation function
Suppose you have two vectors x = [1, 2, 3, 4] ;y = [1, 2, 5, 7] .
The Hamming distance between them is 2, Because the last two corresponding elements are different .
R The computational logic of the language is :
sum(x != y)
Python The calculation process of is :
# Custom function implementation
def hammingDistance(s1, s2):
""" Returns the Hamming distance between equal length sequences """
if len(s1) != len(s2):
raise ValueError("Undefined for sequences of unequal length")
return sum(el1 != el2 for el1, el2 in zip(s1, s2))
You can also use it scipy.spatial.distance package hamming The function calculates directly .
Example 1( Binary type )
The following example calculates the distance between two including binary vectors :
# R Language implementation
# Create a vector
x <- c(0, 0, 1, 1, 1)
y <- c(0, 1, 1, 1, 0)
sum(x != y)
# [1] 2
Python Code implementation :
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
Example 2( value type )
Hamming distance of numeric type vector :
# Create a vector
x <- c(7, 12, 14, 19, 22)
y <- c(7, 12, 16, 26, 27)
# Calculate Hamming distance
sum(x != y)
# [1] 3
Python Code implementation :
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
Example 3( String type )
Calculate Hamming distance of string type :
# Create a vector
x <- c('a', 'b', 'c', 'd')
y <- c('a', 'b', 'c', 'r')
# Calculate Hamming distance
sum(x != y)
# [1] 1
Python Language implementation :
from scipy.spatial.distance import hamming
# Define an array
x = ['a', 'b', 'c', 'd']
y = ['a', 'b', 'c', 'r']
hamming(x, y) * len(x)
# 1.0
summary
This paper introduces the Hamming distance , And introduce through examples R and Python The implementation process of language .
边栏推荐
- Neural network and deep learning Chapter 1: introduction reading questions
- jmeter -- 分布式压测
- Official announcement! The third cloud native programming challenge is officially launched!
- level18
- English topic assignment (26)
- QT Bluetooth: a class for searching Bluetooth devices -- qbluetooth devicediscoveryagent
- After the deployment of web resources, the navigator cannot obtain the solution of mediadevices instance (navigator.mediadevices is undefined)
- [crampon game] MC tutorial - first day of survival
- CSDN正文自动生成目录
- Inline built-in function
猜你喜欢

User behavior collection platform

取余操作是一个哈希函数

MacBook installation postgresql+postgis

TPG x AIDU|AI领军人才招募计划进行中!
![[uniapp] system hot update implementation ideas](/img/1e/77ee9d9f0e08fa2a7734a54e0c5020.png)
[uniapp] system hot update implementation ideas

如何优雅的获取每个分组的前几条数据
![[phantom engine UE] realize the animation production of mapping tripod deployment](/img/89/351641c3da7e2acdbf389bc298b75e.png)
[phantom engine UE] realize the animation production of mapping tripod deployment

The remainder operation is a hash function

托管式服务网络:云原生时代的应用体系架构进化

Raki's notes on reading paper: soft gazetteers for low resource named entity recognition
随机推荐
File upload bypass summary (upload labs 21 customs clearance tutorial attached)
Hexadecimal to decimal
CUDA Programming atomic operation atomicadd reports error err:msb3721, return code 1
Variable category (automatic, static, register, external)
mxnet导入报各种libcudart*.so、 libcuda*.so找不到
概率论与数理统计考试重点复习路线
[phantom engine UE] package error appears! Solutions to findpin errors
How to remove installed elpa package
[Chongqing Guangdong education] National Open University 2047t commercial bank operation and management reference test in autumn 2018
Neural networks and deep learning Chapter 3: linear model reading questions
CSDN正文自动生成目录
Here comes the Lantern Festival red envelope!
美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
【thingsboard】替换首页logo的方法
English topic assignment (27)
2022-2028 global and Chinese video coding and transcoding Market Research Report
About the prompt loading after appscan is opened: guilogic, it keeps loading and gets stuck. My personal solution. (it may be the first solution available in the whole network at present)
这是一个不确定的时代
[moteur illusoire UE] il ne faut que six étapes pour réaliser le déploiement du flux de pixels ue5 et éviter les détours! (4.26 et 4.27 principes similaires)
The principle of attention mechanism and its application in seq2seq (bahadanau attention)