# 汉明距离、汉明损失详解及代码（python）

### 文章目录

• 引言
• 汉明距离(Hamming distance)
• 代码示例
• 汉明损失(Hamming loss)
• 代码示例
• 参考链接

## 汉明距离(Hamming distance)

### 代码示例

# 写法一：
def hamming_distance(string1, string2):
dist_counter = 0
for n in range(len(string1)):
if string1[n] != string2[n]:
dist_counter += 1
return dist_counter

# 写法二：或者采用更短的表达式
sum(xi != yi for xi, yi in zip(x, y))


## 汉明损失(Hamming loss)

L H a m m i n g ( y , y ^ ) = 1 n labels ∑ j = 0 n labels − 1 1 ( y ^ j ≠ y j ) L_{Hamming}(y, \hat{y}) = \frac{1}{n_\text{labels}} \sum_{j=0}^{n_\text{labels} - 1} 1(\hat{y}_j \not= y_j)

### 代码示例

sklearn.metrics.hamming_loss(y_true, y_pred, *, sample_weight=None)

>>> from sklearn.metrics import hamming_loss
>>> y_pred = [1, 2, 3, 4]
>>> y_true = [2, 2, 3, 4]
>>> hamming_loss(y_true, y_pred)
0.25


y_true, y_pred只有第一个位置不同，1/4=0.25。

>>> import numpy as np
>>> hamming_loss(np.array([[0, 1], [1, 1]]), np.zeros((2, 2)))
0.75


## 参考链接

ACM Multi-label Classification Tutorial
https://en.wikipedia.org/wiki/Hamming_distance
https://scikit-learn.org/stable/modules/model_evaluation.html#hamming-loss
http://www.manongjc.com/detail/30-emspeytwauwwdai.html