如何减少 numpy 数组的维数? [英] How do you reduce the dimension of a numpy array?

查看:100
本文介绍了如何减少 numpy 数组的维数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个 mxnxp 数组开始,A

I started with an mxnxp array, 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 元素,所以我尝试切片,但第三维仍然存在.

I wanted to get just the 1st element from the third dimension, so I tried slicing, but the 3rd dimension still existed.

(最初我不小心写了我想要 2nd 元素.)

( originally I accidentally wrote I wanted the 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.]]])

基本上我想要一个没有第三维的 2x27 数组,因为在我的例子中第三维只有一个元素.

Basically I want a 2x27 array without the third dimension, since the third dimension in my case just has one element.

推荐答案

你可以使用 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)

这篇关于如何减少 numpy 数组的维数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆