Some instructions numpy module shaft (axis), and instructions correlation matrix stack (concatenate, hstack, vstack, dstack, stack) usage

Explain the meaning of the shaft

axis meanings: It can be seen,, numpy will be operated by different axis in different directions:

If not set, then the operation of all elements; if axis = 0, the operation is performed along the longitudinal axis; axis = 1, the operation is performed along the horizontal axis.

But this is only a simple two-digit group, if it is multi-dimensional? It can be summed up in one sentence:

Disposed axis = i, then numpy operates along the i-th marked change in the discharge.

For example just examples, and it can be expressed as:

import numpy as np
data = np.array([[1,2],[3,4]])
                            data = [
                            [a00, a01],
                            [a10, a11]
                            ],

Axis = 0 Therefore, the operation along the first subscript 0 changes direction, i.e. a00-> a10, a01-> a11,

I.e. ordinate direction, axis = 1 is similar. Here we give an example of a four-dimensional sum seek to verify:

data = np.random.randint(0, 5, [4,3,2,3])
print data

[[[[4 3 0]
   [0 4 0]]

  [[3 2 1]
   [2 0 3]]

  [[2 2 4]
   [1 2 0]]]


 [[[0 2 4]
   [2 4 0]]

  [[4 2 4]
   [0 4 0]]

  [[3 0 3]
   [4 1 1]]]


 [[[0 2 0]
   [3 3 3]]

  [[4 3 2]
   [3 2 3]]

  [[0 2 4]
   [0 3 2]]]


 [[[2 2 0]
   [0 2 2]]

  [[2 3 3]
   [1 2 4]]

  [[0 1 0]
   [1 0 1]]]]
print "axis=0"
#numpy验证第0维的方向来求和,也就是第一个元素值=a0000+a1000+a2000+a3000=11,
#第二个元素=a0001+a1001+a2001+a3001=5,同理可得最后的结果如下:
print data.sum(axis=0)
print "axis=1"
print data.sum(axis=1)
print "axis=2"
print data.sum(axis=2)
#当axis=3时,numpy验证第3维的方向来求和,也就是第一个元素值=a0000+a0001+a0002=5,
#第二个元素=a0010+a0011+a0012=7,同理可得最后的结果如下:
print "axis=3"
print data.sum(axis=3)
axis=0
[[[13  6  9]
  [ 7  4  5]]

 [[12  9 11]
  [ 6 10  1]]

 [[ 8 13 12]
  [ 9  6  6]]]
axis=1
[[[10  9  8]
  [ 5  3  4]]

 [[ 9  7 10]
  [ 5  4  2]]

 [[ 9  5 10]
  [ 8  5  3]]

 [[ 5  7  4]
  [ 4  8  3]]]
axis=2
[[[5 4 3]
  [5 1 3]
  [5 7 6]]

 [[4 1 4]
  [4 7 4]
  [6 3 4]]

 [[7 2 4]
  [5 4 3]
  [5 4 6]]

 [[4 3 3]
  [4 7 2]
  [1 5 2]]]
axis=3
[[[ 9  3]
  [ 8  1]
  [10  8]]

 [[ 5  4]
  [12  3]
  [ 9  4]]

 [[ 9  4]
  [ 6  6]
  [ 9  6]]

 [[ 5  5]
  [ 6  7]
  [ 5  3]]]

Correlation matrix stack (concatenate, hstack, vstack, dstack, stack) usage instructions

arr=[np.random.randn(2,3) for _ in range(5)]
print arr
print arr[0].shape
[array([[-0.80612405,  0.31055887, -0.442608  ],
       [-0.53218701,  1.73229849,  0.87374842]]), array([[ 2.30505457,  0.2061082 , -1.02476858],
       [ 0.01919642, -0.62611687, -0.35686779]]), array([[ 1.8560166 ,  0.61116091,  0.80456681],
       [-0.11137362, -1.22763138, -0.4134618 ]]), array([[-0.6849199 ,  0.09698213,  0.82980694],
       [-1.12956127, -0.33650792,  0.34616903]]), array([[ 1.78424831,  0.09591247, -0.19971545],
       [ 0.62067279, -1.21686687, -2.0698592 ]])]
(2, 3)

np.stack

The two new matrix stack along a specified axis up
more professional English Description:
np.stack
the Join A Sequence of Arrays Along A new new Axis.
At The Axis at The index of the Parameter specifies at The new new in at The Axis at The Dimensions of the Result.
The For example, if axis = 0 it will be the first dimension and if axis = -1 it will be the last dimension.


print "axis=0"
print np.stack(arr,axis=0)
print "previous: "+str(arr[0].shape)+" now: "+str(np.stack(arr,axis=0).shape)
print "axis=1"
print np.stack(arr,axis=1)
print "previous: "+str(arr[0].shape)+" now: "+str(np.stack(arr,axis=1).shape)
print "axis=2"
print np.stack(arr,axis=2)
print "previous: "+str(arr[0].shape)+" now: "+str(np.stack(arr,axis=2).shape)
axis=0
[[[-0.80612405  0.31055887 -0.442608  ]
  [-0.53218701  1.73229849  0.87374842]]

 [[ 2.30505457  0.2061082  -1.02476858]
  [ 0.01919642 -0.62611687 -0.35686779]]

 [[ 1.8560166   0.61116091  0.80456681]
  [-0.11137362 -1.22763138 -0.4134618 ]]

 [[-0.6849199   0.09698213  0.82980694]
  [-1.12956127 -0.33650792  0.34616903]]

 [[ 1.78424831  0.09591247 -0.19971545]
  [ 0.62067279 -1.21686687 -2.0698592 ]]]
previous: (2, 3) now: (5, 2, 3)
axis=1
[[[-0.80612405  0.31055887 -0.442608  ]
  [ 2.30505457  0.2061082  -1.02476858]
  [ 1.8560166   0.61116091  0.80456681]
  [-0.6849199   0.09698213  0.82980694]
  [ 1.78424831  0.09591247 -0.19971545]]

 [[-0.53218701  1.73229849  0.87374842]
  [ 0.01919642 -0.62611687 -0.35686779]
  [-0.11137362 -1.22763138 -0.4134618 ]
  [-1.12956127 -0.33650792  0.34616903]
  [ 0.62067279 -1.21686687 -2.0698592 ]]]
previous: (2, 3) now: (2, 5, 3)
axis=2
[[[-0.80612405  2.30505457  1.8560166  -0.6849199   1.78424831]
  [ 0.31055887  0.2061082   0.61116091  0.09698213  0.09591247]
  [-0.442608   -1.02476858  0.80456681  0.82980694 -0.19971545]]

 [[-0.53218701  0.01919642 -0.11137362 -1.12956127  0.62067279]
  [ 1.73229849 -0.62611687 -1.22763138 -0.33650792 -1.21686687]
  [ 0.87374842 -0.35686779 -0.4134618   0.34616903 -2.0698592 ]]]
previous: (2, 3) now: (2, 3, 5)

np.hstack, np.vstack, np.dstack

  • np.hstack
    the two matrices in the horizontal direction (axis line is located) piled up Stack arrays in sequence horizontally (column wise ),
    equivalent to np.concatenate (arr, axis = 1)
  • np.vstack
    matrix are stacked along the vertical direction, Stack arrays in sequence vertically (row wise)
    is equivalent to np.concatenate (arr, axis = 0)
  • np.dstack
    matrix are stacked in the depth direction, i.e., the third axis Stack arrays in sequence depth wise (along third dimension)

print np.hstack(arr)
print "previous: "+str(arr[0].shape)+" now: "+str(np.hstack(arr).shape)
print np.vstack(arr)
print "previous: "+str(arr[0].shape)+" now: "+str(np.vstack(arr).shape)
print np.dstack(arr)
print "previous: "+str(arr[0].shape)+" now: "+str(np.dstack(arr).shape)
[[-0.80612405  0.31055887 -0.442608    2.30505457  0.2061082  -1.02476858
   1.8560166   0.61116091  0.80456681 -0.6849199   0.09698213  0.82980694
   1.78424831  0.09591247 -0.19971545]
 [-0.53218701  1.73229849  0.87374842  0.01919642 -0.62611687 -0.35686779
  -0.11137362 -1.22763138 -0.4134618  -1.12956127 -0.33650792  0.34616903
   0.62067279 -1.21686687 -2.0698592 ]]
previous: (2, 3) now: (2, 15)
[[-0.80612405  0.31055887 -0.442608  ]
 [-0.53218701  1.73229849  0.87374842]
 [ 2.30505457  0.2061082  -1.02476858]
 [ 0.01919642 -0.62611687 -0.35686779]
 [ 1.8560166   0.61116091  0.80456681]
 [-0.11137362 -1.22763138 -0.4134618 ]
 [-0.6849199   0.09698213  0.82980694]
 [-1.12956127 -0.33650792  0.34616903]
 [ 1.78424831  0.09591247 -0.19971545]
 [ 0.62067279 -1.21686687 -2.0698592 ]]
previous: (2, 3) now: (10, 3)
[[[-0.80612405  2.30505457  1.8560166  -0.6849199   1.78424831]
  [ 0.31055887  0.2061082   0.61116091  0.09698213  0.09591247]
  [-0.442608   -1.02476858  0.80456681  0.82980694 -0.19971545]]

 [[-0.53218701  0.01919642 -0.11137362 -1.12956127  0.62067279]
  [ 1.73229849 -0.62611687 -1.22763138 -0.33650792 -1.21686687]
  [ 0.87374842 -0.35686779 -0.4134618   0.34616903 -2.0698592 ]]]
previous: (2, 3) now: (2, 3, 5)

np.concatenate usage

Matrix along several different axes stack up Join a sequence of arrays along an existing axis.


print "axis=0"
print np.concatenate(arr,axis=0)
print "previous: "+str(arr[0].shape)+" now: "+str(np.concatenate(arr,axis=0).shape)
print "axis=1"
print np.concatenate(arr,axis=1)
print "previous: "+str(arr[0].shape)+" now: "+str(np.concatenate(arr,axis=1).shape)

axis=0
[[-0.80612405  0.31055887 -0.442608  ]
 [-0.53218701  1.73229849  0.87374842]
 [ 2.30505457  0.2061082  -1.02476858]
 [ 0.01919642 -0.62611687 -0.35686779]
 [ 1.8560166   0.61116091  0.80456681]
 [-0.11137362 -1.22763138 -0.4134618 ]
 [-0.6849199   0.09698213  0.82980694]
 [-1.12956127 -0.33650792  0.34616903]
 [ 1.78424831  0.09591247 -0.19971545]
 [ 0.62067279 -1.21686687 -2.0698592 ]]
previous: (2, 3) now: (10, 3)
axis=1
[[-0.80612405  0.31055887 -0.442608    2.30505457  0.2061082  -1.02476858
   1.8560166   0.61116091  0.80456681 -0.6849199   0.09698213  0.82980694
   1.78424831  0.09591247 -0.19971545]
 [-0.53218701  1.73229849  0.87374842  0.01919642 -0.62611687 -0.35686779
  -0.11137362 -1.22763138 -0.4134618  -1.12956127 -0.33650792  0.34616903
   0.62067279 -1.21686687 -2.0698592 ]]
previous: (2, 3) now: (2, 15)
Published 36 original articles · won praise 0 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_38102912/article/details/101563798