Unwanted extra dimensions in NumPy array

后端 未结 2 1559
[愿得一人]
[愿得一人] 2020-12-18 22:06

I\'ve opened a .fits image:

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

and when I print its s

2条回答
  •  一个人的身影
    2020-12-18 22:16

    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)
    

提交回复
热议问题