繁体   English   中英

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

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

我有一个关于ndarray.reshape结构的ndarray.reshape 我读过numpy.reshape()ndarray.reshape是 python 中用于重塑数组的等效命令。

据我所知,numpy 是一个对象,其中定义了reshape方法 所以在numpy.reshape()使用点运算符对我来说是可以理解的。 但是当谈到ndarray.reshape ,我不明白点运算符是如何工作的。 ndarray.reshape没有对numpy对象的ndarray.reshape 它怎么知道reshape与 numpy 对象有关?

我可能理解错误,但通常numpy指的是实际的 Numpy 模块,通过调用numpy.reshape您正在调用静态函数,您还需要将数组作为第一个参数传递给它,而ndarray位则推迟到一个实际的 numpy 数组。 例子:

# 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))

本质上,这最后两行代码是等效的,因此arr2arr3包含相同的 3x3 数组。

暂无
暂无

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

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