Jérémie Decock (www.jdhp.org)
This notebook is inspired by the following page: https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html
import numpy as np
import math
np.array([1, 2, 3])
np.array([[1, 2, 3],[4, 5, 6]])
np.zeros(3)
np.zeros((3, 4))
np.ones(3)
np.ones((3, 4))
np.eye(3)
np.arange(10)
np.arange(10, 20)
np.arange(10, 20, 2)
np.linspace(0., 2., 5)
xx, yy = np.meshgrid([1, 2, 3], [4, 5, 6])
print(xx)
print()
print(yy)
np.random.rand(3)
np.random.rand(3, 4)
np.random.poisson(10, size=[3, 4])
mu = np.array([0., 0.])
cov = np.array([[1., 0.3],
[0.3, 1.]])
num_points = 10
np.random.multivariate_normal(mu, cov, num_points)
np.get_printoptions()
default_threshold = np.get_printoptions()["threshold"]
default_threshold
Arrays with more than default_threshold
elements are truncated.
max_size = math.ceil(math.sqrt(default_threshold))
max_size
a = np.random.randint(1, size=[max_size + 1, max_size + 1])
a
Print the full array (set threshold to infinity):
np.set_printoptions(threshold=np.inf)
a
Go back to the default threshold:
np.set_printoptions(threshold=default_threshold)
a
a = np.array([[1, 2, 3],[4, 5, 6]])
Number of dimensions:
a.ndim
Number of elements:
a.size
Number of elements per dimension:
a.shape
l = [[1, 2, 3],[4, 5, 6]]
a = np.array([[1, 2, 3],[4, 5, 6]])
np.array(l)
a.tolist()
a = np.array([[1, 2, 3],[4, 5, 6]])
a
b = a.copy()
b
a[0,0] = 10
print(a)
print(b)
a = np.array([[1, 2, 3],[4, 5, 6]])
a
b = a.astype('float64', copy=True)
b
a[0,0] = 10
print(a)
print(b)
a = np.arange(6)
a
a[0]
a[-1]
a[1:4]
a = np.array([[1, 2, 3, 4, 5, 6],
[10, 20, 30, 40, 50, 60],
[100, 200, 300, 400, 500, 600]])
a
a[0,1]
a[1, :]
a[1, ::2]
a[:, 1]
a[0:2, 2:4]
a[1:, 1:]
a[:-1, :-1]
"The ellipsis is used to slice high-dimensional data structures.
It's designed to mean at this point, insert as many full slices (:) to extend the multi-dimensional slice to all dimensions."
https://stackoverflow.com/questions/118370/how-do-you-use-the-ellipsis-slicing-syntax-in-python
a = np.arange(2**3).reshape(2, 2, 2)
a
To select all first elements in the last (3rd) dimension
a[..., 0]
is equivalent to
a[:, :, 0]
To select all first elements in the first (1st) dimension
a[0, ...]
is equivalent to
a[0, :, :]
a = np.array([[1, 2, 3, 4, 5, 6],
[10, 20, 30, 40, 50, 60],
[100, 200, 300, 400, 500, 600]])
a
(a>5)
np.nonzero(a>5)
a * (a<=5)
a[a>5] = 0
a
a = np.array([[-1, 7, 3], [-11, -5, 20]])
a
a[(a > -10) & (a < 10)] = 0
a
a[(a < -10) | (a > 10)] = 1
a
a = np.array([[-1, 7, 3], [-11, -5, 20]])
a
m1 = (a > -10)
m2 = (a < 10)
print(m1)
print(m2)
print(m1 & m2)
a[m1 & m2] = 0
a
a = np.array([])
a = np.append(a, 3)
a
It's probably not a good idea to use np.append
to often as it makes a copy of the array each time it is called...
%%timeit
a = np.array([])
for i in range(10000):
a = np.append(a, i)
Lists use a different data structure that makes them more efficient for repeated additions...
%%timeit
l = []
for i in range(10000):
l.append(i)
a = np.array(l)
In this case, the better option is probably the following:
%%timeit
a = np.array([i for i in range(10000)])
a = np.zeros(3)
b = np.ones(3)
print("a:", a)
print("b:", b)
np.concatenate([a, b])
np.hstack([a, b])
a = np.zeros([2, 3])
b = np.ones([2, 3])
a
b
Using vstack:
np.vstack([a, b])
Using concatenate:
np.concatenate([a, b], axis=0)
Using hstack:
np.hstack([a, b])
Using concatenate:
np.concatenate([a, b], axis=1)
The axis
parameter specifies the index of the new axis in the dimensions
of the result.
a = np.zeros([2, 3])
b = np.ones([2, 3])
a
b
np.stack([a, b], axis=0)
np.stack([a, b], axis=0).shape
np.stack([a, b], axis=1)
np.stack([a, b], axis=1).shape
np.stack([a, b], axis=2)
np.stack([a, b], axis=2).shape
a = np.array([[1, 2, 3], [4, 5, 6]])
np.tile(a, (2, 3))
a = np.array([[1, 2, 3], [4, 5, 6]])
a.T
a.flatten()
a = np.arange(6)
a
a.reshape([-1, 1])
a.reshape([2, 3])
a.reshape([3, 2])
a = np.arange(3)
a
np.repeat(a, 5)
a = np.arange(3).reshape([-1, 1])
a
np.repeat(a, 5, axis=0)
a = np.array([[1, 3, 5],[2, 4, 6]])
a
np.repeat(a, 5, axis=0)
a = np.array([8, 5, 1])
a
a.argsort()
a = np.array([[4, 4, 2],
[8, 5, 1],
[7, 0, 0],
[3, 1, 1],
[3, 0, 5]])
a
n = 0 # the column sorted by
a[a[:,n].argsort()]
n = 1 # the column sorted by
a[a[:,n].argsort()]
n = 2 # the column sorted by
a[a[:,n].argsort()]
a = np.array([[1, 2, 3], [4, 5, 6]])
Change the axis
value in the following functions to aggregate along a given axis.
np.sum(a, axis=None)
np.cumsum(a, axis=None)
np.diff(a.ravel())
np.mean(a, axis=None)
np.var(a, axis=None)
np.std(a, axis=None)
np.median(a, axis=None)
np.min(a, axis=None)
np.max(a, axis=None)
np.prod(a, axis=None)
np.cumprod(a, axis=None)
a = np.array([1, 1, 3, 2, 2, 2])
a
All but the last (righthand-most) bin is half-open. In other words,
if bins
is:
[1, 2, 3, 4]
then the first bin is [1, 2)
(including 1, but excluding 2) and
the second [2, 3)
. The last bin, however, is [3, 4]
, which
includes 4.
bins = np.array([1, 2, 3, 4])
bins
hist, bins_ = np.histogram(a, bins=bins)
hist
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
np.dot(a, b)
a.dot(b)
a = np.random.normal(size=(3, 3))
a
np.linalg.inv(a)
a = np.random.normal(size=(3, 3))
a
np.linalg.eig(a)
a = np.random.normal(size=(3, 3))
a
U, s, V = np.linalg.svd(a)
print(U, s, V)
a = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
np.linalg.solve(a, b)
Extract the diagonal:
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
np.diag(a)
Make a diagonal matrix:
d = np.array([1, 2, 3])
np.diag(d)
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
np.trace(a)
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
np.triu(a)
np.tril(a)
a = np.arange(-2., 2., 0.5)
a.dtype
a = np.arange(-2., 2., 0.5)
Per item:
a.itemsize
Full array:
a.nbytes
a = np.zeros(3)
a.dtype
a = np.zeros(3, dtype=np.bool)
a.dtype
a = np.zeros(3, dtype=np.int)
a.dtype
a = np.zeros(3, dtype=np.int8)
a.dtype
a = np.zeros(3, dtype=np.uint8)
a.dtype
a = np.arange(-2., 2., 0.5)
a
a.astype(np.bool)
a.astype(np.int)
a.astype(np.int8)
a.astype(np.uint8)
a = np.array([[np.nan, 2, 3], [1, np.nan, 6]])
a
a.min()
np.nanmin(a)
a.max()
np.nanmax(a)
a.mean()
np.nanmean(a)
a.shape
ma = np.ma.masked_where(np.isnan(a), a)
ma
ma.min()
ma.max()
ma.mean()
ma.shape