import numpy as np
# 创建一个三维数组
three_d_array = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
# 使用 reshape 方法将三维数组变成二维数组
# -1 表示自动计算该维度的大小
two_d_array = three_d_array.reshape(-1, three_d_array.shape[-1])
print("原始三维数组:")
print(three_d_array)
print("\n转换后的二维数组:")
print(two_d_array)
numpy
库创建了一个三维数组 three_d_array
。这个数组的形状是 (2, 2, 2)
,表示有两个 2x2 的矩阵。reshape
方法可以改变数组的形状。-1
参数表示让 numpy
自动计算该维度的大小。这里我们将三维数组变成了二维数组,新的形状为 (4, 2)
。希望这段代码和解释对你有帮助!
上一篇:python贪吃蛇代码可复制
下一篇:python 字符串索引
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站