View and Copy¶
In [1]:
%pip install numpy
import numpy as np
Requirement already satisfied: numpy in /home/samrat/Documents/numpy-torch-tutorials/venv/lib/python3.8/site-packages (1.24.4) Note: you may need to restart the kernel to use updated packages.
In [2]:
squid = np.arange(12).reshape(3,-1)
squid
Out[2]:
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
In [5]:
ward = squid[:, :2]
ward
Out[5]:
array([[0, 1], [4, 5], [8, 9]])
In [7]:
sponge = squid[:, [0,1]]
sponge
Out[7]:
array([[0, 1], [4, 5], [8, 9]])
In [8]:
sponge[0, 0] = 100
sponge, squid, ward
Out[8]:
(array([[100, 1], [ 4, 5], [ 8, 9]]), array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]), array([[0, 1], [4, 5], [8, 9]]))
In [9]:
ward[0, 0] = 100
squid, ward
Out[9]:
(array([[100, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]), array([[100, 1], [ 4, 5], [ 8, 9]]))
In [10]:
squid.__array_interface__
Out[10]:
{'data': (31710704, False), 'strides': None, 'descr': [('', '<i8')], 'typestr': '<i8', 'shape': (3, 4), 'version': 3}
In [11]:
sponge.__array_interface__
Out[11]:
{'data': (41664864, False), 'strides': (8, 24), 'descr': [('', '<i8')], 'typestr': '<i8', 'shape': (3, 2), 'version': 3}
In [12]:
ward.__array_interface__
Out[12]:
{'data': (31710704, False), 'strides': (32, 8), 'descr': [('', '<i8')], 'typestr': '<i8', 'shape': (3, 2), 'version': 3}
In [13]:
np.shares_memory(squid, ward)
Out[13]:
True
In [14]:
np.shares_memory(squid, sponge)
Out[14]:
False
In [15]:
squid[:, :2].copy()
Out[15]:
array([[100, 1], [ 4, 5], [ 8, 9]])