1

So I have an array of 5 integers v and another of 10 integers v. I have a 5 by 10 matrix P that I would want to fill so that (P)ij = v[i] + u[j]

I tried:

P = np.empty((len(asset_grid),len(asset_grid)))
for i in range(asset_grid):
    for j in range(asset_grid):
        P[i,j] = asset_grid[i] + asset_grid[j]

but it gives me an error

TypeError: only integer arrays with one element can be converted to an index

How should I be able to do this in Python. I apologize if my approach is too naive, I am used to Matlab and now slowly learning Python. Any help is appreciated.

Netwave
  • 40,134
  • 6
  • 50
  • 93
mathemagician
  • 173
  • 1
  • 11

1 Answers1

2

Broadcasting is what you want to do. Although for small arrays such as yours, it doesn't make a difference, it makes a significant difference with larger arrays:

>>> arr1 = np.arange(5)
>>> arr2 = np.arange(10,20)
>>> arr1[:,None] + arr2
array([[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
       [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
       [12, 13, 14, 15, 16, 17, 18, 19, 20, 21],
       [13, 14, 15, 16, 17, 18, 19, 20, 21, 22],
       [14, 15, 16, 17, 18, 19, 20, 21, 22, 23]])

Generally with numpy you want to avoid iteration over rows and columns and use vectorized/broadcasted operations. This is where speed improvements actually come from.

So, elaborating based on your comment:

Say P_ij is ith element of x raised to the 4th power minus jth element of y raised to 2nd power

In general, Python supports most arithmetical operations you would want in a vectorized way, using the usual Python operators:

>>> arr1[:, None]**4 - arr2**2
array([[-100, -121, -144, -169, -196, -225, -256, -289, -324, -361],
       [ -99, -120, -143, -168, -195, -224, -255, -288, -323, -360],
       [ -84, -105, -128, -153, -180, -209, -240, -273, -308, -345],
       [ -19,  -40,  -63,  -88, -115, -144, -175, -208, -243, -280],
       [ 156,  135,  112,   87,   60,   31,    0,  -33,  -68, -105]])
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • Great. What if instead of adding I wanted to do some other kind of operation. Say P_ij is ith element of x raised to the 4th power minus jth element of y raised to 2nd power – mathemagician Aug 30 '17 at 18:22
  • 1
    @mathemagician added an example. You should start practicing vectorizing your code. It's not always straight-forward, but for these simple examples it's easy as pie. You can always ask a question if a particular attempt stumps you - we've got a couple of `numpy` magicians on here... – juanpa.arrivillaga Aug 30 '17 at 18:26
  • Haha, awesome. What if my question was about filling a 3d matrix with say 4th power of ith element of x minus 3rd power of jth element of y plus 2nd power of lth element of z. Does this vectorization strategy generalize? – mathemagician Aug 30 '17 at 18:31
  • @mathemagician probably a good time to [check out the docs](https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html). And [this](http://scipy.github.io/old-wiki/pages/EricsBroadcastingDoc) link is pretty good. – juanpa.arrivillaga Aug 30 '17 at 18:32
  • I tried arr1[:,:,None]*arr2[:,None]*arr3 and it didn't work. Am I able to go beyond 2 dimensions with this technique? – mathemagician Aug 30 '17 at 18:42
  • @mathemagician "it didn't work" isn't an adequately specified problem statement. Regardless, this sounds like you should ask another question. Please see [ask] first though... but yes, broadcasting extends into higher dimensions... – juanpa.arrivillaga Aug 30 '17 at 18:55