当前位置:网站首页>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 .
边栏推荐
- Interview related high-frequency algorithm test site 3
- Rk3399 platform development series explanation (network debugging) 7.29 summary of network performance tools
- 这是一个不确定的时代
- level18
- 概率论与数理统计考试重点复习路线
- WeNet:面向工业落地的E2E语音识别工具
- [Business Research Report] top ten trends of science and technology and it in 2022 - with download link
- Sequence diagram of single sign on Certification Center
- Data security -- 14 -- Analysis of privacy protection governance
- Error statuslogger log4j2 could not find a logging implementation
猜你喜欢

介绍汉明距离及计算示例

Decryption function calculates "task state and lifecycle management" of asynchronous task capability

Serpentine matrix

Discussion on the dimension of confrontation subspace

揭秘技术 Leader 必备的七大清奇脑回路

C26451: arithmetic overflow: use the operator * on a 4-byte value, and then convert the result to an 8-byte value. To avoid overflow, cast the value to wide type before calling the operator * (io.2)
![[goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens](/img/20/5c5550e6dabc76702f0e7ce3bef068.jpg)
[goweb development] Introduction to authentication modes based on cookies, sessions and JWT tokens
![[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)](/img/eb/a93630aff7545c6c3b71dcc9f5aa61.png)
[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)

首席信息官如何利用业务分析构建业务价值?

Setting up redis cluster cluster under Windows
随机推荐
[popular science] basic knowledge of thermal design: heat dissipation analysis of 5g optical devices
[AI bulletin 20220211] the hard core up owner has built a lidar and detailed AI accelerator
[crampon game] MC tutorial - first day of survival
Official announcement! The third cloud native programming challenge is officially launched!
Observable time series data downsampling practice in Prometheus
How to remove installed elpa package
Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...
Sequelize. JS and hasmany - belongsto vs hasmany in serialize js
Hexadecimal to octal
Variable category (automatic, static, register, external)
这是一个不确定的时代
Chapter 6 text processing tools for shell programming (awk)
Practice | mobile end practice
Interface joint commissioning test script optimization V5.0 (end)
Debug insights
假设检验——《概率论与数理统计》第八章学习笔记
Mxnet imports various libcudarts * so、 libcuda*. So not found
官宣!第三届云原生编程挑战赛正式启动!
10 programming habits that web developers should develop
MySQL: view with subquery in the from clause limit