np.c_ 用于连接两个矩阵

在这里插入图片描述

np.c,是 np.r_['-1,2,0', index expression] 的一种简写,沿着矩阵的第二个轴拼接,对于2维矩阵,就是按照列吧, column(列)的缩写,就是按列叠加两个矩阵,就是把两个矩阵左右组合,要求行数相等。

# -*- coding: utf-8 -*
import numpy as np

x_1 = np.array([1, 2, 3, 4, 5, 6]).reshape(2, 3)
x_2 = np.array([3, 2, 1, 8, 9, 6]).reshape(2, 3)
x_new = np.c_[x_1,x_2]
print("x_1 = \n",x_1)
print("x_2 = \n",x_2)
print("x_new = \n",x_new)

结果输出如下:

x_1 = 
 [[1 2 3]
  [4 5 6]]
x_2 = 
 [[3 2 1]
  [8 9 6]]
x_new = 
 [[1 2 3 3 2 1]
  [4 5 6 8 9 6]]
x1 = np.array([[[1,2,3,8],[1,2,3,8],[1,2,3,8]],
               [[1,2,3,8],[1,2,3,8],[1,2,3,8]]])
x2 = np.array([[[0,2,3,8],[1,3,5,8],[1,2,3,8]],
               [[1,2,9,8],[9,2,3,8],[1,2,3,8]]])
print(x1.shape)
a = np.c_[x1,x2]
print(a, a.shape)

输出:

(2, 3, 4)
[[[1 2 3 8 0 2 3 8]
  [1 2 3 8 1 3 5 8]
  [1 2 3 8 1 2 3 8]]

 [[1 2 3 8 1 2 9 8]
  [1 2 3 8 9 2 3 8]
  [1 2 3 8 1 2 3 8]]] (2, 3, 8)

Process finished with exit code 0

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐