Vladislav Ladenkov Vladislav Ladenkov - 2 months ago 11
Python Question

Slice numpy array to make it desired shaped

Surprisingly, couldn't find the answer across the internet. I have an n-dimensional numpy array. E.g.: 2-D np array:

array([['34.5500000', '36.9000000', '37.3200000', '37.6700000'],
['41.7900000', '44.8000000', '48.2600000', '46.1800000'],
['36.1200000', '37.1500000', '39.3100000', '38.1000000'],
['82.1000000', '82.0900000', '76.0200000', '77.7000000'],
['48.0100000', '51.2500000', '51.1700000', '52.5000000', '55.2500000'],
['39.7500000', '39.5000000', '36.8100000', '37.2500000']], dtype=object)


As you can see, the 5th row consists of 5 elemnts and i want to make the 5th dissapear, using something like this:

np.slice(MyArray, [6,4])


[6,4] is a shape. I really DO not want to iterate threw dimensions and cut them. I tried the
resize
method, but it returns nothing!

Answer

This is not a 2d array. It is a 1d array, whose elements are objects, in this case some 4 element lists and one 5 element one. And this lists contain strings.

In [577]: np.array([['34.5500000', '36.9000000', '37.3200000', '37.6700000'],
     ...:        ['41.7900000', '44.8000000', '48.2600000', '46.1800000'],
     ...:        ['36.1200000', '37.1500000', '39.3100000', '38.1000000'],
     ...:        ['82.1000000', '82.0900000', '76.0200000', '77.7000000'],
     ...:        ['48.0100000', '51.2500000', '51.1700000', '52.5000000', '55.25
     ...: 00000'],
     ...:        ['39.7500000', '39.5000000', '36.8100000', '37.2500000']], dtyp
     ...: e=object)
Out[577]: 
array([['34.5500000', '36.9000000', '37.3200000', '37.6700000'],
       ['41.7900000', '44.8000000', '48.2600000', '46.1800000'],
       ['36.1200000', '37.1500000', '39.3100000', '38.1000000'],
       ['82.1000000', '82.0900000', '76.0200000', '77.7000000'],
       ['48.0100000', '51.2500000', '51.1700000', '52.5000000', '55.2500000'],
       ['39.7500000', '39.5000000', '36.8100000', '37.2500000']], dtype=object)
In [578]: MyArray=_
In [579]: MyArray.shape
Out[579]: (6,)
In [580]: MyArray[0]
Out[580]: ['34.5500000', '36.9000000', '37.3200000', '37.6700000']
In [581]: MyArray[5]
Out[581]: ['39.7500000', '39.5000000', '36.8100000', '37.2500000']
In [582]: MyArray[4]
Out[582]: ['48.0100000', '51.2500000', '51.1700000', '52.5000000', '55.2500000']
In [583]: 

To slice this you need to iterate on the elements of the array

In [584]: [d[:4] for d in MyArray]
Out[584]: 
[['34.5500000', '36.9000000', '37.3200000', '37.6700000'],
 ['41.7900000', '44.8000000', '48.2600000', '46.1800000'],
 ['36.1200000', '37.1500000', '39.3100000', '38.1000000'],
 ['82.1000000', '82.0900000', '76.0200000', '77.7000000'],
 ['48.0100000', '51.2500000', '51.1700000', '52.5000000'],
 ['39.7500000', '39.5000000', '36.8100000', '37.2500000']]

Now with all the sublists the same length, np.array will create a 2d array:

In [585]: np.array(_)
Out[585]: 
array([['34.5500000', '36.9000000', '37.3200000', '37.6700000'],
       ['41.7900000', '44.8000000', '48.2600000', '46.1800000'],
       ['36.1200000', '37.1500000', '39.3100000', '38.1000000'],
       ['82.1000000', '82.0900000', '76.0200000', '77.7000000'],
       ['48.0100000', '51.2500000', '51.1700000', '52.5000000'],
       ['39.7500000', '39.5000000', '36.8100000', '37.2500000']], 
      dtype='<U10')

Still strings, though

In [586]: np.array(__,dtype=float)
Out[586]: 
array([[ 34.55,  36.9 ,  37.32,  37.67],
       [ 41.79,  44.8 ,  48.26,  46.18],
       [ 36.12,  37.15,  39.31,  38.1 ],
       [ 82.1 ,  82.09,  76.02,  77.7 ],
       [ 48.01,  51.25,  51.17,  52.5 ],
       [ 39.75,  39.5 ,  36.81,  37.25]])
Comments