我开始使用mxnxp
数组,A
,
In [16]: A
Out[16]:
array([[[ 2.10000000e+01, 3.70060693e-01],
[ 2.00000000e+01, 2.15659121e-01],
[ 1.50000000e+01, 1.35009735e-01],
[ 2.30000000e+01, 1.15997981e-01],
[ 2.20000000e+01, 7.02226670e-02],
[ 1.60000000e+01, 3.96571639e-02],
[ 2.50000000e+01, 1.64442373e-02],
[ 2.40000000e+01, 1.29001995e-02],
[ 1.20000000e+01, 8.15782143e-03],
[ 4.00000000e+00, 6.13186659e-03],
[ 7.00000000e+00, 5.95704145e-03],
[ 1.00000000e+00, 2.66991888e-03],
[ 6.00000000e+00, 1.39767193e-04],
[ 3.00000000e+00, 1.07608518e-04],
[ 1.90000000e+01, 1.02427053e-04],
[ 1.30000000e+01, 1.00084545e-04],
[ 1.10000000e+01, 9.35799784e-05],
[ 9.00000000e+00, 8.64687546e-05],
[ 8.00000000e+00, 8.20845769e-05],
[ 2.70000000e+01, 7.61546973e-05],
[ 1.40000000e+01, 7.41430049e-05],
[ 1.80000000e+01, 6.78797119e-05],
[ 1.00000000e+01, 6.02706017e-05],
[ 1.70000000e+01, 4.80705068e-05],
[ 2.60000000e+01, 4.39569061e-05],
[ 2.00000000e+00, 3.49337884e-05],
[ 5.00000000e+00, 1.41243870e-05]],
[[ 2.00000000e+01, 5.12832239e-01],
[ 2.10000000e+01, 2.50467388e-01],
[ 1.20000000e+01, 8.93222985e-02],
[ 1.00000000e+00, 2.17633761e-02],
[ 1.70000000e+01, 1.68455794e-02],
[ 4.00000000e+00, 1.55807665e-02],
[ 2.20000000e+01, 1.51387993e-02],
[ 2.30000000e+01, 1.34972674e-02],
[ 1.60000000e+01, 1.14371791e-02],
[ 6.00000000e+00, 8.99163916e-03],
[ 1.50000000e+01, 8.58543707e-03],
[ 2.60000000e+01, 8.42629684e-03],
[ 1.30000000e+01, 8.05955820e-03],
[ 1.90000000e+01, 5.19301656e-03],
[ 2.40000000e+01, 5.06486482e-03],
[ 2.00000000e+00, 3.99051461e-03],
[ 1.00000000e+01, 3.97385580e-03],
[ 2.50000000e+01, 9.76157597e-05],
[ 3.00000000e+00, 9.24458526e-05],
[ 7.00000000e+00, 9.17936963e-05],
[ 8.00000000e+00, 9.17617111e-05],
[ 1.10000000e+01, 9.03015260e-05],
[ 2.70000000e+01, 8.75101021e-05],
[ 1.40000000e+01, 8.27902640e-05],
[ 9.00000000e+00, 7.88132804e-05],
[ 1.80000000e+01, 6.67699579e-05],
[ 5.00000000e+00, 5.01210508e-05]]])
在这种情况下,(2, 27, 2)
In [17]: A.shape
Out[17]: (2, 27, 2)
我想从第三维获得1st
元素,所以我尝试切片,但第三维仍然存在。
(编辑:最初我不小心写了我想要2nd
元素。)
In [18]: A[:,:,:1]
Out[18]:
array([[[ 21.],
[ 20.],
[ 15.],
[ 23.],
[ 22.],
[ 16.],
[ 25.],
[ 24.],
[ 12.],
[ 4.],
[ 7.],
[ 1.],
[ 6.],
[ 3.],
[ 19.],
[ 13.],
[ 11.],
[ 9.],
[ 8.],
[ 27.],
[ 14.],
[ 18.],
[ 10.],
[ 17.],
[ 26.],
[ 2.],
[ 5.]],
[[ 20.],
[ 21.],
[ 12.],
[ 1.],
[ 17.],
[ 4.],
[ 22.],
[ 23.],
[ 16.],
[ 6.],
[ 15.],
[ 26.],
[ 13.],
[ 19.],
[ 24.],
[ 2.],
[ 10.],
[ 25.],
[ 3.],
[ 7.],
[ 8.],
[ 11.],
[ 27.],
[ 14.],
[ 9.],
[ 18.],
[ 5.]]])
基本上我想要一个没有第三维的2x27
数组,因为我的第三维只有一个元素。
答案 0 :(得分:5)
您可以使用numpy.squeeze()
x = np.array([[[0], [1], [2]]])
x.shape
(1, 3, 1)
np.squeeze(x).shape
(3,)
np.squeeze(x, axis=(2,)).shape
(1, 3)
答案 1 :(得分:3)
我偶然发现A.reshape(1,27,1)
并且首先没有保留大小,我得到了一个
ValueError: total size of new array must be unchanged
错误,但不小心,我最终试图在重塑中省略第三个维度,
In [21]: A[:,:,:1].reshape(2,27)
Out[21]:
array([[ 21., 20., 15., 23., 22., 16., 25., 24., 12., 4., 7.,
1., 6., 3., 19., 13., 11., 9., 8., 27., 14., 18.,
10., 17., 26., 2., 5.],
[ 20., 21., 12., 1., 17., 4., 22., 23., 16., 6., 15.,
26., 13., 19., 24., 2., 10., 25., 3., 7., 8., 11.,
27., 14., 9., 18., 5.]])
神奇的第三个维度消失了。
这正是我想要的。