import numpy as np
%precision 2
ipython_plain = get_ipython().display_formatter.formatters['text/plain']
ipython_plain.for_type(np.float64, ipython_plain.lookup_by_type(float));
x = np.arange(3) ; print(x, x.dtype)
x = np.arange(3.0) ; print(x, x.dtype)
print( np.arange(1, 3, 0.5) )
[0 1 2] int64 [0. 1. 2.] float64 [1. 1.5 2. 2.5]
v3 = np.array([1, 2, 3])
v3
array([1, 2, 3])
m33 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
m33
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
from scipy.sparse import csr_matrix
sparse = csr_matrix(np.array([[0, 0, 1]]))
(sparse + sparse)[0, 2]
2
v2 = np.array([1, 2])
v2a = v2
v2a[0] = 3
v2
# NOTE: Direct assignment results in a reference!
array([3, 2])
v2b = v2 + 1
v2b[0] = 5
v2
# NOTE: Arithmetic operations results in a copy!
array([3, 2])
v2c = v2.copy()
v2c[0] = 0
v2
array([3, 2])
np.column_stack((v3, v3))
array([[1, 1],
[2, 2],
[3, 3]])
np.hstack((v3, v3))
array([1, 2, 3, 1, 2, 3])
np.hstack((m33, m33))
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[7, 8, 9, 7, 8, 9]])
np.vstack((v3, v3))
array([[1, 2, 3],
[1, 2, 3]])
m33.shape
(3, 3)
m33.ndim
2
m33.size
9
v3[2]
3
m33[2, 2]
9
print( m33[:, 2] )
print( m33[2, :] )
# NOTE: Both Slices Return 1-D *Vectors*
[3 6 9] [7 8 9]
print( m33[:, 2:3] )
print( m33[2:3, :] )
# NOTE: Both Slices Return 2-D *Matrixes*
[[3] [6] [9]] [[7 8 9]]
m33[1]
# NOTE: Returns 1-D *Vector*
array([4, 5, 6])
m33[1:3]
# NOTE: Returns 2-D *Matrix*
array([[4, 5, 6],
[7, 8, 9]])
print( m33[1:2] )
print( m33[[1]] )
# NOTE: Returns 2-D *Matrix*
[[4 5 6]] [[4 5 6]]
vect_fn = np.vectorize(lambda x: -x if x < 3 else 0)
vect_fn(m33)
array([[-1, -2, 0],
[ 0, 0, 0],
[ 0, 0, 0]])
m33
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print( m33.min() )
print( m33.max() )
1 9
print( m33.max(axis=0) )
print( m33.max(axis=1) )
# NOTE: Both Return 1-D *Vectors*
[7 8 9] [3 6 9]
m33.mean(), m33.std(), m33.var()
(5.00, 2.58, 6.67)
m23 = np.array([[1, 2, 3],
[4, 5, 6]])
m23
array([[1, 2, 3],
[4, 5, 6]])
m23.reshape(3, 2)
# Scan Order: Left-to-Right, Top-to-Bottom
array([[1, 2],
[3, 4],
[5, 6]])
m23.reshape(1, -1)
array([[1, 2, 3, 4, 5, 6]])
print( m23.ravel() )
print( m23.reshape(-1) )
print( m23.flatten() )
# NOTE: All Return 1-D *Vectors*
[1 2 3 4 5 6] [1 2 3 4 5 6] [1 2 3 4 5 6]
m23.T
array([[1, 4],
[2, 5],
[3, 6]])
v3, v3.T
# NOTE: Vectors *CANNOT* be Transposed!
(array([1, 2, 3]), array([1, 2, 3]))
v3[None]
# wrap/encapsulate inside an additional outer dimension
array([[1, 2, 3]])
v3.reshape(-1, 1)
# v3[None].T
# convert vector to" column vector"
array([[1],
[2],
[3]])
x = np.array([[[0], [1], [2]]])
print( x.shape )
print( np.squeeze(x).shape )
print( np.squeeze(x, axis=0).shape )
print( np.squeeze(x, axis=2).shape )
print( np.array([[100]]).squeeze() )
# remove axis of length 1
(1, 3, 1) (3,) (3, 1) (1, 3) 100
np.triu(m33), np.tril(m33)
(array([[1, 2, 3],
[0, 5, 6],
[0, 0, 9]]),
array([[1, 0, 0],
[4, 5, 0],
[7, 8, 9]]))
v3
array([1, 2, 3])
v3 @ v3
# vector dot product (1*1 + 2*2 + 3*3)
14
m33 @ m33
# matrix multiplication
array([[ 30, 36, 42],
[ 66, 81, 96],
[102, 126, 150]])
m33 * m33
# element-wise multiplication
array([[ 1, 4, 9],
[16, 25, 36],
[49, 64, 81]])
np.linalg.inv(np.array([[1,0], [0,2]]))
# matrix inverse
array([[1. , 0. ],
[0. , 0.5]])
np.linalg.pinv(np.array([[1,0], [0,2]]))
# matrix pseudo-inverse
array([[1. , 0. ],
[0. , 0.5]])
np.random.seed(0)
np.random.random(3)
# uniform between 0.0 and 1.0
array([0.55, 0.72, 0.6 ])
[np.random.randint(3) for _ in range(10)]
# np.random.randint(<max-excl>)
[1, 1, 2, 0, 2, 0, 0, 0, 2, 1]
[np.random.randint(1, 4) for _ in range(10)]
# np.random.randint(<min-incl>, <max-excl>)
[3, 3, 1, 2, 2, 2, 2, 1, 2, 1]
np.random.uniform(1, 5, 10)
# np.random.uniform(<min>, <max>, <size>)
array([1.08, 4.33, 4.11, 4.48, 4.91, 4.2 , 2.85, 4.12, 1.47, 3.56])
np.random.normal(5, 1, (3, 4))
# np.random.normal(<mean>, <stddev>, <size>)
array([[2.45, 5.65, 5.86, 4.26],
[7.27, 3.55, 5.05, 4.81],
[6.53, 6.47, 5.15, 5.38]])
np.random.choice(v3, size=5, replace=True)
# 'replace': with/without replacement
array([3, 1, 2, 2, 2])
x = np.arange(5)
np.random.shuffle(x)
x
# shuffle *in-place*
array([0, 4, 2, 3, 1])
m23
array([[1, 2, 3],
[4, 5, 6]])
m23 < 4
array([[ True, True, True],
[False, False, False]])
(m23 < 2) | (m23 > 4)
array([[ True, False, False],
[False, True, True]])
m23[(m23 < 4) & (m23 >= 1)]
# NOTE: Returns 1-D *Vector*
array([1, 2, 3])
np.where(m23 < 4, 1, -1)
# np.where(<condition>, <val_if_true>, <val_if_false>)
array([[ 1, 1, 1],
[-1, -1, -1]])
v10 = np.linspace(0, 5, 6)
v10
# np.linspace(<start-incl>, <stop-incl>, <num_samples>)
array([0., 1., 2., 3., 4., 5.])
np.percentile(v10, [25, 50, 100])
# Returns value(s) corresponding to given percentile(s)
array([1.25, 2.5 , 5. ])
x = np.array([[1, 2, 1], [1, 3, 1], [1, 2, 1]])
print('x:\n', x)
print('Unique Elements:\n', np.unique(x))
print('Unique Rows:\n', np.unique(x, axis=0))
print('Unique Cols:\n', np.unique(x, axis=1))
x: [[1 2 1] [1 3 1] [1 2 1]] Unique Elements: [1 2 3] Unique Rows: [[1 2 1] [1 3 1]] Unique Cols: [[1 2] [1 3] [1 2]]