我打开了一张.fits图片:
scaled_flat1 = pyfits.open('scaled_flat1.fit')
scaled_flat1a = scaled_flat1[0].data
当我打印它的形状时:
print scaled_flat1a.shape
我得到以下内容:
(1, 1, 510, 765)
我想要阅读:
(510,765)
如何摆脱之前的两个?
答案 0 :(得分:32)
有一种名为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)
答案 1 :(得分:5)
我假设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)