NumPy ndarray.view()

The numpy.ndarray.view() method allows you to create a new view of an existing array with a different data type or as a different array subclass. This operation does not create a copy of the data but instead provides a new perspective on the same memory buffer.

Syntax

</>
Copy
ndarray.view([dtype][, type])

Parameters

ParameterTypeDescription
dtypedata-type or ndarray sub-class, optionalSpecifies the data type for the new view, such as float32 or int16. If omitted, the view retains the original data type.
typePython type, optionalSpecifies the type of the returned view (e.g., numpy.ndarray or numpy.matrix). If omitted, the original type is preserved.

Return Value

Returns a new view of the array with the specified data type or subclass, while sharing the same data buffer as the original array.


Examples

1. Creating a View with a Different Data Type

In this example, we create an integer array and view it as an array of 32-bit floating-point numbers.

</>
Copy
import numpy as np

# Create an array of integers
arr = np.array([1, 2, 3, 4], dtype=np.int32)

# Create a view of the array with a different data type (float32)
view_arr = arr.view(np.float32)

print("Original array:", arr)
print("Data type of original array:", arr.dtype)
print("Viewed array:", view_arr)
print("Data type of viewed array:", view_arr.dtype)

Output:

Original array: [1 2 3 4]
Data type of original array: int32
Viewed array: [1.e-45 3.e-45 4.e-45 6.e-45]
Data type of viewed array: float32

Since the memory layout remains unchanged, interpreting the integer values as floating-point numbers produces seemingly arbitrary small numbers.

2. Creating a View as a Different Array Type

Here, we create a view of an array as a numpy.matrix object.

</>
Copy
import numpy as np

# Create a NumPy array
arr = np.array([[1, 2], [3, 4]])

# Create a view as a matrix
matrix_view = arr.view(np.matrix)

print("Original array:\n", arr)
print("Type of original array:", type(arr))
print("Viewed matrix:\n", matrix_view)
print("Type of viewed matrix:", type(matrix_view))

Output:

Original array:
 [[1 2]
 [3 4]]
Type of original array: <class 'numpy.ndarray'>
Viewed matrix:
 [[1 2]
 [3 4]]
Type of viewed matrix: <class 'numpy.matrix'>

The data remains the same, but the view is now an instance of numpy.matrix instead of numpy.ndarray.

3. Demonstrating that Views Share Memory

Modifying elements in a view affects the original array because they share the same memory.

</>
Copy
import numpy as np

# Create a NumPy array
arr = np.array([10, 20, 30, 40])

# Create a view of the array
view_arr = arr.view()

# Modify the view
view_arr[0] = 99

print("Original array after modification:", arr)
print("Viewed array:", view_arr)

Output:

Original array after modification: [99 20 30 40]
Viewed array: [99 20 30 40]

The change in the view affects the original array, proving that they share the same data buffer.

4. Viewing an Array with a Different Data Layout

We can use view() to reinterpret the memory layout of an array.

</>
Copy
import numpy as np

# Create an array of 4 integers
arr = np.array([1, 2, 3, 4], dtype=np.int16)

# View the same data as bytes
byte_view = arr.view(np.uint8)

print("Original array:", arr)
print("Data type of original array:", arr.dtype)
print("Viewed array (as bytes):", byte_view)
print("Data type of viewed array:", byte_view.dtype)

Output:

Original array: [1 2 3 4]
Data type of original array: int16
Viewed array (as bytes): [1 0 2 0 3 0 4 0]
Data type of viewed array: uint8

Each 16-bit integer is stored as two consecutive bytes, and view(np.uint8) allows us to interpret them as raw bytes.