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

标签 python numpy

我刚刚开始使用 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/

相关文章:

python - 将 Pandas Dataframe 转换为 numpy 数组

python - 如何在Python中计算网格数据集的平均值

python - 更改多行数组的numpy矢量化方式(行可以重复)

python - 从可迭代对象创建字典

python - PasswordResetConfirmView 不生成邮件

python - 将带括号的字符串转换为 numpy 数组

python - 如何在 Python 中矢量化点积?

python - 为什么 Sentry 不将来自 Celery 的 SoftTimeLimitExceeded 错误分组?

python - 使用 pisa 和非 ascii 字符创建 PDF

python - AttributeError: 'Application' 对象没有属性 'label'