简体   繁体   English

numpy中2d数组的选定元素的矢量化赋值语句

[英]vectorized assignment statement for selected elements of 2d array in numpy

I am a beginner in python. 我是python的初学者。 I was wondering if there is a "good" way to do this operation without using for loops. 我想知道是否有一种不使用for循环的“好”方式来执行此操作。 Consider the problem 考虑问题

u = zeros((4,2))
u_pres = array([100,200,300])
row_col_index = array([[0,0,2], [0,1,1]])

I want to assign u[0,0], u[0,1], and u[2,1] as 100,200 and 300 respectively. 我想将u [0,0],u [0,1]和u [2,1]分别指定为100,200和300。 I wanted to do something of the form 我想做某种形式的事情

u[row_col_index] = u_pres

If u were a 1d array such an assignment works, but am unable to figure out how to make this work for 2d arrays. 如果u是一维数组,则这样的分配有效,但无法弄清楚如何使此数组适用于二维数组。 Your suggestions will be most helpful. 您的建议将最有帮助。 Thanks 谢谢

You're almost there. 你快到了。

What you need is the following: 您需要以下内容:

u[row_col_index[0], row_col_index[1]] = u_pres

Explanation: 说明:

Since you say you're a beginner in Python (I'm too!), I thought I might tell you this; 既然您说您是Python的初学者(我也是!),我想我可能会告诉您这一点。 it is considered unpythonic to load a module the way you did: 以这种方式加载模块被认为是非Python的

#BAD
from numpy import *
#GOOD
from numpy import array #or whatever it is you need
#GOOD
import numpy as np #if you need lots of things, this is better

Explanation: 说明:

In [18]: u = np.zeros(10)

In [19]: u
Out[19]: array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

#1D assignment
In [20]: u[0] = 1

In [21]: u[1] = 10

In [22]: u[-1] = 9 #last element

In [23]: u[-2] = np.pi #second last element

In [24]: u
Out[24]: 
array([  1.        ,  10.        ,   0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
         3.14159265,   9.        ])

In [25]: u.shape
Out[25]: (10,)

In [27]: u[9] #calling
Out[27]: 9.0

#2D case
In [28]: y = np.zeros((4,2))

In [29]: y
Out[29]: 
array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])

In [30]: y[1] = 10 #this will assign all the second row to be 10

In [31]: y
Out[31]: 
array([[  0.,   0.],
       [ 10.,  10.],
       [  0.,   0.],
       [  0.,   0.]])

In [32]: y[0,1] = 9 #now this is 2D assignment, we use 2 indices!

In [33]: y[3] = np.pi #all 4th row, similar to y[3,:], ':' means all

In [34]: y[2,1] #3rd row, 2nd column
Out[34]: 0.0


In [36]: y[2,1] = 7

In [37]: 

In [37]: y
Out[37]: 
array([[  0.        ,   9.        ],
       [ 10.        ,  10.        ],
       [  0.        ,   7.        ],
       [  3.14159265,   3.14159265]])

In your case, we had the 1st array of the row_col_index ( row_col_index[0] ) to be used for the rows and the 2nd array ( row_col_index[1] ) to be used for the columns. 在您的情况下,我们将row_col_indexrow_col_index[0] )的第一个数组用于 ,将第二个数组( row_col_index[1] )用于列。

Finally, if you're not using ipython , I suggest you do, it will assist you in the learning process and in many other things. 最后,如果您不使用ipython ,我建议您这样做,它将在学习过程和其他许多方面帮助您。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM