Python numpy 中的 flatten 和 ravel 函数有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28930465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:54:47  来源:igfitidea点击:

What is the difference between flatten and ravel functions in numpy?

pythonnumpymultidimensional-arrayflattennumpy-ndarray

提问by DEEPAK YADAV

import numpy as np
y = np.array(((1,2,3),(4,5,6),(7,8,9)))
OUTPUT:
print(y.flatten())
[1   2   3   4   5   6   7   8   9]
print(y.ravel())
[1   2   3   4   5   6   7   8   9]

Both function return the same list. Then what is the need of two different functions performing same job.

两个函数都返回相同的列表。那么需要两个不同的功能来执行相同的工作。

回答by IanH

The current API is that:

当前的API是:

  • flattenalways returns a copy.
  • ravelreturns a view of the original array whenever possible. This isn't visible in the printed output, but if you modify the array returned by ravel, it may modify the entries in the original array. If you modify the entries in an array returned from flatten this will never happen. ravel will often be faster since no memory is copied, but you have to be more careful about modifying the array it returns.
  • reshape((-1,))gets a view whenever the strides of the array allow it even if that means you don't always get a contiguous array.
  • flatten总是返回一个副本。
  • ravel尽可能返回原始数组的视图。这在打印输出中不可见,但如果您修改 ravel 返回的数组,它可能会修改原始数组中的条目。如果您修改从 flatten 返回的数组中的条目,这将永远不会发生。ravel 通常会更快,因为没有内存被复制,但你必须更加小心修改它返回的数组。
  • reshape((-1,))获取视图,只要数组的步幅允许它,即使这意味着您并不总是获得连续的数组。

回答by Bryan P

As explained herea key difference is that:

正如此处所解释的一个主要区别在于:

  • flattenis a method of an ndarray object and hence can only be called for true numpy arrays.

  • ravelis a library-level function and hence can be called on any object that can successfully be parsed.

  • flatten是 ndarray 对象的方法,因此只能为真正的 numpy 数组调用。

  • ravel是一个库级函数,因此可以在任何可以成功解析的对象上调用。

For example ravelwill work on a list of ndarrays, while flattenis not available for that type of object.

例如ravel将在 ndarrays 列表上工作,flatten而不适用于该类型的对象。

@IanH also points out important differences with memory handling in his answer.

@IanH 在他的回答中还指出了与内存处理的重要区别。

回答by prosti

Here is the correct namespace for the functions:

这是函数的正确命名空间:

Both functions return flattened 1D arrays pointing to the new memory structures.

这两个函数都返回指向新内存结构的扁平一维数组。

import numpy
a = numpy.array([[1,2],[3,4]])

r = numpy.ravel(a)
f = numpy.ndarray.flatten(a)  

print(id(a))
print(id(r))
print(id(f))

print(r)
print(f)

print("\nbase r:", r.base)
print("\nbase f:", f.base)

---returns---
140541099429760
140541099471056
140541099473216

[1 2 3 4]
[1 2 3 4]

base r: [[1 2]
 [3 4]]

base f: None

In the upper example:

在上面的例子中:

  • the memory locations of the results are different,
  • the results look the same
  • flatten would return a copy
  • ravel would return a view.
  • 结果的内存位置不同,
  • 结果看起来一样
  • flatten 会返回一个副本
  • ravel 会返回一个视图。

How we check if something is a copy? Using the .baseattribute of the ndarray. If it's a view, the base will be the original array; if it is a copy, the base will be None.

我们如何检查某些东西是否是副本?使用.base的属性ndarray。如果是视图,则基数将是原始数组;如果是副本,则基数为None.