简体   繁体   中英

How are 'numpy.reshape' and 'ndarray.reshape' equivalent?

I have a question about the structure of ndarray.reshape . I have read that numpy.reshape() and ndarray.reshape are equivalent commands in python to reshape an array.

As far as I know, numpy is an Object in which the reshape method is defined. So the usage of the dot operator in numpy.reshape() is understandable for me. But when it comes to ndarray.reshape , I don't understand how the dot operator works. There is no reference to the numpy object in ndarray.reshape ; how does it know that reshape is related to the numpy object?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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