首页 > 解决方案 > 我可以在同一轴的数组中添加超过 1 个维度吗?

问题描述

我没有正确理解尺寸/轴的概念。我尝试了以下代码:

import numpy as np
l = [1,2,3,4,5,6]
x = np.expand_dims(l,0)
print(x)
print(x.shape)
x = np.expand_dims(l,0)
print(x)
print(x.shape)

结果我得到:

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

为什么我没有得到:

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

标签: arrayspython-3.xnumpy

解决方案


您需要在第二次尝试扩展尺寸时更改l为:x

x = np.expand_dims(l,0)
x = np.expand_dims(x,0)
print(x.shape)
(1, 1, 6)

推荐阅读