Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

Returns Copy or View?

Example

Determine whether the returned array is a copy or a view.

import numpy as np

arr = np.array([12345678])

print(arr.reshape(24).base)

The example above returns the original array, indicating that it is a view.

Unknown Dimension

You can include one “unknown” dimension in the reshape method, allowing you to leave one dimension unspecified. By using -1 as the value, NumPy will automatically determine the correct size for that dimension.

Example

Transform a 1-D array with 8 elements into a 3-D array with dimensions of 2×2 elements.

import numpy as np

arr = np.array([12345678])

newarr = arr.reshape(22, –1)

print(newarr)