美文网首页
numpy.ravel 和 numpy.flatten()

numpy.ravel 和 numpy.flatten()

作者: 默写年华Antifragile | 来源:发表于2018-09-16 11:40 被阅读138次

两者的功能是一致的,将多维数组降为一维,但是两者的区别是返回拷贝还是返回视图,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

相关文章

  • numpy.ravel 和 numpy.flatten()

    两者的功能是一致的,将多维数组降为一维,但是两者的区别是返回拷贝还是返回视图,np.flatten(0返回一份拷贝...

  • -和 和 -

    产品介绍:和和是一款会员制共享平台;所有 经营者可在APP内注册和和商家成为会员供 应商(实体店、网店、微商、平台...

  • &和&&,|和||

    原文:https://blog.csdn.net/chinabestchina/article/details/7...

  • 和可和,非常和

    我年纪很小的时候,父亲有一本笔记本,上面只写了一句话:万物并育而不相害,道并行而不相悖。我当时很喜欢这句话,所以期...

  • kotlin中的空? 和 ?. 和 ?: 和 as? 和 !!

    ? 可空类型 kotlin和Java的类型系统之间的一个很重要的区别就是,Kotlin对可空类型的显示支持 也就是...

  • self. 和 _ 和 = 和 set

    声明了一个属性 @property (a,b) p1; 只有用self.调用时修饰关键词才起作用, 用_调用...

  • Observable和Observe和Subcriblers 和

    Observable事件源,被观察者。Subcriblers 观察者,事件订阅者Observer 同Subcrib...

  • ?. 和 ?: 和 let 和 with和 解构声明 使用说明

  • 房子和粮食和蔬菜和大海

    我有一间房子,面朝房子,房子和房子和房子的后面是群山。我有一盏粉红色的落地灯,商品详情页写的是茱萸粉,与百度首页的...

  • nil和NSNull和NULL和Nil

    一、nil 我们给对象赋值时一般会使用object = nil,表示我想把这个对象释放掉; 或者对象由于某种原因,...

网友评论

      本文标题:numpy.ravel 和 numpy.flatten()

      本文链接:https://www.haomeiwen.com/subject/hiwxnftx.html