如何在两个不同大小的 numpy 列表数组中获取相似列表的布尔矩阵
How to get Boolean matrix for similar lists in two different-size numpy arrays of lists
首先我写了一个to lists的小例子:
F = [[1,2,3],[3,2,7],[4,4,1],[5,6,3],[1,3,7]] # (1*5) 5 lists
S = [[1,3,7],[6,8,1],[3,2,7]] # (1*3) 3 lists
我想为两个 F 和 S 中的相同“列表”获取布尔矩阵:
[False, True, False, False, True] # (1*5) 5 Booleans for 5 lists of F
通过使用 IM = reduce(np.in1d, (F, S))
它给出 F:
的每个列表中每个数字的结果
[ True True True True True True False False True False True True
True True True] # (1*15)
通过使用 IM = reduce(np.isin, (F, S))
它也给出了 F 的每个列表中每个数字的结果,但是以另一种形式:
[[ True True True]
[ True True True]
[False False True]
[False True True]
[ True True True]] # (5*3)
真正的结果将通过示例列表的代码 IM = [i in S for i in F]
实现,但是当我将此代码用于我的两个主要的更大的 numpy 列表数组时:
https://drive.google.com/file/d/1YUUdqxRu__9-fhE1542xqei-rjB3HOxX/view?usp=sharing
numpy 数组:3036 个列表
https://drive.google.com/file/d/1FrggAa-JoxxoRqRs8NVV_F69DdVdiq_m/view?usp=sharing
numpy 数组:300 个列表
给出了错误的答案。对于主要文件,它必须给出 3036 Boolean,其中 'True' 只是 300 个数字。我不明白为什么这会得到错误的答案?它似乎只适用于 F 的每个列表中的第 3 个字符。
最好通过np.in1d和np.isin这两个函数来使用reduce函数,而不是最后一种方法。如何解决以上三种方法?
让我知道这是否有效,
[x for x in map(lambda m: m in S, F)]
问题已通过 'tolist' 将输入转换为列表解决,同时 IM = [i in S for i in F] 和 IM = [x for x in map(lambda m: m在 S, F)] 代码中;然而,这些类型的转换会少量降低精度。如果除了 reduce.
之外,还可以通过 np.logical_and 等 numpy 函数解决问题,那将很有用
首先我写了一个to lists的小例子:
F = [[1,2,3],[3,2,7],[4,4,1],[5,6,3],[1,3,7]] # (1*5) 5 lists
S = [[1,3,7],[6,8,1],[3,2,7]] # (1*3) 3 lists
我想为两个 F 和 S 中的相同“列表”获取布尔矩阵:
[False, True, False, False, True] # (1*5) 5 Booleans for 5 lists of F
通过使用 IM = reduce(np.in1d, (F, S))
它给出 F:
[ True True True True True True False False True False True True
True True True] # (1*15)
通过使用 IM = reduce(np.isin, (F, S))
它也给出了 F 的每个列表中每个数字的结果,但是以另一种形式:
[[ True True True]
[ True True True]
[False False True]
[False True True]
[ True True True]] # (5*3)
真正的结果将通过示例列表的代码 IM = [i in S for i in F]
实现,但是当我将此代码用于我的两个主要的更大的 numpy 列表数组时:
https://drive.google.com/file/d/1YUUdqxRu__9-fhE1542xqei-rjB3HOxX/view?usp=sharing
numpy 数组:3036 个列表
https://drive.google.com/file/d/1FrggAa-JoxxoRqRs8NVV_F69DdVdiq_m/view?usp=sharing
numpy 数组:300 个列表
给出了错误的答案。对于主要文件,它必须给出 3036 Boolean,其中 'True' 只是 300 个数字。我不明白为什么这会得到错误的答案?它似乎只适用于 F 的每个列表中的第 3 个字符。 最好通过np.in1d和np.isin这两个函数来使用reduce函数,而不是最后一种方法。如何解决以上三种方法?
让我知道这是否有效,
[x for x in map(lambda m: m in S, F)]
问题已通过 'tolist' 将输入转换为列表解决,同时 IM = [i in S for i in F] 和 IM = [x for x in map(lambda m: m在 S, F)] 代码中;然而,这些类型的转换会少量降低精度。如果除了 reduce.
之外,还可以通过 np.logical_and 等 numpy 函数解决问题,那将很有用