Python 在 Numpy 中使用数组时,resize 和 reshape 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41518351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:08:19  来源:igfitidea点击:

What is the difference between resize and reshape when using arrays in Numpy?

pythonnumpy

提问by Rish_Saxena

I have just started using 'NumPy' and I am trying to understand the difference between resizeand reshapefor arrays.

我刚开始使用“NumPy的”,我试图理解之间的区别resizereshape数组。

回答by Rahul Reddy Vemireddy

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

Reshape 不会改变这里提到的数据。调整大小更改数据可以看出这里

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 outputs 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 it's shape.

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

回答by GadaaDhaariGeek

One major difference is reshape() does not change your data but resize() doeschange it. resize() first accommodates all the values in original array, after that if extra space is there (or size of new array is greater than original array), then it adds its own values. As @David mentioned in comments, what values resize() adds depends on how that is called.

一个主要区别是reshape() 不会更改您的数据,但 resize() 会更改它。resize() 首先容纳原始数组中的所有值,然后如果有额外空间(或新数组的大小大于原始数组),则添加自己的值。正如@David 在评论中提到的, resize() 添加的值取决于它的调用方式。

You can call reshape()and resize()function in following two ways.

您可以通过以下两种方式调用reshape()resize()运行。

numpy.resize()

numpy.resize()

ndarray.resize()- where ndarrayis ndimensional array you are resizing.

ndarray.resize()-这里ndarrayn维数组所调整。

You can similarly call reshape also as numpy.reshape()and ndarray.reshape(). But here they are almost same except the syntax.

您也可以类似地将 reshape 称为numpy.reshape()and ndarray.reshape()。但在这里,除了语法之外,它们几乎相同。

One point to notice is that, reshape() will always try to return a view wherever possible otherwise to would return a copy. Also it can't tell what will be returned when, but you can make your code to raise error whenever the data is copied.

需要注意的一点是,reshape() 将始终尝试尽可能返回视图,否则将返回副本。它也不能告诉什么时候会返回什么,但是你可以让你的代码在复制数据时引发错误。

For resize() function, numpy.resize()returns a new copy of array whereas ndarray.resize()does it in-place. But they don't go to viewthing.

对于 resize() 函数,numpy.resize()返回数组的新副本,而ndarray.resize()原地执行。但他们不去view事情。

Now coming to the point that what should be the values of extra elements. From the docs, it says

现在到点,额外元素的值应该是什么。从文档中,它说

If the new array is larger than the original array, then the new array is filled with repeated copies of a. Note that this behavior is different from a.resize(new_shape) which fills with zeros instead of repeated copies of a.

如果新数组大于原始数组,则新数组将填充 a 的重复副本。请注意,此行为与 a.resize(new_shape) 不同,后者填充零而不是 a 的重复副本。

So for ndarray.resize()it is the value 0, but for numpy.resize()it is the values of the array itself (of course whatever can fit in new size). Below code snippet will make it clear.

所以ndarray.resize()它是 value 0,但numpy.resize()它是数组本身的值(当然任何可以适应新大小的东西)。下面的代码片段将使其清晰。

In [40]: arr = np.array([1, 2, 3, 4])

In [41]: np.resize(arr, (2,5))
Out[41]:
array([[1, 2, 3, 4, 1],
      [2, 3, 4, 1, 2]])

In [42]: arr.resize((2,5))

In [43]: arr
Out[43]:
array([[1, 2, 3, 4, 0],
       [0, 0, 0, 0, 0]])

You can also see that ndarray.resize()returns Noneand does the resizing in-place.

您还可以看到ndarray.resize()返回None并就地调整大小。

回答by MarianD

  1. reshape()is able to change the shape only,notthe number of elements.

    If the array has 5 elements, we may use e.g. reshape(5, ), reshape(1, 5), reshape(1, 5, 1), but notreshape(2, 3).

    reshape()in general don't modify data themselves, only meta info about them.

    The .reshape()method(of ndarray) returns the reshaped array, keeping the original array untouched.

  2. resize()is able to change both the shape and the number of elements, too.

    So for an array with 5 elements we may use resize(5, 1), but also resize(2, 2)or resize(7, 9).

    The .resize()method(of ndarray) returns None, changing only the original array (so it seems as in-place change).

  1. reshape()只能改变形状,不能改变元素的数量。

    如果数组有 5 个元素,我们可以使用 eg reshape(5, ), reshape(1, 5), reshape(1, 5, 1), 但不能使用reshape(2, 3)

    reshape()通常不要修改数据本身,只修改关于它们的元信息。

    .reshape()方法(的ndarray)返回重构阵列,保持原有的阵列不变。

  2. resize()也可以改变形状和元素数量。

    因此,对于具有 5 个元素的数组,我们可以使用resize(5, 1), 但也可以使用resize(2, 2)resize(7, 9)

    .resize()方法(ndarray)返回None,仅更改原始数组(因此它看起来像就地更改)。

回答by Harrison Wang

One more point is: np.reshape can take -1 in one dimension. np.resize can't.

还有一点是: np.reshape 可以在一维中取-1。np.resize 不能。

example as below:

示例如下:

arr = np.arange(20)
arr.resize(5,2,2)
arr.reshape(2,2,-1)

回答by Harikrishnan M S

suppose you have the following np.ndarray :

假设您有以下 np.ndarray :

a = np.array([1,2,3,4]) # shape of this is (4,)

Now we try 'a.reshape'

现在我们尝试'a.reshape'

a.reshape(1,4)

array([[1, 2, 3, 4]])

a.shape         # This will again return (4,)

We see that the shape of a hasn't changed

我们看到 a 的形状没有改变

let's try 'a.resize' now

让我们现在尝试'a.resize'

a.resize(1,4)

a.shape         # Now the shape changes to (1,4)

'resize' changed the shape of our original numpy array a (It changes shape 'IN-PLACE').

'resize' 改变了我们原来的 numpy 数组 a 的形状(它改变了形状 'IN-PLACE')。