مجموعات NumPy المستنسخة مقابل الرؤية

الفرق بين المستنسخ والرؤية

الفرق الرئيسي بين المستنسخ والرؤية بين المجموعات هو أن المستنسخ هو مجموعة جديدة، بينما الرؤية هي مجرد رؤية للمجموعة الأصلية.

المستنسخ يحتوي على بيانات، أي تغيير يتم إجراؤه على المستنسخ لن يؤثر على المجموعة الأصلية، وأي تغيير يتم إجراؤه على المجموعة الأصلية لن يؤثر على المستنسخ.

The view does not have data, any changes made to the view will affect the original array, and any changes made to the original array will affect the view.

Copy:

Instance

Make a copy, change the original array, and then display two arrays:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy()
arr[0] = 61
print(arr) 
print(x)

Run Instance

The copy should not be affected by the changes made to the original array.

View:

Instance

Create a view, change the original array, and then display two arrays:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.view()
arr[0] = 61
print(arr) 
print(x)

Run Instance

The view should be affected by the changes made to the original array.

Changes in the view:

Instance

Create a view, change the view, and display two arrays:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.view()
x[0] = 31
print(arr) 
print(x)

Run Instance

The original array should be affected by the changes made to the view.

Check if the array has data

As described above, the copy has data, while the view does not have data, but how do we check?

Each NumPy array has a property baseIf the array has data, then this base attribute returns None.

Otherwise,base The attribute will refer to the original object.

Instance

Print the value of the base attribute to check if the array has its own data:

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy()
y = arr.view()
print(x.base)
print(y.base)

Run Instance

Copy Return None.

View Return Original Array