我有一个关于ndarray.reshape的结构的问题。我已经读过,numpy.reshape()和ndarray.reshape是python中用来重塑数组的等效命令。
据我所知,numpy是一个定义reshape方法的对象。所以在numpy.reshape()中使用点运算符对我来说是可以理解的。但是说到ndarray.reshape,我不明白点运算符是如何工作的。ndarray.reshape中没有对numpy对象的引用;它如何知道reshape与numpy对象相关?
发布于 2020-01-15 16:08:49
我可能理解了一些错误,但通常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))本质上,这两行代码是等价的,因此arr2和arr3包含相同的3x3数组。
https://stackoverflow.com/questions/59754486
复制相似问题