Skip to main content

reshape

Gives a new shape to an array without changing its data.
numpy.reshape(a, /, shape, order='C', *, copy=None)
a
array_like
Array to be reshaped.
shape
int or tuple of ints
The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.
order
{'C', 'F', 'A'}
default:"'C'"
Read the elements of a using this index order, and place the elements into the reshaped array using this index order.
  • 'C' - Read/write in C-like (row-major) order, with the last axis index changing fastest
  • 'F' - Read/write in Fortran-like (column-major) order, with the first index changing fastest
  • 'A' - Read/write in Fortran-like order if a is Fortran contiguous in memory, C-like order otherwise
copy
bool
default:"None"
If True, then the array data is copied. If None, a copy will only be made if required by order. For False it raises a ValueError if a copy cannot be avoided.
Returns: ndarray - The reshaped array. This will be a new view object if possible; otherwise, it will be a copy.

Examples

import numpy as np

# Reshape 2D array to 1D
a = np.array([[1,2,3], [4,5,6]])
print(np.reshape(a, 6))
# array([1, 2, 3, 4, 5, 6])

# Use -1 to infer dimension
print(np.reshape(a, (3, -1)))
# array([[1, 2],
#        [3, 4],
#        [5, 6]])

# Fortran-style ordering
print(np.reshape(a, 6, order='F'))
# array([1, 4, 2, 5, 3, 6])

See Also

  • ndarray.reshape - Equivalent method
  • ravel - Return a flattened array

ravel

Return a contiguous flattened array.
numpy.ravel(a, order='C')
a
array_like
Input array. The elements in a are read in the order specified by order, and packed as a 1-D array.
order
{'C', 'F', 'A', 'K'}
default:"'C'"
The elements of a are read using this index order.
  • 'C' - Index in row-major, C-style order
  • 'F' - Index in column-major, Fortran-style order
  • 'A' - Read in Fortran-like order if a is Fortran contiguous in memory, C-like order otherwise
  • 'K' - Read in the order the elements occur in memory, except for reversing the data when strides are negative
Returns: ndarray - A contiguous 1-D array of the same subtype as a, with shape (a.size,). A copy is made only if needed.

Examples

import numpy as np

x = np.array([[1, 2, 3], [4, 5, 6]])
print(np.ravel(x))
# array([1, 2, 3, 4, 5, 6])

# Equivalent to reshape(-1)
print(x.reshape(-1))
# array([1, 2, 3, 4, 5, 6])

# Fortran-style ordering
print(np.ravel(x, order='F'))
# array([1, 4, 2, 5, 3, 6])

# Preserve array's ordering
print(np.ravel(x.T, order='A'))
# array([1, 2, 3, 4, 5, 6])

See Also

  • ndarray.flat - 1-D iterator over an array
  • ndarray.flatten - 1-D array copy in row-major order
  • reshape - Change the shape without changing data

ndarray.flat

A 1-D iterator over the array.
ndarray.flat
This is a numpy.flatiter instance, which acts as an iterator over the array. It returns elements in C-contiguous style (row-major order).

Examples

import numpy as np

x = np.array([[1, 2, 3], [4, 5, 6]])

# Iterate over all elements
for element in x.flat:
    print(element, end=' ')
# Output: 1 2 3 4 5 6

# Index into flat
print(x.flat[3])
# 4

# Assign to flat
x.flat = 0
print(x)
# array([[0, 0, 0],
#        [0, 0, 0]])

See Also

  • ravel - Return a flattened array
  • ndarray.flatten - Return a copy of the array collapsed into one dimension

ndarray.flatten

Return a copy of the array collapsed into one dimension.
ndarray.flatten(order='C')
order
{'C', 'F', 'A', 'K'}
default:"'C'"
The order to read the elements of the array.
  • 'C' - Flatten in row-major (C-style) order
  • 'F' - Flatten in column-major (Fortran-style) order
  • 'A' - Flatten in column-major order if a is Fortran contiguous, row-major otherwise
  • 'K' - Flatten in the order the elements occur in memory
Returns: ndarray - A copy of the input array, flattened to one dimension.

Examples

import numpy as np

a = np.array([[1,2], [3,4]])

# Flatten with default (C) order
print(a.flatten())
# array([1, 2, 3, 4])

# Flatten with Fortran order
print(a.flatten('F'))
# array([1, 3, 2, 4])

# flatten() always returns a copy
b = a.flatten()
b[0] = 99
print(a[0, 0])  # Original is unchanged
# 1

See Also

  • ravel - Return a flattened array (may return a view)
  • ndarray.flat - A 1-D flat iterator over the array

Build docs developers (and LLMs) love