np.concatenate () instructions

np.concatenate array is a function of the splicing numpy using the following method:

import numpy as np

x1 = np.random.normal(1,1,(5,4))
x2 = np.random.normal(1,1,(3,4))

print(x1)
print(x1.shape)
print(x2)
print(x2.shape)

con = np.concatenate([x1,x2],axis=0)
print(con)
print(con.shape)

输出结果为:
[[ 2.22806658  0.15277615  2.21245262  1.63831116]
 [ 1.30131232 -1.09226289 -0.65959394  1.16066688]
 [ 1.52737722  0.84587186  1.53041503  0.4584277 ]
 [ 1.56096219  1.29506244  3.08048523  2.06008988]
 [ 1.79964236  0.95087117  1.30845477 -0.2644263 ]]
(5, 4)
[[0.89383392 1.49502055 2.90571116 1.71943997]
 [1.44451535 1.87838383 1.4763242  0.82597179]
 [0.72629108 1.42406398 1.35519112 0.58121617]]
(3, 4)
[[ 2.22806658  0.15277615  2.21245262  1.63831116]
 [ 1.30131232 -1.09226289 -0.65959394  1.16066688]
 [ 1.52737722  0.84587186  1.53041503  0.4584277 ]
 [ 1.56096219  1.29506244  3.08048523  2.06008988]
 [ 1.79964236  0.95087117  1.30845477 -0.2644263 ]
 [ 0.89383392  1.49502055  2.90571116  1.71943997]
 [ 1.44451535  1.87838383  1.4763242   0.82597179]
 [ 0.72629108  1.42406398  1.35519112  0.58121617]]
(8, 4)

axis in accordance with the specified parameter which dimension spliced, x1 is the example described above [5,4] x2 is [3,4], disposed axis = 0 represents the splicing in accordance with a first dimension, the size of the splice to [8 , 4] except that the size of the first dimension is changed, other dimensions the same, but also shows, must ensure that the other dimensions of the size of the can, if x1 is [5,4], x2 is [5,3] here provided if axis = 1, then, an error is reported, as a second dimension x1 and x2 are not equal size, it can not be spliced.

Splicing according axis dimension = 1, examples are as follows:

import numpy as np

x1 = np.random.normal(1,1,(5,4))
x2 = np.random.normal(1,1,(5,2))

print(x1)
print(x1.shape)
print(x2)
print(x2.shape)

con = np.concatenate([x1,x2],axis=1)
print(con)
print(con.shape)

输出结果如下:
[[ 1.06700795  2.49432822  0.13721596  0.86647501]
 [-0.24454185  0.83414428  2.06012125 -0.63322426]
 [ 2.01993142 -0.27599932  1.9101389   1.92564214]
 [ 0.12627442  0.97560762  2.00993226  2.02754602]
 [ 0.23883256  1.4805339  -0.83029287  1.37207756]]
(5, 4)
[[ 0.67988459  2.46464482]
 [ 1.19166015  2.16522311]
 [ 1.41193468 -0.01165058]
 [ 0.62496307  1.05706225]
 [ 0.85055712 -0.09588572]]
(5, 2)
[[ 1.06700795  2.49432822  0.13721596  0.86647501  0.67988459  2.46464482]
 [-0.24454185  0.83414428  2.06012125 -0.63322426  1.19166015  2.16522311]
 [ 2.01993142 -0.27599932  1.9101389   1.92564214  1.41193468 -0.01165058]
 [ 0.12627442  0.97560762  2.00993226  2.02754602  0.62496307  1.05706225]
 [ 0.23883256  1.4805339  -0.83029287  1.37207756  0.85055712 -0.09588572]]
(5, 6)

In this example x1 is [5,4], x2 of [5,2] is spliced ​​according axis = 1, the size of the stitched [5,6]

 

Published 36 original articles · won praise 11 · views 6527

Guess you like

Origin blog.csdn.net/t20134297/article/details/105006864