8 KiB
8 KiB
In [107]:
import numpy as np
def is_view(a):
return a.base is not None
def print_info(a):
txt = f"""
Is it a view? {is_view(a)}
dtype\t{a.dtype}
ndim\t{a.ndim}
shape\t{a.shape}
strides\t{a.strides}
"""
print(a)
print(txt)
x = np.arange(12).reshape(3, 4).copy()
print_info(x)
In [108]:
y = x[::2, :]
print_info(y)
In [109]:
y = x[1, :]
print_info(y)
In [110]:
y = x[1]
print_info(y)
In [111]:
y = x[[1, 2], [0, 3]]
print_info(y)
In [112]:
# Get the first and third row
y = x[[0, 2], :]
print_info(y)
In [113]:
y = x + 2
print_info(y)
In [114]:
y = x[[1, 2, 0], [1, 1, 2]]
print_info(y)
In [115]:
y = x.reshape((6, 2))
print_info(y)
In [116]:
y = x.T.reshape((6, 2))
print_info(y)
In [117]:
y = x.ravel()
print_info(y)
In [118]:
y = x.T.ravel()
print_info(y)
In [119]:
y = x[(x % 2) == 0]
print_info(y)
In [120]:
y = np.sort(x, axis=1)
print_info(y)
In [ ]:
In [ ]: