Python 如何减少 numpy 数组的维数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41203137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you reduce the dimension of a numpy array?
提问by HeyWatchThis
I started with an mxnxp
array, A
,
我从一个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]]])
In this case, (2, 27, 2)
在这种情况下, (2, 27, 2)
In [17]: A.shape
Out[17]: (2, 27, 2)
I wanted to get just the 1st
element from the third dimension, so I tried slicing, but the 3rd dimension still existed.
我只想1st
从第三维中获取元素,所以我尝试切片,但第三维仍然存在。
(EDIT: originally I accidentally wrote I wanted the 2nd
elem.)
(编辑:最初我不小心写了我想要2nd
elem。)
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.]]])
Basically I want a 2x27
array without the third dimension, since the third dimension in my case just has one element.
基本上我想要一个2x27
没有第三维的数组,因为在我的例子中第三维只有一个元素。
回答by yeharav
You could use numpy.squeeze()
你可以使用 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)
回答by HeyWatchThis
I stumbled upon A.reshape(1,27,1)
and first without conserving the size and I got a
我偶然发现A.reshape(1,27,1)
,首先没有保持尺寸,我得到了一个
ValueError: total size of new array must be unchanged
error, but then accidentally, I ended up trying omitting the third dimension in the reshape,
错误,但后来不小心,我最终尝试在重塑中省略第三维,
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.]])
and magically the third dimension disappeared.
第三维度神奇地消失了。
And this is exactly what I wanted.
而这正是我想要的。