Numpy#
import numpy as np
np.random.seed(42)
print(np.__version__)
1.26.4
Array creation#
# creates an array from available memory space, and then fills it with zeros for your chosen dtype.
print(' np.zeros((3, 4))')
print(np.zeros((3, 4)))
print('\n np.ones((3, 4))')
print(np.ones((3, 4)))
print('\n np.eye(3)')
print(np.eye(3))
print('\n np.diag([1, 2, 3])')
print(np.diag([1, 2, 3]))
# creates an array from available memory space,
# leaving whatever values happened to be hanging around in memory as the values.
print('\n np.empty((3, 4))')
print(np.empty((3, 4)))
np.zeros((3, 4))
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
np.ones((3, 4))
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
np.eye(3)
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
np.diag([1, 2, 3])
[[1 0 0]
[0 2 0]
[0 0 3]]
np.empty((3, 4))
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
a = np.array([1,2,3,4])
print(a,"\n")
a = np.arange(9).reshape(3, 3)
print(a,"\n")
# Create a two-dimensional array of size 3 rows x 4 columns:
b = np.array([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]])
print(b,"\n")
[1 2 3 4]
[[0 1 2]
[3 4 5]
[6 7 8]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Random#
Numbers are created from 0 (inclusive) to 10 (exclusive)
rng = np.random.default_rng(seed=42)
rng.uniform(low=0, high=10, size=10) # 10 random numbers in [0, 10)
array([7.73956049, 4.3887844 , 8.5859792 , 6.97368029, 0.94177348,
9.75622352, 7.61139702, 7.86064305, 1.28113633, 4.50385938])
rng.integers(low=0, high=10, size=(4, 3))
array([[5, 3, 1],
[9, 7, 6],
[4, 8, 5],
[4, 4, 2]])
Concatenate#
https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
c = np.concatenate((a, b), axis=0)
print(c)
[[1 2]
[3 4]
[5 6]]
Append#
Parameters:
arr: Values are appended to a copy of this array.
values: These values are appended to a copy of arr.
axis (opt)
https://numpy.org/doc/stable/reference/generated/numpy.append.html
a = np.array([1, 2, 3, 4, 5, 6])
a = np.append(a, [7, 8, 9])
a
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
Astype#
a = a.astype(float)
Boolean Masks#
x = rng.integers(low=-5, high=10, size=10)
mask_mult_3 = (x > 0) & (x % 3 == 0)
print("x: ", x)
print("mask: ", mask_mult_3)
print("True values: ", x[mask_mult_3])
print("True indeces: ", np.where(mask_mult_3))
x: [ 3 1 2 3 -5 -3 -2 -4 1 5]
mask: [ True False False True False False False False False False]
True values: [3 3]
True indeces: (array([0, 3]),)
Broadcasting#
https://numpy.org/doc/stable/reference/ufuncs.html#broadcasting
Combine operations on numpy arrays that have different shapes but are compatible. Technically, A and 3 have different shapes: the former is a \(4 \times 3\) matrix, while the latter is a scalar (\(1 \times 1\)). However, they are compatible because Numpy knows how to extend—or broadcast—the value 3 into an equivalent matrix object of the same shape in order to combine them.
print(c,"\n\n",c+3)
c_row_means = np.mean(c, axis=0)
print("\n", c_row_means)
c_col_means = np.mean(c, axis=1)
print("\n", c_col_means)
print(c.shape, c_row_means.shape)
print(c.shape, c_col_means.shape)
print()
c_col_means2 = np.reshape(c_col_means, (len(c_col_means), 1))
print(c_col_means2, "=>", c_col_means2.shape)
print()
print("A - A_col_means2\n\n", c, "\n-", c_col_means2)
print("\n=>\n", c - c_col_means2)
[[1 2]
[3 4]
[5 6]]
[[4 5]
[6 7]
[8 9]]
[3. 4.]
[1.5 3.5 5.5]
(3, 2) (2,)
(3, 2) (3,)
[[1.5]
[3.5]
[5.5]] => (3, 1)
A - A_col_means2
[[1 2]
[3 4]
[5 6]]
- [[1.5]
[3.5]
[5.5]]
=>
[[-0.5 0.5]
[-0.5 0.5]
[-0.5 0.5]]
Indexing / Slicing#
When you slice a Numpy array, you are actually creating a view into that array. That means modifications through the view will modify the original array.
A = np.array ([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
], dtype=float)
print ("A[:, :] -> \n", A)
print ("\nA[0, :] ->\n", A[0, :])
print ("\nA[:, 0:1] ->\n", A[:, 0:1])
print ("\nA[:, 2:3] ->\n", A[:, 2:3])
A[:, :] ->
[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]
A[0, :] ->
[1. 2. 3.]
A[:, 0:1] ->
[[1.]
[4.]
[7.]]
A[:, 2:3] ->
[[3.]
[6.]
[9.]]
Functions#
npFunc = np.array([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]])
print("arr.ndim:", npFunc.ndim) # Number of array dimensions
print("arr.shape", npFunc.shape) # Rows x Columns
print("len(arr)", len(npFunc)) # Number of rows
print()
print("np.max:", np.max(npFunc))
print("np.min:",np.min(npFunc))
print("np.sum:",np.sum(npFunc))
print("np.mean:",np.mean(npFunc))
print("np.nanmean:",np.nanmean(npFunc)) # ignore NaN values
print("np.std:",np.std(npFunc))
print()
# aggregate along a dimension
print("Max in each column:", np.amax(npFunc, axis=0)) # i.e., aggregate along axis 0, the rows, producing column maxes
print("Max in each row:", np.amax(npFunc, axis=1)) # i.e., aggregate along axis 1, the columns, producing row maxes
arr.ndim: 2
arr.shape (3, 4)
len(arr) 3
np.max: 11
np.min: 0
np.sum: 66
np.mean: 5.5
np.nanmean: 5.5
np.std: 3.452052529534663
Max in each column: [ 8 9 10 11]
Max in each row: [ 3 7 11]
Universal functions#
Universal functions apply a given function elementwise to one or more Numpy objects.
print(-npFunc,"\n")
print("np.abs(c)","\n",npFunc, "\n==>\n", np.abs(npFunc),"\n")
print("maximum(c,d)\n",np.maximum(npFunc,npFunc),"\n")
# Create universal functions
def f(x):
from math import exp
return exp(-(x**2))
f_np = np.frompyfunc(f,1,1)
print(f_np(npFunc))
[[ 1 -9 -19 -29]
[ -39 -49 -59 -69]
[ -79 -89 -99 -109]]
np.abs(c)
[[ -1 9 19 29]
[ 39 49 59 69]
[ 79 89 99 109]]
==>
[[ 1 9 19 29]
[ 39 49 59 69]
[ 79 89 99 109]]
maximum(c,d)
[[ -1 9 19 29]
[ 39 49 59 69]
[ 79 89 99 109]]
[[0.36787944117144233 6.639677199580735e-36 1.6584104776811452e-157 0.0]
[0.0 0.0 0.0 0.0]
[0.0 0.0 0.0 0.0]]
Matrix Multiplication#
You can only multiply two matrices if the number of columns in the first matrix equals the number of rows in the second matrix.
mm = [0, 1, 2, 3]
mm = np.array(mm)
MM = [[ 0, 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[12, 13, 14, 15, 16, 17],
[18, 19, 20, 21, 22, 23]]
MM = np.array(MM)
print(mm.shape, MM.shape)
print("\n",mm)
mm = np.expand_dims(mm, axis=-1) # Add an extra dimension in the last axis.
print(mm,"\n")
print(mm.shape, MM.shape)
print(mm*MM)
#print(mm.dot(MM)) # shapes (4,1) and (4,6) not aligned: 1 (dim 1) != 4 (dim 0)
(4,) (4, 6)
[0 1 2 3]
[[0]
[1]
[2]
[3]]
(4, 1) (4, 6)
[[ 0 0 0 0 0 0]
[ 6 7 8 9 10 11]
[24 26 28 30 32 34]
[54 57 60 63 66 69]]