簡體   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