python numpy中 ravel() 和 flatten() 函数的用法与区别 (返回一个展平的数组)

ravel from multiarray.py

    def ravel(self, order=None): # real signature unknown; restored from __doc__
        """
        a.ravel([order])
        
            Return a flattened array. 返回一个展平的数组。
        
            Refer to `numpy.ravel` for full documentation.
            请参考`numpy.ravel`以获得完整的文档。
        
            See Also
            --------
            numpy.ravel : equivalent function
        
            ndarray.flat : a flat iterator on the array. 数组上的平面迭代器。
        """
        pass

ravel from fromnumeric.py

def ravel(a, order='C'):
    """Return a contiguous flattened array. 返回一个连续的扁平数组。

    A 1-D array, containing the elements of the input, is returned.  A copy is
    made only if needed.
    返回包含输入元素的一维数组。 仅在需要时才进行复制。

    As of NumPy 1.10, the returned array will have the same type as the input
    array. (for example, a masked array will be returned for a masked array
    input)
    从NumPy 1.10开始,返回的数组将具有与输入数组相同的类型。 (例如,将为掩码数组输入返回一个掩码数组)

    Parameters
    ----------
    a : array_like
        Input array.  The elements in `a` are read in the order specified by
        `order`, and packed as a 1-D array.
        array_like
         输入数组。 “ a”中的元素按“ order”指定的顺序读取,并打包为一维数组。
         
    order : {'C','F', 'A', 'K'}, optional

        The elements of `a` are read using this index order. 'C' means
        to index the elements in row-major, C-style order,
        with the last axis index changing fastest, back to the first
        axis index changing slowest.  'F' means to index the elements
        in column-major, Fortran-style order, with the
        first index changing fastest, and the last index changing
        slowest. Note that the 'C' and 'F' options take no account of
        the memory layout of the underlying array, and only refer to
        the order of axis indexing.  'A' means to read the elements in
        Fortran-like index order if `a` is Fortran *contiguous* in
        memory, C-like order otherwise.  'K' means to read the
        elements in the order they occur in memory, except for
        reversing the data when strides are negative.  By default, 'C'
        index order is used.
        使用该索引顺序读取`a`的元素。 
        “ C”表示以行优先,C样式的顺序索引元素,最后一个轴索引更改最快,回到第一个轴索引更改最慢。 
        “ F”表示以Fortran样式的列主顺序索引元素,第一个索引变化最快,最后一个索引变化最慢。 请注意,“ C”和“ F”选项不考虑基础数组的内存布局,仅参考轴索引的顺序。 
        如果A在内存中是“连续”的,则“ A”表示以类似于Fortran的索引顺序读取元素,否则为类似C的顺序。 
        “ K”表示按步在内存中的顺序读取元素,但步幅为负时反转数据除外。 
        默认情况下,使用“ C”索引顺序。

    Returns
    -------
    y : array_like
        y is an array of the same subtype as `a`, with shape ``(a.size,)``.
        Note that matrices are special cased for backward compatibility, if `a`
        is a matrix, then y is a 1-D ndarray.
        array_like
         y是与`a'相同子类型的数组,形状为``(a.size,)``。 
         请注意,矩阵是特殊情况下的向后兼容性,如果a是矩阵,则y是一维ndarray。

    See Also
    --------
    ndarray.flat : 1-D iterator over an array. 数组上的一维迭代器。
    
    ndarray.flatten : 1-D array copy of the elements of an array 
                      in row-major order.
                      按行优先顺序排列的数组元素的一维数组副本。
                      
    ndarray.reshape : Change the shape of an array without changing its data.
    更改数组的形状而不更改其数据。

    Notes
    -----
    In row-major, C-style order, in two dimensions, the row index
    varies the slowest, and the column index the quickest.  This can
    be generalized to multiple dimensions, where row-major order
    implies that the index along the first axis varies slowest, and
    the index along the last quickest.  The opposite holds for
    column-major, Fortran-style index ordering.
    以行优先C样式的顺序在二维中,行索引变化最慢,列索引变化最快。 
    这可以推广到多个维度,其中行优先顺序意味着沿第一个轴的索引变化最慢,而沿最后一个轴的索引变化最快。 
    对于以列为主的Fortran风格的索引排序则相反。

    When a view is desired in as many cases as possible, ``arr.reshape(-1)``
    may be preferable.
    在尽可能多的情况下需要视图时,``arr.reshape(-1)``可能更可取。

    Examples
    --------
    It is equivalent to ``reshape(-1, order=order)``.

    >>> x = np.array([[1, 2, 3], [4, 5, 6]])
    >>> print(np.ravel(x))
    [1 2 3 4 5 6]

    >>> print(x.reshape(-1))
    [1 2 3 4 5 6]

    >>> print(np.ravel(x, order='F'))	# 列优先
    [1 4 2 5 3 6]

    When ``order`` is 'A', it will preserve the array's 'C' or 'F' ordering:
    当order为'A'时,它将保留数组的'C'或'F'排序:

    >>> print(np.ravel(x.T))
    [1 4 2 5 3 6]
    >>> print(np.ravel(x.T, order='A'))
    [1 2 3 4 5 6]

    When ``order`` is 'K', it will preserve orderings that are neither 'C'
    nor 'F', but won't reverse axes:
    当``order''为'K'时,它将保留既不是'C'也不是'F'的顺序,但不会反转轴:

    >>> a = np.arange(3)[::-1]; a
    array([2, 1, 0])
    >>> a.ravel(order='C')
    array([2, 1, 0])
    >>> a.ravel(order='K')
    array([2, 1, 0])

    >>> a = np.arange(12).reshape(2,3,2).swapaxes(1,2); a
    array([[[ 0,  2,  4],
            [ 1,  3,  5]],
           [[ 6,  8, 10],
            [ 7,  9, 11]]])
    >>> a.ravel(order='C')
    array([ 0,  2,  4,  1,  3,  5,  6,  8, 10,  7,  9, 11])
    >>> a.ravel(order='K')
    array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

    """
    if isinstance(a, np.matrix):
        return asarray(a).ravel(order=order)
    else:
        return asanyarray(a).ravel(order=order)

flatten from multiarray.py

    def flatten(self, order='C'): # real signature unknown; restored from __doc__
        """
        a.flatten(order='C')
        
            Return a copy of the array collapsed into one dimension.
            返回折叠成一维的数组副本。
        
            Parameters
            ----------
            order : {'C', 'F', 'A', 'K'}, optional
                'C' means to flatten in row-major (C-style) order.
                'F' means to flatten in column-major (Fortran-
                style) order. 'A' means to flatten in column-major
                order if `a` is Fortran *contiguous* in memory,
                row-major order otherwise. 'K' means to flatten
                `a` in the order the elements occur in memory.
                The default is 'C'.

				“ C”表示按行优先(C样式)的顺序展平。 
				“ F”表示按列主(Fortran样式)顺序展平。 
				'A'表示如果`a'在内存中是Fortran *连续*,则以列优先顺序展平,否则以行优先顺序展平。 
				“ K”表示按元素在内存中出现的顺序展平“ a”。 默认值为“ C”。
        
            Returns
            -------
            y : ndarray
                A copy of the input array, flattened to one dimension.
                输入数组的副本,展平为一个维度。
        
            See Also
            --------
            ravel : Return a flattened array. 返回一个展平的数组。
            flat : A 1-D flat iterator over the array. 数组上的一维平面迭代器。
        
            Examples
            --------
            >>> a = np.array([[1,2], [3,4]])
            >>> a.flatten()
            array([1, 2, 3, 4])
            >>> a.flatten('F')
            array([1, 3, 2, 4])
        """
        pass

引用

你可能感兴趣的:(深入浅出,python机器学习)