Movatterモバイル変換


[0]ホーム

URL:


SciPy

numpy.ndarray.resize

ndarray.resize(new_shape,refcheck=True)

Change shape and size of array in-place.

Parameters:

new_shape : tuple of ints, orn ints

Shape of resized array.

refcheck : bool, optional

If False, reference count will not be checked. Default is True.

Returns:

None

Raises:

ValueError

Ifa does not own its own data or references or views to it exist,and the data memory must be changed.PyPy only: will always raise if the data memory must be changed, sincethere is no reliable way to determine if references or views to itexist.

SystemError

If theorder keyword argument is specified. This behaviour is abug in NumPy.

See also

resize
Return a new array with the specified shape.

Notes

This reallocates space for the data area if necessary.

Only contiguous arrays (data elements consecutive in memory) can beresized.

The purpose of the reference count check is to make sure youdo not use this array as a buffer for another Python object and thenreallocate the memory. However, reference counts can increase inother ways so if you are sure that you have not shared the memoryfor this array with another Python object, then you may safely setrefcheck to False.

Examples

Shrinking an array: array is flattened (in the order that the data arestored in memory), resized, and reshaped:

>>>a=np.array([[0,1],[2,3]],order='C')>>>a.resize((2,1))>>>aarray([[0],       [1]])
>>>a=np.array([[0,1],[2,3]],order='F')>>>a.resize((2,1))>>>aarray([[0],       [2]])

Enlarging an array: as above, but missing entries are filled with zeros:

>>>b=np.array([[0,1],[2,3]])>>>b.resize(2,3)# new_shape parameter doesn't have to be a tuple>>>barray([[0, 1, 2],       [3, 0, 0]])

Referencing an array prevents resizing...

>>>c=a>>>a.resize((1,1))Traceback (most recent call last):...ValueError:cannot resize an array that has been referenced ...

Unlessrefcheck is False:

>>>a.resize((1,1),refcheck=False)>>>aarray([[0]])>>>carray([[0]])

Previous topic

numpy.ndarray.reshape

Next topic

numpy.ndarray.round

  • © Copyright 2008-2009, The Scipy community.
  • Last updated on Jun 10, 2017.
  • Created usingSphinx 1.5.3.

[8]ページ先頭

©2009-2025 Movatter.jp