【问题标题】:How to get Boolean matrix for similar lists in two different-size numpy arrays of lists如何在两个不同大小的numpy列表数组中获取相似列表的布尔矩阵
【发布时间】:2021-06-11 20:14:07
【问题描述】:

首先,我写了一个 to list 的小例子:

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 布尔值,其中“真”只有 300 个数字。我不明白为什么会得到错误的答案??似乎它仅适用于每个 F 列表中的第三个字符。 最好通过 np.in1d 和 np.isin 这两个函数来使用 reduce 函数,而不是最后一种方法。上述三种方法如何分别解决??

【问题讨论】:

    标签: python list numpy scipy numpy-ndarray


    【解决方案1】:

    让我知道这是否有效,

    [x for x in map(lambda m: m in S, F)]
    

    【讨论】:

    • 没有。它不起作用;结果为IM = [i in S for i in F]
    【解决方案2】:

    问题已通过“tolist”将输入转换为列表来解决,其中 IM = [i in S for i in F] 和 IM = [x for x in map(lambda m: m in S, F )] 代码;但是,这些类型的转换会少量降低精度。如果问题可以通过诸如 np.logical_and 除了 reduce 之类的 numpy 函数来解决,那将很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多