import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
np.array([1, 2, 3])
array([1, 2, 3])
np.array([1, 2, 3], dtype=np.float32)
array([ 1., 2., 3.], dtype=float32)
np.array([[1, 2, 3], [4, 5, 6]])
array([[1, 2, 3], [4, 5, 6]])
np.arange(5, 10)
array([5, 6, 7, 8, 9])
np.linspace(5, 10, 10)
array([ 5. , 5.556, 6.111, 6.667, 7.222, 7.778, 8.333, 8.889, 9.444, 10. ])
np.empty((3, 2))
array([[ 0., 0.], [ 0., 0.], [ 0., 0.]])
np.zeros((3, 2))
array([[ 0., 0.], [ 0., 0.], [ 0., 0.]])
np.ones((3, 2))
array([[ 1., 1.], [ 1., 1.], [ 1., 1.]])
np.eye(3)
array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0., 0., 1.]])
np.diag([1, 2, 3])
array([[1, 0, 0], [0, 2, 0], [0, 0, 3]])
np.repeat(3, 5)
array([3, 3, 3, 3, 3])
np.repeat([[1, 2, 3]], 3, axis=0)
array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
a = np.arange(3 * 4).reshape(3, -1)
a.shape
(3, 4)
a.size
12
a.dtype
dtype('int64')
a.nbytes
96
np.random.seed(0)
np.random.rand(3, 4)
array([[ 0.549, 0.715, 0.603, 0.545], [ 0.424, 0.646, 0.438, 0.892], [ 0.964, 0.383, 0.792, 0.529]])
np.random.randn(3, 4)
array([[ 0.761, 0.122, 0.444, 0.334], [ 1.494, -0.205, 0.313, -0.854], [-2.553, 0.654, 0.864, -0.742]])
plt.hist(np.random.normal(10, 2.0, 1000));
plt.hist(np.random.binomial(1, 0.8, 1000));
v = np.arange(3)
v
array([0, 1, 2])
v.reshape(-1, 1) # column matrix
array([[0], [1], [2]])
v[:, np.newaxis] # column matrix
array([[0], [1], [2]])
v.reshape(1, -1) # row vector
array([[0, 1, 2]])
v[np.newaxis, :] # row vector
array([[0, 1, 2]])
r = v[np.newaxis, :] # returns view -> no copy
r[0, 0] = 10
r
array([[10, 1, 2]])
v
array([10, 1, 2])
A = np.arange(3 * 4).reshape(3, -1)
A
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
A.flatten()
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
A.flatten()[0] = 10
A
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
A.ravel()
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
A.ravel()[0] = 100
A
array([[100, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
A.ravel()
array([100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
a = np.arange(10)
a[1]
1
a[[0, 5]]
array([0, 5])
a[-1]
9
a[:5]
array([0, 1, 2, 3, 4])
a[5:]
array([5, 6, 7, 8, 9])
a[5::2]
array([5, 7, 9])
b = a[::2]
b[2] = -1
b
array([ 0, 2, -1, 6, 8])
a
array([ 0, 1, 2, 3, -1, 5, 6, 7, 8, 9])
a[a > 5]
array([6, 7, 8, 9])
A = np.arange(3 * 4).reshape(3, 4)
A
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
A[1, :]
array([4, 5, 6, 7])
b = A.take(1, axis=0)
A.take(1, axis=1)
array([1, 5, 9])
b
array([4, 5, 6, 7])
b[0] = 10
b
array([10, 5, 6, 7])
A
array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
A = np.arange(1, 7).reshape(3,2)
A
array([[1, 2], [3, 4], [5, 6]])
A * 2
array([[ 2, 4], [ 6, 8], [10, 12]])
A + 2
array([[3, 4], [5, 6], [7, 8]])
v = np.array([1, 2])
v
array([1, 2])
A.dot(v)
array([ 5, 11, 17])
v.dot(v)
5
A.sum()
21
A.sum(axis=0)
array([ 9, 12])
A.sum(axis=1)
array([ 3, 7, 11])
A.prod(axis=0)
array([15, 48])
A.mean(axis=0)
array([ 3., 4.])
A.std(axis=0)
array([ 1.633, 1.633])
A.min(axis=0)
array([1, 2])
A
array([[1, 2], [3, 4], [5, 6]])
# Scaling rows
r = np.array([[2, 3]])
A * r
array([[ 2, 6], [ 6, 12], [10, 18]])
# Scaling columns
c = np.array([1, 2, 3]).reshape(3, -1)
A * c
array([[ 1, 2], [ 6, 8], [15, 18]])
<img src='figures/bcast.png', align='left'/>
A = np.ones((2, 3, 5))
b = np.arange(3).reshape(3, 1)
A
array([[[ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.]], [[ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.], [ 1., 1., 1., 1., 1.]]])
b
array([[0], [1], [2]])
A + b
array([[[ 1., 1., 1., 1., 1.], [ 2., 2., 2., 2., 2.], [ 3., 3., 3., 3., 3.]], [[ 1., 1., 1., 1., 1.], [ 2., 2., 2., 2., 2.], [ 3., 3., 3., 3., 3.]]])
Ac = np.ones((10, 100000), order='C') # C order
Af = np.ones((10, 100000), order='F') # Fortran order
%timeit Ac.sum(axis=0)
1000 loops, best of 3: 895 µs per loop
%timeit Af.sum(axis=0)
100 loops, best of 3: 1.98 ms per loop
Ac = np.ones((100000, 10), order='C')
Af = np.ones((100000, 10), order='F')
%timeit Ac.sum(axis=1)
100 loops, best of 3: 2.08 ms per loop
%timeit Af.sum(axis=1)
1000 loops, best of 3: 886 µs per loop