You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python如何将3D numpy数组转换为指定形状的2D数组

实现方法

你可以直接使用numpy的reshape方法完成转换,只需保留第一个维度长度不变,让numpy自动计算合并后第二个维度的长度即可,操作效率很高,不会额外复制数据。
示例代码如下:

import numpy as np

# 你的原始3D数组,示例为arr,替换成你自己的变量名即可
arr = np.random.rand(66183, 59, 5)
# 转换为目标形状的2D数组
arr_2d = arr.reshape(arr.shape[0], -1)

# 验证输出形状
print(arr_2d.shape) # 输出为 (66183, 295),即59*5=295,符合需求

效果验证

针对你给出的测试用例,运行上述方法的结果完全匹配预期:

# 测试用例原始数组
test_arr = np.array([[[ 1.,  5.,  4.],
    [ 1.,  5.,  4.],
    [ 1.,  2.,  4.]],
   [[ 3.,  6.,  4.],
    [ 6.,  6.,  4.],
    [ 6.,  6.,  4.]]])

test_arr_2d = test_arr.reshape(test_arr.shape[0], -1)
print(test_arr_2d)

输出:

array([[1., 5., 4., 1., 5., 4., 1., 2., 4.],
       [3., 6., 4., 6., 6., 4., 6., 6., 4.]])

内容的提问来源于stack exchange,提问作者John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 13:24:04