Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

array
numpy
view()
python
communitycreator

What is the array.view() method from NumPy in Python?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Overview

The array.view() method in NumPy is used to make changes to an array without saving the original state of the array.

Syntax

array.view()

Parameter

The array.view() method takes no parameter.

Return value

The array.view() method returns the modified array but does not return the original array.

Example

from numpy import array
# creating an array
my_array = array([1, 2, 3, 4, 5])
# using the array.view() method
new_array = my_array.view()
# changing the 0 index of the array
new_array[0] = 9
print(my_array)
print(new_array)
Implementing the array.view() method

Explanation

  • Line 1: We import the array from the numpy module.
  • Line 4: We create an array using the array() method and assign its value to a variable my_array.
  • Line 7: We use the array.view() method to copy the array, my_array, to a new array new_array.
  • Line 10: We change the index 0 value of the array, my_array, to 9.
  • Line 11: We print the variable my_array.
  • Line 12: We print the variable new_array.

Note: The array.view() method does not save the old array after it is modified.

RELATED TAGS

array
numpy
view()
python
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring