简体   繁体   English

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

[英]What is the difference between resize and reshape when using arrays in NumPy?

I have just started using NumPy.我刚刚开始使用 NumPy。 What is the difference between resize and reshape for arrays?数组的resizereshape什么区别?

Reshape doesn't change the data as mentioned here . Reshape 不会改变这里提到的数据。 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. reshape 后数组没有改变,只是输出一个临时数组 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.调整大小后,数组改变了它的形状。

One major difference is reshape() does not change your data, but resize() does change it.一个主要区别是reshape() 不会更改您的数据,但 resize() 会更改它。 resize() first accommodates all the values in the original array. resize() 首先容纳原始数组中的所有值。 After that, if extra space is there (or size of new array is greater than original array), it adds its own values.之后,如果有额外的空间(或新数组的大小大于原始数组),它会添加自己的值。 As @David mentioned in comments, what values resize() adds depends on how that is called.正如@David 在评论中提到的, resize() 添加的值取决于它的调用方式。

You can call reshape() and resize() function in the following two ways.您可以通过以下两种方式调用reshape()resize()函数。

numpy.resize()

ndarray.resize() - where ndarray is an n dimensional array you are resizing. ndarray.resize() - 其中ndarray是您正在调整大小的n维数组。

You can similarly call reshape also as numpy.reshape() and ndarray.reshape() .您也可以类似地将 reshape 称为numpy.reshape()ndarray.reshape() But here they are almost the same except the syntax.但在这里,除了语法之外,它们几乎相同。

One point to notice is that, reshape() will always try to return a view wherever possible, otherwise it would return a copy .需要注意的一点是, reshape() 将始终尽可能地尝试返回一个视图,否则它将返回一个副本 Also, it can't tell what will be returned when, but you can make your code to raise error whenever the data is copied.此外,它无法确定何时返回什么内容,但是您可以让代码在复制数据时引发错误。

For resize() function, numpy.resize() returns a new copy of the array whereas ndarray.resize() does it in-place.对于 resize() 函数, numpy.resize()返回数组的新副本,而ndarray.resize()就地进行。 But they don't go to the view thing.但是他们不会去view东西。

Now coming to the point that what the values of extra elements should be.现在谈到额外元素的值应该是什么。 From the documentation, it says从文档中,它说

If the new array is larger than the original array, then the new array is filled with repeated copies of a.如果新数组大于原始数组,则新数组将填充 a 的重复副本。 Note that this behavior is different from a.resize(new_shape) which fills with zeros instead of repeated copies of 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 the new size).所以对于ndarray.resize()它是值0 ,但对于numpy.resize()它是数组本身的值(当然,任何可以适应新大小的东西)。 The below code snippet will make it clear.下面的代码片段将说明这一点。

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 None and does the resizing in-place.您还可以看到ndarray.resize()返回None并就地调整大小。

  1. reshape() is able to change the shape only (ie the meta info), not the number of elements. reshape()只能改变形状(即元信息),而不是元素的数量。

    If the array has five elements, we may use eg reshape(5, ) , reshape(1, 5) ,如果数组有五个元素,我们可以使用例如reshape(5, ) , reshape(1, 5)
    reshape(1, 5, 1) , but not reshape(2, 3) . reshape(1, 5, 1) ,但不是reshape(2, 3)

    • reshape() in general don't modify data themselves, only meta info about them, reshape()通常不修改数据本身,只修改有关它们的元信息,

    • the .reshape() method (of ndarray) returns the reshaped array, keeping the original array untouched. .reshape()方法(ndarray 的)返回重新整形的数组,保持原始数组不变。

  2. resize() is able to change both the shape and the number of elements, too. resize()也可以改变元素的形状和数量。

    So for an array with five elements we may use resize(5, 1) , but also resize(2, 2) or resize(7, 9) .因此,对于具有五个元素的数组,我们可以使用resize(5, 1) ,但也可以使用resize(2, 2)resize(7, 9)

    • The .resize() method (of ndarray) returns None , changing only the original array (so it seems as an in-place change ). .resize()方法(ndarray 的)返回None更改原始数组(因此它似乎是就地更改)。

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')。

One more point is:还有一点是:

np.reshape can take -1 in one dimension. np.reshape在一维中可以取 -1。 np.resize can't. np.resize不能。

Example as below:示例如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM