How to fill a matrix in Python using iteration ove

2019-07-29 16:41发布

问题:

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.

回答1:

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]])