Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

numpy

I have a 2D numpy array and I have a arrays of rows and columns which should be set to a particular value. 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]. 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=[n x z] then I want to modify exactly three elements at locations (a,n),(b,x),(c,z).

like image 389
Shan Avatar asked Oct 13 '11 22:10

Shan


People also ask

Can a NumPy array be modified?

There are different ways to change the dimension of an array. Reshape function is commonly used to modify the shape and thus the dimension of an array.

How do you reshape an array to a 2D array in python?

NumPy reshape() function is used to change the dimensions of the array, for example, 1-D to 2-D array, 2-D to 1-D array without changing the data. For instance, np. reshape(arr,(-1,3)) changes the 1-D array to 2-D array.


2 Answers

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

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.

like image 167
JoshAdel Avatar answered Sep 17 '22 17:09

JoshAdel


one work around: 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]). Then use ndarray.reshape() to get to the original dimensions.

like image 42
0-_-0 Avatar answered Sep 19 '22 17:09

0-_-0