【问题标题】:How do I select rows in an input numpy array and store operation results in corresponding row in an output array?如何在输入 numpy 数组中选择行并将操作结果存储在输出数组中的相应行中?
【发布时间】:2020-10-25 13:24:00
【问题描述】:

This is a rough visualization of what I want to achieve, but also read the text.

所以我有一堆以以下方式存储的 numpy 数组。为了简单起见,我在“其他”中使用 2。

matrices['in'] = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrices['other'] = np.array([[2, 2, 2], [2, 2, 2], [2, 2, 2]])
matrices['out'] = np.zeros((n, n), dtype=np.int)

>>> matrices['in']
[[1 2 3]
 [4 5 6]
 [7 8 9]]

>>> matrices['other']
[[2 2 2]
 [2 2 2]
 [2 2 2]]

>>> matrices['out']
[[0 0 0]
 [0 0 0]
 [0 0 0]]

我希望对每行进行某种操作,但我希望控制哪些行包含在我的整体计算中。于是我又做了一个数组:

included = [True] * n
matrices['included'] = np.array(included)
matrices['included'].itemset(1, False)

>>> matrices['included']
[ True False  True]

而且它似乎按预期工作。

mask = matrices['included']
>>> matrices['in'][mask]
[[1 2 3]
 [7 8 9]]

但是。正如您在上面可能已经注意到的那样,我在matrices['out'] 中使用了预定义的输出,因为操作执行的频率高于数组大小的变化,所以我自然不想每次进行新计算时都重新创建一个新的数组对象.

因此我的操作看起来像这样:

np.multiply(matrices['in'], matrices['other'], out=matrices['out'])

或者在使用numexpr的情况下:

numexpr.evaluate("m_in * m_other",
    local_dict={
        'm_in': matrices['in'],
        'm_other': matrices['other']
    },
    out=matrices['out'])

这些工作正常,但我遇到的问题是我无法弄清楚如何让它与面具一起工作;通过matrices['included'] 选择行并将它们存储在我的输出矩阵的相应行中。

当我执行以下任一操作时:

np.multiply(matrices['in'][mask], matrices['other'][mask], out=matrices['out'][mask])

numexpr.evaluate("m_in * m_other",
    local_dict={
        'm_in': matrices['in'][mask],
        'm_other': matrices['other'][mask]
    },
    out=matrices['out'][mask])

什么都没有发生,即它导致:

>>> matrices['out']
[[0 0 0]
 [0 0 0]
 [0 0 0]]

使用[mask, ...][mask, :] 会得到同样的结果。

在这种情况下我想要的输出是:

[[2 4 6]
 [0 0 0]
 [14 16 18]]

【问题讨论】:

  • 我的回答对你有用吗?
  • @Tyberius 是的,感谢您的回答,它促使我试一试,这就是我目前解决问题的方法。这是我熟悉的一个解决方案,我想我部分尝试避免,因为它可能会增加开销并使事情变慢,因为它仍然迭代整个数组。我想出了一些方法来进行“类似切片”的选择,然后将这些“切片”放入输出数组中。就像对字典中的一组特定条目使用查找一样,而不是遍历列表并跳过不需要的条目。但我可能只是对切片在实践中的工作方式一无所知。

标签: python arrays numpy matrix


【解决方案1】:

Numpy.multiply 有一个where 关键字,允许您包含操作的掩码。这将在指定位置应用该操作,并在其他任何地方保持输出数组不变。

下面的示例应该为您提供您正在寻找的行为:

import numpy as np
A=np.array([[1,2,3],[4,5,6],[7,8,9]])
B=2*np.ones(3,dtype=int)
C=np.zeros((3,3),dtype=int)
mask=np.transpose([[True,False,True],])

np.multiply(A,B,out=C,where=mask)
print(C)

我没有尝试您的示例来确认这一点,但我相信当您将掩码应用于 out 数组时,您创建了一个新数组,实际发送输出的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多