汉明距离的三元函数,其中"2"是通配符



假设我有以下向量数组x,其中可能的值为0,1,2:

import numpy as np
x = np.random.randint(0,3,(10,5), dtype=np.int8)

我想对所有汉明距离为零或 1 的向量进行相似性匹配,其中匹配规则为:

1. 0 == 0 and 1 == 1 i.e. hamming distance is 0
2. 2 match both 1 and 0 i.e. hamming distance is 0
3. otherwise Hamming distance is 1

即找到一些将返回的算术运算:

0 x 0 = 0
1 x 1 = 0
0 x 1 = 1
1 x 0 = 1
0 x 2 = 0
1 x 2 = 0

我的输出应该是每个向量(行(x和任意向量z之间的汉明距离:

z = np.random.randint(0,2,5)
np.sum(np.add(x,z) == 1, axis=1)
int(x+y == 1)

这个问题中有什么我遗漏的吗???

这难道不行吗?

((x!=y) ^ (x==2) ^ (y==2)).sum() <=1

或者,如果您想在两侧允许两个

((x!=y) ^ (x==2) | (y==2)).sum() <=1

最新更新