gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:44:46 29 4
gpt4 key购买 nike

我对 ndarray.reshape 的结构有疑问.我读过 numpy.reshape()ndarray.reshape是 python 中用于 reshape 数组的等效命令。

据我所知,numpy 是一个对象,其中 reshape方法已定义。所以numpy.reshape()中点运算符的用法对我来说是可以理解的。但是说到ndarray.reshape ,我不明白点运算符是如何工作的。 ndarray.reshape 中没有对 numpy 对象的引用;它怎么知道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 数组。

关于python - 'numpy.reshape' 和 'ndarray.reshape' 如何等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59754486/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com