Подтвердить что ты не робот

Нежелательные дополнительные размеры в массиве numpy

Я открыл изображение .fits:

scaled_flat1 = pyfits.open('scaled_flat1.fit')

scaled_flat1a = scaled_flat1[0].data

и когда я печатаю его форму:

print scaled_flat1a.shape

Я получаю следующее:

(1, 1, 510, 765)

Я хочу, чтобы он читал:

(510,765)

Как мне избавиться от этих двух?

4b9b3361

Ответ 1

Я предполагаю, что scaled_flat1a является массивом numpy? В этом случае она должна быть такой же простой, как команда reshape.

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)

Ответ 2

Существует метод squeeze, который делает именно то, что вы хотите:

Удалить одномерные записи из формы массива.

Параметры

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.

Возвращает

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`.

например:

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)