Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you reduce the dimension of a numpy array?

Tags:

python

numpy

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]]])

In this case, (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.

(EDIT: 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.]]])

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

like image 488
HeyWatchThis Avatar asked Dec 17 '16 21:12

HeyWatchThis


People also ask

How do I reduce the size of a NumPy array?

You can use numpy. squeeze() to remove all dimensions of size 1 from the NumPy array ndarray . squeeze() is also provided as a method of ndarray .

How do you reduce a dimension in Python?

Principal Component Analysis (PCA) PCA is one the simplest and by far the most common method for Dimensionality Reduction. It can be thought of as a lossy compression method that linearly combines dimensions to reduce them, keeping as much of the dataset variance as possible.

How do I change the size of an array in NumPy?

The shape of the array can also be changed using the resize() method. If the specified dimension is larger than the actual array, The extra spaces in the new array will be filled with repeated copies of the original array.

How do you reduce an array shape in Python?

Use A[:,:,0] or A[:,:,1] to get a lower dimensional slice.


2 Answers

You could use 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)
like image 132
yeharav Avatar answered Oct 06 '22 01:10

yeharav


I stumbled upon A.reshape(1,27,1) and first without conserving the size and I got a

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.

like image 32
HeyWatchThis Avatar answered Oct 05 '22 23:10

HeyWatchThis