Some python_numpy's "Thriller" operation

The contact time is not long python, also looked at the most basic grammar, and almost no other language basis. Because the laboratory to do the project, and now want to implement an algorithm, which involves the transformation S, we did not find a suitable open source project can be called directly. I would like to achieve according to their own formula, according to the formula of literature, write directly if needed quadruple for loop, in order to reduce complexity, want to try to quantify. The story that is happening at this time.

Since the speech signal sub-frame, it needs to be introduced into the three-dimensional transformation matrix S, in the (N * N * nf) is operated when the index nf frame found after performing a series of operations, some fatal numpy operation, if not carefully to find out about the general will feel the pit.

Look,

C = np.array([[[1,2],[3,4],[5,6]],[[7,8,],[9,10],[11,12]],[[13,14],[15,16],[17,18]]])

operation result

C
array([[[ 1,  2],
        [ 3,  4],
        [ 5,  6]],

       [[ 7,  8],
        [ 9, 10],
        [11, 12]],

       [[13, 14],
        [15, 16],
        [17, 18]]])

To think with some test to determine when the operation can not succeed themselves algorithm (in fact, want, while giving each frame of the k-th column assignment), the following are some of the tests

D = np.zeros((3,2,3))

What do you think will be the result?

D
array([[[0., 0., 0.],
        [0., 0., 0.]],

       [[0., 0., 0.],
        [0., 0., 0.]],

       [[0., 0., 0.],
        [0., 0., 0.]]])

When the results, I did not pay attention, then carry out the operation after it (in fact, the matrix dimensions and then I want is not consistent)

D[:,1,:] = C[:,0,:]
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    D[:,1,:] = C[:,0,:]
ValueError: could not broadcast input array from shape (3,2) into shape (3,3)

? ? ? what? what happened? Then go back and view only to find the problem matrix dimension! ! !

Numpy original dimensional index of the first three-dimensional matrix of row and the number of frames ..., and finally the column

The next few tests are as follows:

>>> D = np.zeros((2,3,3))
>>> D
array([[[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]],

       [[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]]])
>>> np.zeros((3,3,2))
array([[[0., 0.],
        [0., 0.],
        [0., 0.]],

       [[0., 0.],
        [0., 0.],
        [0., 0.]],

       [[0., 0.],
        [0., 0.],
        [0., 0.]]])
>>> D[:,1,:] = C[:,0,:]
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    D[:,1,:] = C[:,0,:]
ValueError: could not broadcast input array from shape (3,2) into shape (2,3)
>>> C[:,0,:]
array([[ 1,  2],
       [ 7,  8],
       [13, 14]])
>>> E=np.zeros((3,2))
>>> E
array([[0., 0.],
       [0., 0.],
       [0., 0.]])
>>> C[:,0,:] = E
>>> C
array([[[ 0,  0],
        [ 3,  4],
        [ 5,  6]],

       [[ 0,  0],
        [ 9, 10],
        [11, 12]],

       [[ 0,  0],
        [15, 16],
        [17, 18]]])
>>> C[1,:,:]
array([[ 0,  0],
       [ 9, 10],
       [11, 12]])
>>> C[:,:,0]
array([[ 0,  3,  5],
       [ 0,  9, 11],
       [ 0, 15, 17]])        

By the matrix E on the C assignment can be seen simultaneously on a matrix corresponding to each frame of the first (corresponding to index 0) A bulk assignment;

Meanwhile, the index of the last operation C[:,:,0]result know, this is actually the first (corresponding to index 0) transposed column vector of each frame and longitudinal rows stacked (stacked in rows down) a new matrix obtained.

Released five original articles · won praise 0 · Views 86

Guess you like

Origin blog.csdn.net/a1227605575/article/details/102589921