简体   繁体   English

'numpy.reshape' 和 'ndarray.reshape' 如何等效?

[英]How are 'numpy.reshape' and 'ndarray.reshape' equivalent?

I have a question about the structure of ndarray.reshape .我有一个关于ndarray.reshape结构的ndarray.reshape I have read that numpy.reshape() and ndarray.reshape are equivalent commands in python to reshape an array.我读过numpy.reshape()ndarray.reshape是 python 中用于重塑数组的等效命令。

As far as I know, numpy is an Object in which the reshape method is defined.据我所知,numpy 是一个对象,其中定义了reshape方法 So the usage of the dot operator in numpy.reshape() is understandable for me.所以在numpy.reshape()使用点运算符对我来说是可以理解的。 But when it comes to ndarray.reshape , I don't understand how the dot operator works.但是当谈到ndarray.reshape ,我不明白点运算符是如何工作的。 There is no reference to the numpy object in ndarray.reshape ; ndarray.reshape没有对numpy对象的ndarray.reshape how does it know that reshape is related to the numpy object?它怎么知道reshape与 numpy 对象有关?

I might be understanding something wrong, but usually numpy refers to the actual Numpy module, and by calling numpy.reshape you are calling the static function and you also need to pass the array into it as the first argument, whereas the ndarray bit defers to an actual numpy-array.我可能理解错误,但通常numpy指的是实际的 Numpy 模块,通过调用numpy.reshape您正在调用静态函数,您还需要将数组作为第一个参数传递给它,而ndarray位则推迟到一个实际的 numpy 数组。 Example:例子:

# import the module here
import numpy

# create an vector of 9 elements
arr = numpy.random.rand(1,9)

# and now I call the 'static' version of the reshape method:
arr2 = numpy.reshape(arr, (3,3))

# and here I just call the reshape method of the existing array
arr3 = arr.reshape((3,3))

Essentially, these last two lines of code are equivalent, so the arr2 and arr3 contain the same 3x3 array.本质上,这最后两行代码是等效的,因此arr2arr3包含相同的 3x3 数组。

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

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