gpt4 book ai didi

python - 在 NumPy 中使用数组时,resize 和 reshape 之间有什么区别?

转载 作者:太空狗 更新时间:2023-10-29 17:05:44 24 4
gpt4 key购买 nike

我刚刚开始使用 NumPy。数组的 resizereshape 有什么区别?

最佳答案

Reshape 不会像提到的那样更改数据 here .resize改变数据可以看出here .

这里有一些例子:

>>> numpy.random.rand(2,3)
array([[ 0.6832785 , 0.23452056, 0.25131171],
[ 0.81549186, 0.64789272, 0.48778127]])
>>> ar = numpy.random.rand(2,3)
>>> ar.reshape(1,6)
array([[ 0.43968751, 0.95057451, 0.54744355, 0.33887095, 0.95809916,
0.88722904]])
>>> ar
array([[ 0.43968751, 0.95057451, 0.54744355],
[ 0.33887095, 0.95809916, 0.88722904]])

reshape后数组没有变化,只是输出一个临时数组reshape。

>>> ar.resize(1,6)
>>> ar
array([[ 0.43968751, 0.95057451, 0.54744355, 0.33887095, 0.95809916,
0.88722904]])

调整大小后,数组改变了它的形状。

关于python - 在 NumPy 中使用数组时,resize 和 reshape 之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41518351/

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