简体   繁体   English

如何在没有循环的情况下在特定位置修改2D numpy数组?

[英]how to modify a 2D numpy array at specific locations without a loop?

I have a 2D numpy array and I have a arrays of rows and columns which should be set to a particular value. 我有一个2D numpy数组,我有一个行和列的数组,应该设置为一个特定的值。 Lets consider the following example 让我们考虑以下示例

 a = array([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]])

I want to modify entries at rows [0,2] and columns [1,2]. 我想修改行[0,2]和列[1,2]的条目。 This should result in the following array 这应该导致以下数组

 a = array([[1, 2, 0],
           [4, 5, 0],
           [7, 8, 9]])

I did following and it resulted in modifying each sequence of column in every row 我做了以下操作,结果修改了每一行中的每个列序列

rows = [0,1]
cols = [2,2]
b=a[numpy.ix_(rows,columns)]

It resulted in the following array modifying every column of the specified array 它导致以下数组修改指定数组的每一列

array([[1, 0, 0],
       [4, 5, 6],
       [7, 0, 0]])

Some one could please let me know how to do it? 有人可以让我知道怎么做吗?

Thanks a lot 非常感谢

EDIT: It is to be noted that rows and columns coincidently happend to be sequentia. 编辑:需要注意的是,行和列恰好发生在顺序上。 The actual point is that these could be arbitrary and in any order. 实际的一点是,这些可以是任意的,也可以是任何顺序。 if it is rows = [a,b,c] and cols=[nxz] then I want to modify exactly three elements at locations (a,n),(b,x),(c,z). 如果是rows = [a,b,c]和cols = [nxz],那么我想在位置(a,n),(b,x),(c,z)处准确修改三个元素。

Adding to what others have said, you can modify these elements using fancy indexing as follows: 添加其他人所说的内容,您可以使用花式索引修改这些元素,如下所示:

In [39]: rows = [0,1]

In [40]: cols = [2,2]

In [41]: a = np.arange(1,10).reshape((3,3))

In [42]: a[rows,cols] = 0

In [43]: a
Out[43]: 
array([[1, 2, 0],
       [4, 5, 0],
       [7, 8, 9]])

You might want to read the documentation on indexing multidimensional arrays: http://docs.scipy.org/doc/numpy/user/basics.indexing.html#indexing-multi-dimensional-arrays 您可能希望阅读有关索引多维数组的文档: http//docs.scipy.org/doc/numpy/user/basics.indexing.html#indexing-multi-dimensional-arrays

The key point is: 关键点是:

if the index arrays have a matching shape, and there is an index array for each dimension of the array being indexed, the resultant array has the same shape as the index arrays, and the values correspond to the index set for each position in the index arrays. 如果索引数组具有匹配的形状,并且索引数组的每个维度都有一个索引数组,则结果数组具有与索引数组相同的形状,并且值对应于索引中每个位置的索引集阵列。

Importantly this also allows you to do things like: 重要的是,这也允许您执行以下操作:

In [60]: a[rows,cols] = np.array([33,77])

In [61]: a
Out[61]: 
array([[ 1,  2, 33],
       [ 4,  5, 77],
       [ 7,  8,  9]])

where you can set each element independently using another array, list or tuple of the same size. 您可以使用另一个相同大小的数组,列表或元组独立设置每个元素。

one work around: ndarray.flatten, np.put(), ndarray.reshape 一个解决方法: ndarray.flatten, np.put(), ndarray.reshape

try ndarray.flatten(array) , that way you are dealing with a one dim array which can be manipulated with numpy.put(array,[indices],[values]) . 尝试ndarray.flatten(array) ,这样你就可以处理一个可以用numpy.put(array,[indices],[values])操作的dim数组。 Then use ndarray.reshape() to get to the original dimensions. 然后使用ndarray.reshape()来获取原始尺寸。

First off, your description of the "correct" array doesn't match the columns and rows you specify... 首先,您对“正确”数组的描述与您指定的列和行不匹配...

To get your "correct" array, you'd do this: 要获得“正确”的数组,您可以这样做:

a[:2, 2] = 0

To modify the second and third columns of the first and third rows, (rows [0,2] and columns [1,2]) you'd do what you're doing... (Your description of modifying rows [0,2] and columns [1,2] is exactly the result you get, right?) 要修改第一行和第三行的第二列和第三列(行[0,2]和列[1,2]),你要做你正在做的事情......(你修改行的描述[0, 2]和列[1,2]正是你得到的结果,对吧?)

It should be as simple as a[0,2]=0 and a[1,2]=0. 它应该像[0,2] = 0和[1,2] = 0一样简单。 You could also do a[0:2,2]=0. 你也可以做[0:2,2] = 0。 The ':' based range indexing in python is a half-open interval [0,2) which actually range from 0 to 1 (the end point of 2 is not included). python中基于':'的范围索引是半开区间[0,2],其实际范围从0到1(不包括2的结束点)。

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

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