Smoothing an array with just one layer?

Is there a built-in numpy function:

a=np.asarray([[[1,2],[3,4]],[[1,2],[3,4]]])

And will return:

b=[[1,2],[3,4],[1,2],[3,4]]

? Something like smoothing one layer.

PS I'm looking for a vectorized version, otherwise this dumb code is available:

flat1D(a):
   b=np.array([])
   for item in a:
      b=np.append(b,item)
   return b
+4
source share
2 answers

You can just resize the array.

>>> a.reshape(-1,a.shape[-1])
array([[1, 2],
       [3, 4],
       [1, 2],
       [3, 4]])

The code shown returns a 1D array to do this:

>>> a.ravel()
array([1, 2, 3, 4, 1, 2, 3, 4])

Or, if you are sure you want to copy the array:

>>> a.flatten()
array([1, 2, 3, 4, 1, 2, 3, 4])

The difference between ravel and flatten is primarily due to the fact that flatten will always return a copy, and ravel will return a view, if possible, and a copy, if not.

+8
source

if you know the dimensions of the new array, you can specify them as a tuple (4,2)and use.reshape()

a.reshape((4,2))
0
source

All Articles