gpt4 book ai didi

python - NumPy 数组中不需要的额外维度

转载 作者:太空狗 更新时间:2023-10-29 17:10:32 24 4
gpt4 key购买 nike

我打开了一张 .fits 图片:

scaled_flat1 = pyfits.open('scaled_flat1.fit')   
scaled_flat1a = scaled_flat1[0].data

当我打印它的形状时:

print scaled_flat1a.shape

我得到以下信息:

(1, 1, 510, 765)

我希望它显示为:

(510, 765)

我如何摆脱它之前的两个?

最佳答案

有一个方法叫做squeeze这正是你想要的:

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

例如:

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)

关于python - NumPy 数组中不需要的额外维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25453587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com