NumPy Tutorial NumPy Statistics NumPy Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

NumPy - ndarray.view() function



The NumPy ndarray.view() returns a copy of the array. The syntax for using this function is given below:

Syntax

numpy.ndarray.copy(order='C')

Parameters

order Optional. Specify the memory layout of the copy. It can take values from {'C', 'F', 'A', 'K'}. The default is 'C'.
  • 'C' - for C order (row major).
  • 'F' - for F order (column major).
  • 'A' - F if a is Fortran contiguous, C otherwise.
  • 'K' - match the layout of a as closely as possible.

Return Value

Returns a copy of the array.

Example:

In the example below, ndarray.view() is used to copy a given array.

import numpy as np

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

#creating a copy of array x
y = x.view()

#displaying arrays
print("x =")
print(x)
print("\ny =")
print(y)

#performing operation on y
#this will change y but not x
y = y + 50

#displaying arrays
print("\nx =")
print(x)
print("\ny =")
print(y)

The output of the above code will be:

x =
[[1 2 3]
 [4 5 6]
 [7 8 9]]

y =
[[1 2 3]
 [4 5 6]
 [7 8 9]]

x =
[[1 2 3]
 [4 5 6]
 [7 8 9]]

y =
[[51 52 53]
 [54 55 56]
 [57 58 59]]

❮ NumPy - Functions

5