记住 3 大类拼接

  1. 横向拼接(左右拼) = 列变多
  2. 纵向拼接(上下拼) = 行变多
  3. 维度拼接(任意维度) = 通用

一、NumPy 数组拼接(ndarray)

先构造两个简单数组:

import numpy as np

a = np.array([[1, 2],
              [3, 4]])  # shape (2,2)

b = np.array([[5, 6],
              [7, 8]])  # shape (2,2)

1. 横向拼接 np.hstack

水平拼接,列增加

c = np.hstack((a, b))  # h = horizontal
print(c)
print(c.shape)  # (2, 4)
[[1 2 5 6]
 [3 4 7 8]]

2. 纵向拼接 np.vstack

垂直拼接,行增加

c = np.vstack((a, b))  # v = vertical
print(c)
print(c.shape)  # (4, 2)
[[1 2]
 [3 4]
 [5 6]
 [7 8]]

3. 通用拼接 np.concatenate

  • axis=0 → 垂直(行方向),上下拼 = 增加行
  • axis=1 → 水平(列方向),左右拼 = 增加列
# 垂直拼接 = vstack
c = np.concatenate([a, b], axis=0)  #shape (4,2)

# 水平拼接 = hstack
c = np.concatenate([a, b], axis=1)  #shape (2,4)

4. 新增维度拼接 np.stack

多出来一个维度

c = np.stack([a, b], axis=0)
print(c.shape)  # (2, 2, 2)

即:
c=
[
[[1 2]
[3 4]]

[[5 6]
[7 8]]
]


二、Python 原生 list 拼接

list1 = [1, 2, 3]
list2 = [4, 5, 6]

1. + 拼接

list3 = list1 + list2  # [1,2,3,4,5,6]

2. .extend()

list1.extend(list2)     # list1 变成 [1,2,3,4,5,6]

⚠️ 注意:
list 没有行列概念,只能简单拼接,不能按维度拼接


三、PyTorch 张量拼接(torch.Tensor)

import torch

a = torch.tensor([[1,2],[3,4]])  # (2,2)
b = torch.tensor([[5,6],[7,8]])  # (2,2)

1. torch.cat 最常用

# 垂直拼接(行增加)dim=0
c = torch.cat([a, b], dim=0)  # (4,2)

# 水平拼接(列增加)dim=1
c = torch.cat([a, b], dim=1)  # (2,4)

2. torch.stack 新增维度

c = torch.stack([a, b], dim=0)
print(c.shape)  # (2, 2, 2)

3. 没有专门的 hstack / vstack

但可以用 cat 完全替代:

  • hstackcat(..., dim=1)
  • vstackcat(..., dim=0)

五、总结表

类型横向拼接(列)纵向拼接(行)新增维度
NumPynp.hstacknp.vstacknp.stack
np.concatenate(axis=1)np.concatenate(axis=0)
Torchtorch.cat(dim=1)torch.cat(dim=0)torch.stack
Listlist1 + list2list1 + list2不支持

六、NumPy 拼接 ↔ PyTorch 张量拼接

在深度学习领域,通常使用张量(如,PyTorch 张量),为此我们专门讨论一下“NumPy 拼接 ↔ PyTorch 张量拼接”。

0. 先构造数据

import numpy as np
import torch

# 两个 (2,1) 数组,模拟你代码里的 oof_1, oof_2
a_np = np.array([[1], [2]])        # shape (2,1)
b_np = np.array([[3], [4]])        # shape (2,1)

a_torch = torch.tensor([[1], [2]]) # shape (2,1)
b_torch = torch.tensor([[3], [4]])

1. 横向拼接(列拼接 → 特征变多)

NumPy 方式

# 水平拼接
h_np = np.hstack([a_np, b_np])
print(h_np)
print(h_np.shape)  # (2, 2)

结果:

[[1 3]
 [2 4]]

等价通用写法:

h_np2 = np.concatenate([a_np, b_np], axis=1)

PyTorch 对应方式

h_torch = torch.cat([a_torch, b_torch], dim=1)
print(h_torch)
print(h_torch.shape)  # torch.Size([2, 2])

效果完全一样!


2. 纵向拼接(行拼接 → 样本变多)

NumPy

v_np = np.vstack([a_np, b_np])
print(v_np.shape)  # (4,1)

等价:

v_np2 = np.concatenate([a_np, b_np], axis=0)

PyTorch

v_torch = torch.cat([a_torch, b_torch], dim=0)
print(v_torch.shape)  # torch.Size([4,1])

3. 新增维度拼接(stack)

NumPy

s_np = np.stack([a_np, b_np], axis=0)
print(s_np.shape)  # (2, 2, 1)

PyTorch

s_torch = torch.stack([a_torch, b_torch], dim=0)
print(s_torch.shape)  # torch.Size([2, 2, 1])

4. 对照表

操作NumPyPyTorch效果
横向拼接(列)np.hstack([a,b])torch.cat([a,b], dim=1)特征增加
纵向拼接(行)np.vstack([a,b])torch.cat([a,b], dim=0)样本增加
通用拼接np.concatenate(..., axis=)torch.cat(..., dim=)任意维度
新增维度np.stack(..., axis=)torch.stack(..., dim=)维度+1
  • numpy:hstack / vstack / concatenate(axis=...)
  • torch:统一用 torch.cat(dim=...)
  • dim=0 上下拼,dim=1 左右拼

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐