In [1]:
import numpy as np

In [2]:
a_2d = np.arange(12).reshape(3, 4)
print(a_2d)


[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

In [3]:
a_slice = a_2d[:2, :2]
print(a_slice)


[[0 1]
 [4 5]]

In [4]:
print(np.shares_memory(a_2d, a_slice))


True

In [5]:
a_slice[0, 0] = 100
print(a_slice)


[[100   1]
 [  4   5]]

In [6]:
print(a_2d)


[[100   1   2   3]
 [  4   5   6   7]
 [  8   9  10  11]]

In [7]:
a_2d[0, 0] = 0
print(a_2d)


[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

In [8]:
print(a_slice)


[[0 1]
 [4 5]]

In [9]:
a_fancy_index = a_2d[[0, 1]]
print(a_fancy_index)


[[0 1 2 3]
 [4 5 6 7]]

In [10]:
print(np.shares_memory(a_2d, a_fancy_index))


False

In [11]:
a_fancy_index[0, 0] = 100
print(a_fancy_index)


[[100   1   2   3]
 [  4   5   6   7]]

In [12]:
print(a_2d)


[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

In [13]:
a_slice_copy = a_2d[:2, :2].copy()
print(a_slice_copy)


[[0 1]
 [4 5]]

In [14]:
print(np.shares_memory(a_2d, a_slice_copy))


False

In [15]:
a_slice_copy[0, 0] = 100
print(a_slice_copy)


[[100   1]
 [  4   5]]

In [16]:
print(a_2d)


[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]

In [17]:
a_fancy_index_slice = a_2d[[0, 1], :3]
print(a_fancy_index_slice)


[[0 1 2]
 [4 5 6]]

In [18]:
print(np.shares_memory(a_2d, a_fancy_index_slice))


False

In [19]:
a_scalar_slice = a_2d[1, :3]
print(a_scalar_slice)


[4 5 6]

In [20]:
print(np.shares_memory(a_2d, a_scalar_slice))


True