1

Topic: in multidimentional numpy-files

All greetings! A question amateurish, do not kick strongly. At first a simple example. Here there is at me a two-dimensional array, for example A = np.array ([[1, 2, 3], [4, 5, 6]]) and is an one-dimensional array of indexes of elements in lines, we tell B = np.array ([2, 0]) I want to receive the one-dimensional array consisting of elements with these indexes in appropriate lines, i.e. [3, 4] In this two-dimensional case I can write a C = A [np.arange (2), B] and to receive that is required. And now the task hardly becomes complicated... Now A is a 3-dimensional array, say, 3x3x100, B - a 2-dimensional array of indexes 3x3. And I want to receive on an output a 2-dimensional array of a C, 3x3, with elements A in which specifies B. I.e. I need to make the such: for i in range (3): for j in range (3): the C [i, j] = A [i, j, B [i, j]] Is a method to make it more elegantly and faster?

2

Re: in multidimentional numpy-files

Hello, kfmn, you wrote: K> And now the task hardly becomes complicated... Now A is a 3-dimensional array, say, 3x3x100, B - a 2-dimensional array of indexes 3x3. And I want to receive on an output a 2-dimensional array of a C, 3x3, with elements A in which specifies B. K> I.e. me it is necessary to make the such: K> K> for i in range (3): K> for j in range (3): K> the C [i, j] = A [i, j, B [i, j]] K> K> Is a method to make it more elegantly and faster? http://stackoverflow.com/questions/4257 … -an-nxn-ar

3

Re: in multidimentional numpy-files

Hello, c-smile, you wrote: CS> Hello, kfmn, you wrote: K>> And now the task hardly becomes complicated... Now A is a 3-dimensional array, say, 3x3x100, B - a 2-dimensional array of indexes 3x3. And I want to receive on an output a 2-dimensional array of a C, 3x3, with elements A in which specifies B. K>> I.e. me it is necessary to make the such: K>> K>> for i in range (3): K>> for j in range (3): K>> the C [i, j] = A [i, j, B [i, j]] K>> K>> Is a method to make it more elegantly and faster? CS> http://stackoverflow.com/questions/4257 … -an-nxn-ar thanks, but are other task. And those methods which there are offered as it seems to me, for my task directly do not approach.

4

Re: in multidimentional numpy-files

Hello, kfmn, you wrote: K> There is a method to make it more elegantly and faster? Not especially elegantly, but, at least, not a cycle: a C = A.reshape (B.size, A.shape [-1]) [numpy.arange (B.size), B.ravel ()].reshape (B.shape)

5

Re: in multidimentional numpy-files

Hello, kfmn, you wrote: K> And now the task hardly becomes complicated... Now A is a 3-dimensional array, say, 3x3x100, B - a 2-dimensional array of indexes 3x3. And I want to receive on an output a 2-dimensional array of a C, 3x3, with elements A in which specifies B. K> I.e. me it is necessary to make the such: K> K> for i in range (3): K> for j in range (3): K> the C [i, j] = A [i, j, B [i, j]] K> K> Is a method to make it more elegantly and faster? All indexes in A [i, j, B []] should be in the size 3x3 (not only B). I.e. to set appropriate arrays for indexes i and j A = np.arange (54).reshape reshape (3,3,6) array ([[[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17]], [[18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35]], [[36, 37, 38, 39, 40, 41], [42, 43, 44, 45, 46, 47], [48, 49, 50, 51, 52, 53]]]) # idx1 = i idx1 = np.tile (np.arange (3), (3, 1)) array ([[0, 1, 2], [0, 1, 2], [0, 1, 2]]) # idx2 = j idx2 = idx1.T array ([[0, 0, 0], [1, 1, 1], [2, 2, 2]]) # idx3 = B idx3 = np.zeros ((3,3), dtype=int) array ([[0, 0, 0]

6

Re: in multidimentional numpy-files

Hello, the Corkcrew, you wrote: Hello, kfmn, you wrote: K>> And now the task hardly becomes complicated... Now A is a 3-dimensional array, say, 3x3x100, B - a 2-dimensional array of indexes 3x3. And I want to receive on an output a 2-dimensional array of a C, 3x3, with elements A in which specifies B. K>> I.e. me it is necessary to make the such: K>> K>> for i in range (3): K>> for j in range (3): K>> the C [i, j] = A [i, j, B [i, j]] K>> K>> Is a method to make it more elegantly and faster? Here so, in general: idx = np.tile (np.arange (3), (3, 1)) With = A [idx, idx. T, B]

7

Re: in multidimentional numpy-files

Hello, the Corkcrew, you wrote: Thanks! I will try! Hello, the Corkcrew, you wrote:> Hello, kfmn, you wrote: K>>> And now the task hardly becomes complicated... Now A is a 3-dimensional array, say, 3x3x100, B - a 2-dimensional array of indexes 3x3. And I want to receive on an output a 2-dimensional array of a C, 3x3, with elements A in which specifies B. K>>> I.e. me it is necessary to make the such: K>>> K>>> for i in range (3): K>>> for j in range (3): K>>> the C [i, j] = A [i, j, B [i, j]] K>>> K>>> Is a method to make it more elegantly and faster?> Here so, in general: idx = np.tile (np.arange (3), (3, 1)) With = A [idx, idx. T, B]