索引索引数组时设置Numpy数组的值 [英] Setting values of Numpy array when indexing an indexed array

查看:91
本文介绍了索引索引数组时设置Numpy数组的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试索引某些矩阵y,然后使用一些布尔语句对结果重新索引,并将y中的相应元素设置为0.下面显示了我用于测试此索引方案的伪代码.

I'm trying to index some matrix, y, and then reindex that result with some boolean statement and set the corresponding elements in y to 0. The dummy code I'm using to test this indexing scheme is shown below.

x=np.zeros([5,4])+0.1;
y=x;
print(x)
m=np.array([0,2,3]);
y[0:4,m][y[0:4,m]<0.5]=0;
print(y)

我不确定为什么它不起作用.我想要的输出:

I'm not sure why it does not work. The output I want:

[[ 0.1  0.1  0.1  0.1]
 [ 0.1  0.1  0.1  0.1]
 [ 0.1  0.1  0.1  0.1]
 [ 0.1  0.1  0.1  0.1]
 [ 0.1  0.1  0.1  0.1]]
[[ 0.   0.1  0.   0. ]
 [ 0.   0.1  0.   0. ]
 [ 0.   0.1  0.   0. ]
 [ 0.   0.1  0.   0. ]
 [ 0.1  0.1  0.1  0.1]]

但是我实际上得到的是:

But what I actually get:

[[ 0.1  0.1  0.1  0.1]
 [ 0.1  0.1  0.1  0.1]
 [ 0.1  0.1  0.1  0.1]
 [ 0.1  0.1  0.1  0.1]
 [ 0.1  0.1  0.1  0.1]]
[[ 0.1  0.1  0.1  0.1]
 [ 0.1  0.1  0.1  0.1]
 [ 0.1  0.1  0.1  0.1]
 [ 0.1  0.1  0.1  0.1]
 [ 0.1  0.1  0.1  0.1]]

我确定我缺少一些幕后细节,这些细节解释了为什么这种方法不起作用.有趣的是,如果将m替换为:,则分配有效.由于某些原因,选择列的子集不会让我分配零.

I'm sure I'm missing some under-the-hood details that explains why this does not work. Interestingly, if you replace m with :, then the assignment works. For some reason, selecting a subset of the columns does not let me assign the zeros.

如果有人可以解释正在发生的事情并帮助我找到另一种解决方案(希望该解决方案不涉及生成临时的numpy数组,因为我的实际y将会非常大),那么我将不胜感激!谢谢!

If someone could explain what's going on and help me find an alternative solution (hopefully one that does not involve generating a temporary numpy array since my actual y will be really huge), I would really appreciate it! Thank you!

y[0:4,:][y[0:4,:]<0.5]=0; y[0:4,0:3][y[0:4,0:3]<0.5]=0;

所有工作都按预期进行.看来问题是当您使用某种类型的列表编制索引时.

all work as expected. It seems the issue is when you index with a list of some kind.

推荐答案

创建一个数组(由于值不同,这是我的最爱之一):

Make an array (this is one of my favorites because the values differ):

In [845]: x=np.arange(12).reshape(3,4)
In [846]: x
Out[846]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
In [847]: m=np.array([0,2,3])
In [848]: x[:,m]
Out[848]: 
array([[ 0,  2,  3],
       [ 4,  6,  7],
       [ 8, 10, 11]])
In [849]: x[:,m][:2,:]=0
In [850]: x
Out[850]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

没有变化.但是,如果我一步一步进行索引编制,它就会改变.

No change. But if I do the indexing in one step, it changes.

In [851]: x[:2,m]=0
In [852]: x
Out[852]: 
array([[ 0,  1,  0,  0],
       [ 0,  5,  0,  0],
       [ 8,  9, 10, 11]])

如果我颠倒顺序,它也可以工作:

it also works if I reverse the order:

In [853]: x[:2,:][:,m]=10
In [854]: x
Out[854]: 
array([[10,  1, 10, 10],
       [10,  5, 10, 10],
       [ 8,  9, 10, 11]])

x[i,j]作为x.__getitem__((i,j))执行. x[i,j]=v作为x.__setitem__((i,j),v).

x[i,j][k,l]=vx.__getitem__((i,j)).__setitem__((k,l),v).

set适用于get产生的值.如果get返回视图,则更改会影响x.但是,如果生成了副本,则更改不会影响x.

The set applies to the value produced by the get. If the get returns a view, then the change affects x. But if it produces a copy, the change does not affect x.

对于数组my[0:4,m]会生成一个副本(我需要证明这一点吗?). y[0:4,:]产生一个视图.

With array m, y[0:4,m] produces a copy (do I need to demonstrate that?). y[0:4,:] produces a view.

因此,简而言之,如果第一个索引生成了一个视图,则第二个索引分配有效.但是,如果产生副本,则第二个无效.

So in short, if the first indexing produces a view the second indexed assignment works. But if produces a copy, the second has no effect.

这篇关于索引索引数组时设置Numpy数组的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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