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

2024-03-29 14:37:56 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我想对汉明距离为0或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 = np.random.randint(0,2,5)
np.sum(np.add(x,z) == 1, axis=1)

Tags: andimportnumpy距离isasnprandom
2条回答

这不管用吗?你知道吗

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

或者如果你想两边都有两个

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

这个问题有什么我遗漏的吗???你知道吗

相关问题 更多 >