Unwanted extra dimensions in numpy array

爷,独闯天下 提交于 2019-12-01 15:09:29

I'm assuming scaled_flat1a is a numpy array? In that case, it should be as simple as a reshape command.

import numpy as np

a = np.array([[[[1, 2, 3],
                [4, 6, 7]]]])
print(a.shape)
# (1, 1, 2, 3)

a = a.reshape(a.shape[2:])  # You can also use np.reshape()
print(a.shape)
# (2, 3)

There is the method called squeeze which does just what you want:

Remove single-dimensional entries from the shape of an array.

Parameters

a : array_like
    Input data.
axis : None or int or tuple of ints, optional
    .. versionadded:: 1.7.0

    Selects a subset of the single-dimensional entries in the
    shape. If an axis is selected with shape entry greater than
    one, an error is raised.

Returns

squeezed : ndarray
    The input array, but with with all or a subset of the
    dimensions of length 1 removed. This is always `a` itself
    or a view into `a`.

for example:

import numpy as np

extra_dims = np.random.randint(0, 10, (1, 1, 5, 7))
minimal_dims = extra_dims.squeeze()

print minimal_dims.shape
# (5, 7)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!