在 NumPy 中使用数组时,resize 和 reshape 有什么区别? [英] What is the difference between resize and reshape when using arrays in NumPy?

查看:124
本文介绍了在 NumPy 中使用数组时,resize 和 reshape 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

Reshape 不会改变前面提到的数据 此处.调整大小会更改数据,如此处所示.

以下是一些示例:

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

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

<预><代码>>>>ar.resize(1,6)>>>ar数组([[ 0.43968751, 0.95057451, 0.54744355, 0.33887095, 0.95809916,0.88722904]])

调整大小后,数组改变了形状.

I have just started using NumPy. What is the difference between resize and reshape for arrays?

解决方案

Reshape doesn't change the data as mentioned here. Resize changes the data as can be seen here.

Here are some examples:

>>> 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]])

After reshape the array didn't change, but only output a temporary array reshape.

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

After resize the array changed its shape.

这篇关于在 NumPy 中使用数组时,resize 和 reshape 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆