suppose that x is a 2d numpy array which of the following creates deep copy in y in python

We can create deep copy of numpy array using numpy.copy() function.

main.py
import numpy as np

# Suppose x is the 2D numpy array
x = np.array([[1, 2], [3, 4]])

# Creating deep copy of array x
y = np.copy(x)

# Modifying the copy of array y
y[0][0] = 10

print("Array x:", x)
print("Array y:", y)
222 chars
14 lines

Output:

main.py
Array x: [[1 2]
 [3 4]]
Array y: [[10  2]
 [ 3  4]]
52 chars
5 lines

As we can see that, after modifying the copy of numpy array y, numpy array x remains unchanged.

related categories

gistlibby LogSnag