Thursday 15 May 2014

python - Vectorizing a numpy array call of varying indices -


I have a 2D numerical array and a list of index lists for which I want to calculate the respective 1D vectors With oval array it can be easily done for loop or through the understanding of the list, but I wonder if it is possible to vector it with the same code I get about 40x speed-up from vectoring I

Here is the sample code:

  A Import samples as P index = [[1,2], [1,3], [2,0,3], [1]] Array_2d = np.array ([[0.5, 1.5], [1.5] , 2.5], [2.5,3.5], [3.54.5]] soln = [np.sum (array_2d [x], axis = -1 for x in index)   

( Edit): Note that the index array is not coordinate (x, y) for 2D, instead of index [0] = [1,2] and array 2d in the second vector (rows) index of the elements of each list The number variable can be.

This is what I need to be able to do. Do I:

  vectorized_soln = np.sum (array_2d [index [:]], axis = -1) Can anyone know if any methods of achieving this Are you Firstly, I think that you have a typo in the third element of the index ...  

The easiest way to do this is by sub- Hey is making two arrays of index:

  i = np.array ([1,1,2]) j = np.array ([2,3,?]) Sub_arr2d = Array_2d [i, j]   

And finally, you can take the sum of sub-essence 2D ...

No comments:

Post a Comment