[英]Difference between ndarray.transpose and numpy.transpose
What is the difference between ndarray.transpose
and numpy.transpose
? ndarray.transpose
和numpy.transpose
什么numpy.transpose
? and in which scenario, we have to which one ?在哪种情况下,我们必须选择哪种? is there a scenario where only one of the above will work ?
是否存在仅上述一种情况有效的情况?
I have gone through the documentation and per my understanding numpy.transpose
will return a view whenever possible.我已经阅读了文档,根据我的理解
numpy.transpose
将尽可能返回一个视图。 Whereas ndarray.transpose
returns a view always.而
ndarray.transpose
总是返回一个视图。
It would be great if someone could give me some example where only one of the above is a good fit.如果有人能给我一些例子,其中只有上述之一是合适的,那就太好了。
The code for np.transpose
is: np.transpose
的代码是:
def transpose(a, axes=None):
return _wrapfunc(a, 'transpose', axes)
which is, effectively:即,有效地:
np.asarray(a).transpose(axes)
that is, make a no-copy array and apply the method.也就是说,创建一个非复制数组并应用该方法。
If a
is already an array, the two approaches are essentially the same.如果
a
已经是一个数组,则这两种方法本质上是相同的。 Either way transpose
is a low cost operation, just changing shape
and strides
attributes.无论哪种方式
transpose
都是低成本操作,只是改变shape
和strides
属性。 Use which ever makes your code clearest (that is, readable to a human).使用使您的代码最清晰的(即对人类可读)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.