简体   繁体   English

ndarray.transpose 和 numpy.transpose 的区别

[英]Difference between ndarray.transpose and numpy.transpose

What is the difference between ndarray.transpose and numpy.transpose ? ndarray.transposenumpy.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都是低成本操作,只是改变shapestrides属性。 Use which ever makes your code clearest (that is, readable to a human).使用使您的代码最清晰的(即对人类可读)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM