zoukankan      html  css  js  c++  java
  • numpy.ravel() 和 numpy.flatten()

    两者的功能是一致的,将多维数组降为一维,但是两者的区别是返回拷贝还是返回视图,np.flatten(0返回一份拷贝,对拷贝所做修改不会影响原始矩阵,而np.ravel()返回的是视图,修改时会影响原始矩阵

    • numpy.ravel(a, order = 'C'): C axis越小的先变化;'F' axis越大的先变化
      其中

    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.

    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:
    
    >>>
    >>> 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:
    
    >>>
    >>> 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])
    

    参考:https://docs.scipy.org/doc/numpy/reference/generated/numpy.ravel.html

  • 相关阅读:
    03-树3 Tree Traversals Again
    Utuntu下Xshell使用+vi使用
    CSDN总结的面试中的十大算法
    EDM(邮件营销)
    腾讯CDC谈扁平化设计
    Graph Search图谱搜索
    LBS 与 GPS 定位之间的区别
    中间件的理解
    夏梦竹谈Hive vs. HBase的区别
    维基百科上—数据仓库、数据挖掘、OLAP三者之间的区别
  • 原文地址:https://www.cnblogs.com/qiulinzhang/p/9655718.html
Copyright © 2011-2022 走看看