Python NumPy ndarray.view() Function

NumPy ndarray.view() Function:

The ndarray.view() function of the NumPy module returns an array view. The generated view has a different id, but any changes to the view will have an impact on the original array.

Syntax:

ndarray.view(dtype=None, type=None)

Parameters

dtype: This is optional. It is the data-type descriptor for the returned view, such as float32 or int16. If it is not specified, the view has the same data type as the original array.

type: This is optional. It is the type of returned view, such as ndarray or matrix. Again, the parameter’s omission results in type preservation.

Return value:

A view of the array is Returned.

NumPy ndarray.view() Function in Python

Example

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function of the numpy module to create an array.
  • Store it in a variable.
  • Create a view of above-given array using the view() function.
  • Store it in another variable.
  • Print the id of the given array using the id() function by passing the array as an argument to it.
  • Print the given original array.
  • Print the id of the above-obtained array view using the id() function by passing the array as an argument to it.
  • Print the above-obtained array view.
  • Change the data of the above array view with some random number. Here it will change both the array view data and the given original array.
  • Print the given original array after modification.
  • Print the above array view after modification.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function of the 
# numpy module to create an array.
# Store it in a variable.
gvn_arry = np.array([[9,8,7],
              [6,5,4]])

# Create a view of above given array using the view() function
# Store it in another variable.
arry_view = gvn_arry.view()

# Print the id of the given array using the id() function by passing the
# array as an argument to it
print("The id of the given array = ", id(gvn_arry))
# Print the given original array
print("The given original array is:")
print(gvn_arry)

# Print the id of the above obtained array view using the id() function by passing the
# array as an argument to it
print("The id of the above obtained array view = ", id(arry_view))
# Print the above obtained array view
print("The above obtained array view is:")
print(arry_view)

# Change the data of the above array view with some random number
# Here it will change both the array view data and the given original array
arry_view[1,1] = 50

# Print the given original array after modification
print("The given original array  after modification is:")
print(gvn_arry)

# Print the above array view after modification
print("The above array view  after modification is:")
print(arry_view)

Output:

The id of the given array = 140684693927376
The given original array is:
[[9 8 7]
 [6 5 4]]
The id of the above obtained array view = 140684693928016
The above obtained array view is:
[[9 8 7]
 [6 5 4]]
The given original array after modification is:
[[ 9 8 7]
 [ 6 50 4]]
The above array view after modification is:
[[ 9 8 7]
 [ 6 50 4]]