【发布时间】: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