在numpy中替换子阵列 [英] Replace subarrays in numpy

查看:778
本文介绍了在numpy中替换子阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个数组,

>>> n = 2
>>> a = numpy.array([[[1,1,1],[1,2,3],[1,3,4]]]*n)
>>> a
array([[[1, 1, 1],
        [1, 2, 3],
        [1, 3, 4]],

       [[1, 1, 1],
        [1, 2, 3],
        [1, 3, 4]]])

我知道可以简洁地替换它中的值,

I know that it's possible to replace values in it succinctly like so,

>>> a[a==2] = 0
>>> a
array([[[1, 1, 1],
        [1, 0, 3],
        [1, 3, 4]],

       [[1, 1, 1],
        [1, 0, 3],
        [1, 3, 4]]])

是否可以对数组中的整行(最后一个轴)执行相同的操作?我知道 a [a == [1,2,3]] = 11 将工作并用11代替匹配子阵列的所有元素,但我想要替换不同的子阵列。我的直觉告诉我写下面的内容,但会出现错误结果,

Is it possible to do the same for an entire row (last axis) in the array? I know that a[a==[1,2,3]] = 11 will work and replace all the elements of the matching subarrays with 11, but I'd like to substitute a different subarray. My intuition tells me to write the following, but an error results,

>>> a[a==[1,2,3]] = [11,22,33]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: array is not broadcastable to correct shape

总之,我想要的是什么获得是:

In summary, what I'd like to get is:

array([[[1, 1, 1],
        [11, 22, 33],
        [1, 3, 4]],

       [[1, 1, 1],
        [11, 22, 33],
        [1, 3, 4]]])

...当然,n当然是大于2,其他轴也大于3,所以如果我不需要,我不想循环它们。

... and n of course is, in general, a lot larger than 2, and the other axes are also larger than 3, so I don't want to loop over them if I don't need to.

更新: [1,2,3] (或我正在寻找的任何其他内容)并不总是在索引1处。例如:

Update: The [1,2,3] (or whatever else I'm looking for) is not always at index 1. An example:

a = numpy.array([[[1,1,1],[1,2,3],[1,3,4]], [[1,2,3],[1,1,1],[1,3,4]]])


推荐答案

你必须做一些更复杂的事情来实现你想要的东西。

You have to do something a little more complicated to acheive what you want.

您不能选择这样的数组切片,但可以选择所有特定索引你想要的。

You can't select slices of arrays as such, but you can select all the specific indexes you want.

首先你需要构造一个代表你想要选择的行的数组。 ie。

So first you need to construct an array that represents the rows you wish to select. ie.

data = numpy.array([[1,2,3],[55,56,57],[1,2,3]])

to_select = numpy.array([1,2,3]*3).reshape(3,3) # three rows of [1,2,3]

selected_indices = data == to_select
# array([[ True,  True,  True],
#        [False, False, False],
#        [ True,  True,  True]], dtype=bool)

data = numpy.where(selected_indices, [4,5,6], data)
# array([[4, 5, 6],
#        [55, 56, 57],
#        [4, 5, 6]])

# done in one step, but perhaps not very clear as to its intent
data = numpy.where(data == numpy.array([1,2,3]*3).reshape(3,3), [4,5,6], data)

numpy.where 通过选择第二个参数来工作如果为true,则第三个参数为false。

numpy.where works by selecting from the second argument if true and the third argument if false.

您可以使用从哪里选择3种不同类型的数据。第一个是与 selected_indices 形状相同的数组,第二个只是一个值(如2或7)。第一个是最复杂的,可以是形状,可以广播到与 selected_indices 相同的形状。在这种情况下,我们提供了 [1,2,3] ,它们可以堆叠在一起以获得形状为3x3的数组。

You can use where to select from 3 different types of data. The first is an array that has the same shape as selected_indices, the second is just a value on its own (like 2 or 7). The first is most complicated as can be of shape that can be broadcast into the same shape as selected_indices. In this case we provided [1,2,3] which can be stacked together to get an array with shape 3x3.

这篇关于在numpy中替换子阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆