【Numpy】np.c_和np.r_的用法

numpy.c_() and numpy.r_()的用法

np.r_是按列连接两个矩阵,就是把两矩阵上下相加,要求列数相等。

np.c_是按行连接两个矩阵,就是把两矩阵左右相加,要求行数相等。

Demo:

1.numpy.c_:

  import numpy as np
 
 x = np.arange(12).reshape(3,4)
 print('x:',x, x.shape)
 
 y = np.arange(10,22).reshape(3,4)
 print('y:',y, y.shape)
 
 z = np.c_[x,y]
 print('z:',z, z.shape)

result:

 x: [[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]] (3, 4)
 y: [[10 11 12 13]
  [14 15 16 17]
  [18 19 20 21]] (3, 4)
 z: [[ 0  1  2  3 10 11 12 13]
  [ 4  5  6  7 14 15 16 17]
  [ 8  9 10 11 18 19 20 21]] (3, 8)

2.numpy.r_用法:

 import numpy as np
 
 x = np.arange(12).reshape(3,4)
 print('x:',x, x.shape)
 
 y = np.arange(10,22).reshape(3,4)
 print('y:',y, y.shape)
 
 z = np.r_[x,y]
 print('z:',z, z.shape)

result:

x: [[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]] (3, 4)
 y: [[10 11 12 13]
  [14 15 16 17]
  [18 19 20 21]] (3, 4)
 z: [[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]
  [10 11 12 13]
  [14 15 16 17]
  [18 19 20 21]] (6, 4)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容