python - Numpy: increment elements of an array given the indices required to increment -


I am trying to turn a second order tensor into the binary third order tensor Amxn numpy array as a second order Looking at the sensor: A., I have to take every element value: replace it with x, A and it with a vector: v, with dimensions equal to the maximum value of A, but with an increase of 1 on the value of X X (i.e. v [x] = 1) I am following this question: which is a 2-dimensional coordinates Creates an array with increments in the given index. I am reading the answers and NP Trying to use travel_multi_index () and np.bincount (), but with 3 dimensional coordinates, although I continue to have a ValueError: "Invalid entry in the coordinate array". This is what I am using:

  def expand_to_tensor_3 (array): (x, y) = array.shape (a, b) = np.indices ((x, y) ) A = A.reshape (x * y) b = b.reshape (x * y) tensor_3 = np.bincount (np.ravel_multi_index ((a, b, array.reshape (x * y)), (x, y , NP) If you know what's wrong here or know a better way to accomplish your goal, then both will be really useful, thanks.    

>

you (A [:,:, np.newaxis] == np.arange (A. ( ). Astype (int) .

This is a demonstration:

  in [52]: A out [52]: array ( [[2, 0, 0, 2] [3], 1, 2, 3], [3, 2, 1, 0]]) in [53]: B = (A [:,:, NP. [54]: B Out [54]: Array ([[0, 0, 1, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0]], [[0, 0, 0 , 1], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], [[0, 0, 0, 1], [0, 0 , 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]]])   

< 55]: A [0,0] outside [55]: 2 in [56]: B [0,0,:] Out [56]: [[0, 0, 1, 0]) in [57]: A [1,3] Out [57]: 3 in [58]: B [1,3,:] Out [58]: Array ([0, 0, 0, 1]) < P> expression a [:,:, np.newaxis] == np.arange (A.max (+ 1) from each code's a to np Uses to compare .arange (amx (+ 1) + 1) Yes. For a value, it looks like this:

 in  [63]: 3 == N.P. Ranges (AMX) + 1) Out [63]: array ([false, false, false , True], dtype = bool [64]: (3 == np.range (amx (+). Volatile (int) out [64]: array ([0, 0, 0, 1]) < / Code>  

A [:,:, np.newaxis] is a three-dimensional view of A with size (3 , 4,1) . Additional dimensions are added so that the comparison of np.arange (AMX + 1) is broadcast on each entry, resulting in size (3, 4 , A. Max () + 1) .

With a trivial change, it will work for a n-dimensional array. Eelipisis ... means "all other dimensions" With a numpy array.

  (A [..., np.Newxis] == np.range (amx (+). Esplex (int)  < / Pre> 

converts a n-dimensional array into a (n + 1) -dimensional array, where the final dimension is the binary indicator of the integer in A . Here is a one-dimensional array There is an example with:

  in [6]: a = np.array ([3, 4, 0, 1]) in [7]: (a [. .., np.newaxis] == np.arange (a.max () + 1). Astype (int) out [7]: array ([, 0, 0, 1, 0], [0, 0, 0, 0, 1], [1, 0, 0, 0, 0], [0, 1] , 0, 0, 0]])    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -