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

问题描述 投票:0回答:2

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

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

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

 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)

假设我有以下向量x数组,其中可能的值为0,1,2:以num x的形式导入numpy x = np.random.randint(0,3,(10,5),dtype = np。 int8)我想对所有...进行相似性匹配...

python numpy vector similarity hamming-distance
2个回答
4
投票
int(x+y == 1)

0
投票

这不行吗?

© www.soinside.com 2019 - 2024. All rights reserved.