numpy.resize#
- numpy.resize(a,new_shape)[source]#
Return a new array with the specified shape.
If the new array is larger than the original array, then the newarray is filled with repeated copies ofa. Note that this behavioris different from a.resize(new_shape) which fills with zeros insteadof repeated copies ofa.
- Parameters:
- aarray_like
Array to be resized.
- new_shapeint or tuple of int
Shape of resized array.
- Returns:
- reshaped_arrayndarray
The new array is formed from the data in the old array, repeatedif necessary to fill out the required number of elements. Thedata are repeated iterating over the array in C-order.
See also
numpy.reshapeReshape an array without changing the total size.
numpy.padEnlarge and pad an array.
numpy.repeatRepeat elements of an array.
ndarray.resizeresize an array in-place.
Notes
When the total size of the array does not change
reshapeshouldbe used. In most other cases either indexing (to reduce the size)or padding (to increase the size) may be a more appropriate solution.Warning: This functionality doesnot consider axes separately,i.e. it does not apply interpolation/extrapolation.It fills the return array with the required number of elements, iteratingovera in C-order, disregarding axes (and cycling back from the start ifthe new shape is larger). This functionality is therefore not suitable toresize images, or data where each axis represents a separate and distinctentity.
Examples
>>>importnumpyasnp>>>a=np.array([[0,1],[2,3]])>>>np.resize(a,(2,3))array([[0, 1, 2], [3, 0, 1]])>>>np.resize(a,(1,4))array([[0, 1, 2, 3]])>>>np.resize(a,(2,4))array([[0, 1, 2, 3], [0, 1, 2, 3]])