numpy.ndarray.copy () in Python

NumPy | Python Methods and Functions

numpy.ndarray.copy() returns a copy of the array.

Syntax: numpy.ndarray .copy (order = `C`)

Parameters:
order: Controls the memory layout of the copy. `C` means C-order, `F` means F-order, `A` means `F` if a is Fortran contiguous, `C` otherwise. `K` means match the layout of a as closely as possible.

Code # 1:

# Python program explaining
# numpy.ndarray.copy () function

  

import numpy as geek

 

 

x = geek.array ([[ 0 , 1 , 2 , 3 ], [ 4 , 5 , 6 , 7 ]],

order = ` F` )

print ( "x is:" , x)

 
# copy x to y

y = x.copy ()

print ( "y is:" , y)

print ( "x is copied to y " )

Output:

 xi s: [[0 1 2 3] [4 5 6 7]] y is: [[0 1 2 3] [4 5 6 7]] x is copied to y 

Code no. 2:

# Python program explaining
# numpy.ndarray. copy () function

 

import numpy as geek

 

 

x = geek.array ([[ 0 , 1 ,], [ 2 , 3 ]])

print ( " x is: " , x)

  
# copy x to y

y = x.copy ()

 
# filling x 1

x.fill ( 1 )

print ( " Now x is: " , x)

  

print ( "y is:" , y)

Exit:

 x is: [[0 1] [2 3]] Now x is: [[1 1] [1 1]] y is: [[0 1] [2 3]] 




Get Solution for free from DataCamp guru