Wednesday, July 3, 2024
HomeLanguagesPythonnumpy.ndarray.copy() in Python

numpy.ndarray.copy() in Python

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: \n", x)
Ā Ā 
# copying x to y
y = x.copy()
print("y is :\n", y)
print("\nx is copied to y")


Output:

x is: 
 [[0 1 2 3]
 [4 5 6 7]]
y is :
 [[0 1 2 3]
 [4 5 6 7]]

x is copied to y

Ā 
Code #2:




# Python program explainingĀ Ā 
# numpy.ndarray.copy() function
Ā Ā 
import numpy as geek
Ā Ā 
Ā Ā 
x = geek.array([[0, 1, ], [2, 3]])
print("x is:\n", x)
Ā Ā 
# copying x to y
y = x.copy()
Ā Ā 
# filling x with 1's
x.fill(1)
print("\n Now x is : \n", x)
Ā Ā 
print("\n y is: \n", y)


Output:

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

 Now x is : 
 [[1 1]
 [1 1]]

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

Calisto Chipfumbu
Calisto Chipfumbuhttp://cchipfumbu@gmail.com
I have 5 years' worth of experience in the IT industry, primarily focused on Linux and Database administration. In those years, apart from learning significant technical knowledge, I also became comfortable working in a professional team and adapting to my environment, as I switched through 3 roles in that time.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments