numpy中的flatten和ravel有什么区别? [英] What is the difference between flatten and ravel in numpy?
问题描述
Numpy v 1.9包含两个看似相同的功能: 扁平化" 和 "ravel"
Numpy v 1.9 contains two seemingly identical functions: 'flatten' and 'ravel'
有什么区别?我什么时候可以选择一个将另一个二维np.array转换为一维的方法呢?
What is the difference? and when might I pick one vs the other for converting a 2-D np.array to 1-D?
推荐答案
啊哈:
主要的功能差异是flatten
是ndarray对象的方法,因此只能用于真正的numpy数组.相反,ravel()
是库级别的函数,因此可以在可以成功解析的任何对象上调用.例如,ravel()
将在ndarray列表上工作,而扁平化(显然)不会.
Aha:
The primary functional difference is thatflatten
is a method of an ndarray object and hence can only be called for true numpy arrays. In contrast ravel()
is a library-level function and hence can be called on any object that can successfully be parsed. For example ravel()
will work on a list of ndarrays, while flatten (obviously) won't.
此外,正如@jonrsharpe在其评论中指出的那样,flatten方法始终返回一个副本,而ravel仅在需要时"这样做.仍然不太确定如何确定.
In addition, as @jonrsharpe pointed out in his comment, the flatten method always returns a copy, while ravel only does so "if needed." Still not quite sure how this determination is made.
这篇关于numpy中的flatten和ravel有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!