Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy: operations on columns (or rows) of NxM array

This may be a silly question, but I've just started using numpy and I have to figure out how to perform some simple operations.

Suppose that I have the 2x3 array

array([[1, 3, 5],
   [2, 4, 6]])

And that I want to perform some operation on the first column, for example subtract 1 to all the elements to get

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

How can I perform such an operation?

like image 829
valerio Avatar asked Sep 12 '25 20:09

valerio


1 Answers

arr
# array([[1, 3, 5],
#        [2, 4, 6]])

arr[:,0] = arr[:,0] - 1     # choose the first column here, subtract one and 
                            # assign it back to the same column

arr
# array([[0, 3, 5],
#        [1, 4, 6]])
like image 117
Psidom Avatar answered Sep 14 '25 08:09

Psidom