36

I need to do some analysis on a large dataset from a hydrolgeology field work. I am using NumPy. I want to know how I can:

  1. multiply e.g. the 2nd column of my array by a number (e.g. 5.2). And then

  2. calculate the cumulative sum of the numbers in that column.

As I mentioned I only want to work on a specific column and not the whole array.

3 Answers 3

44
 you can do this in two simple steps using NumPy:

>>> # multiply column 2 of the 2D array, A, by 5.2
>>> A[:,1] *= 5.2

>>> # assuming by 'cumulative sum' you meant the 'reduced' sum:
>>> A[:,1].sum()

>>> # if in fact you want the cumulative sum (ie, returns a new column)
>>> # then do this for the second step instead:
>>> NP.cumsum(A[:,1])

with some mocked data:

>>> A = NP.random.rand(8, 5)
>>> A
  array([[ 0.893,  0.824,  0.438,  0.284,  0.892],
         [ 0.534,  0.11 ,  0.409,  0.555,  0.96 ],
         [ 0.671,  0.817,  0.636,  0.522,  0.867],
         [ 0.752,  0.688,  0.142,  0.793,  0.716],
         [ 0.276,  0.818,  0.904,  0.767,  0.443],
         [ 0.57 ,  0.159,  0.144,  0.439,  0.747],
         [ 0.705,  0.793,  0.575,  0.507,  0.956],
         [ 0.322,  0.713,  0.963,  0.037,  0.509]])

>>> A[:,1] *= 5.2

>>> A
  array([[ 0.893,  4.287,  0.438,  0.284,  0.892],
         [ 0.534,  0.571,  0.409,  0.555,  0.96 ],
         [ 0.671,  4.25 ,  0.636,  0.522,  0.867],
         [ 0.752,  3.576,  0.142,  0.793,  0.716],
         [ 0.276,  4.255,  0.904,  0.767,  0.443],
         [ 0.57 ,  0.827,  0.144,  0.439,  0.747],
         [ 0.705,  4.122,  0.575,  0.507,  0.956],
         [ 0.322,  3.71 ,  0.963,  0.037,  0.509]])

>>> A[:,1].sum()
  25.596156138451427

just a few simple rules are required to grok element selection (indexing) in NumPy:

  • NumPy, like Python, is 0-based, so eg, the "1" below refers to the second column

  • commas separate the dimensions inside the brackets, so [rows, columns], eg, A[2,3] means the item ("cell") at row three, column four

  • a colon means all of the elements along that dimension, eg, A[:,1] creates a view of A's column 2; A[3,:] refers to the fourth row

Sign up to request clarification or add additional context in comments.

2 Comments

@Mary Jane : you're welcome. (great short tutorial, by the way:scipy.org/Cookbook/BuildingArrays) If you found one of the Answers more helpful than others, please 'Accept' that Answer by clicking the 'checkmark', visible when you mouse-over each user's score.
when you want to do the same thing for division you need to use // instead of / - flies away
8

Sure:

import numpy as np
# Let a be some 2d array; here we just use dummy data 
# to illustrate the method
a = np.ones((10,5))
# Multiply just the 2nd column by 5.2 in-place
a[:,1] *= 5.2

# Now get the cumulative sum of just that column
csum = np.cumsum(a[:,1])

If you don't want to do this in-place you would need a slightly different strategy:

b = 5.2*a[:,1]
csum = np.cumsum(b)

1 Comment

The second snippet (b = 5.2*a[:,1]) won't work as expected because a[:,1] is a one -dim array. To preserve values from first column you can do something like this: b = a*np.array([1, 5.2])
0

To multiply a constant with a specific column or row:

import numpy as np;
X=np.ones(shape=(10,10),dtype=np.float64);
X;

### this is our default matrix
array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],
   [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])



 ## now say we want to multiple it with 10

 X=X*10;

array([[10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.],
   [10., 10., 10., 10., 10., 10., 10., 10., 10., 10.]])

### Now if, we want to mulitply 3,5, 7 column with 5

X[:,[3,5,7]]=X[:,[3,5,7]]*5

 array([[10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.],
   [10., 10., 10., 50., 10., 50., 10., 50., 10., 10.]])

Similarly, we can do it for any columns. Hope it clarifies.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.