Explanation:
1. Creating the array
a = np.array([[1,2],[3,4]])
a is a 2x2 NumPy array:
[[1, 2],
[3, 4]]
Shape: (2,2)
2. Flattening the array
b = a.flatten()
.flatten() creates a new 1D array from a.
Important: It does not modify the original array and does not share memory with a.
b now is: [1, 2, 3, 4]
3. Modifying the flattened array
b[0] = 99
Changes the first element of b to 99:
b becomes: [99, 2, 3, 4]
a remains unchanged because flatten() returns a copy, not a view.
4. Printing the original array element
print(a[0,0])
Accesses the element at first row, first column of a.
Since a was not modified, a[0,0] is still 1.
Output:
1
Key Concept :
flatten() creates a copy of the array; ravel() creates a view.
Modifying a copy does not affect the original array.
Use ravel() if you want changes to reflect in the original array.


0 Comments:
Post a Comment