Why is Numpy Faster Than Lists ?
- Less bytes of memory (Fixed Type)
- No type checking when iterating through objects
- Contiguous memory
- SIMD vector processing
- Effective cache utilization
Basics
x: 7
x ndim: 0
x shape: ()
x size: 1
x dtype: int64
x itemsize: 8
x nbytes: 8
x: [1.5 2.5 3.5 5. ]
x ndim: 1
x shape: (4,)
x size: 4
x dtype: float64
x itemsize: 8
x nbytes: 32
x:
[[1 2]
[3 4]]
x ndim: 2
x shape: (2, 2)
x size: 4
x dtype: int64
x itemsize: 8
x nbytes: 32
x:
[[[1 2]
[3 4]]
[[5 6]
[7 8]]]
x ndim: 3
x shape: (2, 2, 2)
x size: 8
x dtype: int64
x itemsize: 8
x nbytes: 64
np.zeros((3, 3)):
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
np.ones((3, 3)):
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
np.identity((3, 3)):
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
np.eye((3)):
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
np.random.random((3, 3)):
[[0.37454012 0.95071431 0.73199394]
[0.59865848 0.15601864 0.15599452]
[0.05808361 0.86617615 0.60111501]]
np.random.random_sample((3, 3)):
[[0.70807258 0.02058449 0.96990985]
[0.83244264 0.21233911 0.18182497]
[0.18340451 0.30424224 0.52475643]]
np.random.rand(4, 2):
[[0.43194502 0.29122914]
[0.61185289 0.13949386]
[0.29214465 0.36636184]
[0.45606998 0.78517596]]
np.random.randint(1, 6, size=(3, 3)):
[[3 4 4]
[1 3 5]
[3 5 1]]
np.full(shape=(4, 4), fill_value=100):
[[100 100 100 100]
[100 100 100 100]
[100 100 100 100]
[100 100 100 100]]
np.repeat(my_array, 5, axis=0):
[[1 2 3]
[1 2 3]
[1 2 3]
[1 2 3]
[1 2 3]]
Copying Arrays
[100. 2.5 3.5]
[100. 2.5 3.5]
[100. 2.5 3.5]
[1.5 2.5 3.5]
Indexing
x:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
x[0, 2]: 3
x:
[[ 1 2 99 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
x column 1: [ 2 6 10]
x row 0: [1 2 3 4]
x rows 0,1 & cols 1,2:
[[2 3]
[6 7]]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
rows_to_get: [0 1 2]
cols_to_get: [0 2 1]
indexed values: [ 1 7 10]
x:
[[1 2]
[3 4]
[5 6]]
x > 2:
[[False False]
[ True True]
[ True True]]
x[x > 2]:
[3 4 5 6]
Arithmetic
x + y:
[[10. 10.]
[10. 10.]]
x - y:
[[0. 0.]
[0. 0.]]
x * y:
[[25. 25.]
[25. 25.]]
x / y:
[[1. 1.]
[1. 1.]]
xˆ3:
[[125. 125.]
[125. 125.]]
x * 3:
[[15. 15.]
[15. 15.]]
Linear Algebra
Dot Product
(2, 3) · (3, 2) = (2, 2)
[[ 58. 64.]
[139. 154.]]
or using np.matmul(a, b):
[[ 58. 64.]
[139. 154.]]
Axis Operations
[[1 2]
[3 4]]
sum all: 10
sum axis=0: [4 6]
sum axis=1: [3 7]
min: 1
max: 6
min axis=0: [1 2 3]
min axis=1: [1 4]
Broadcast
z:
[10 30]
Transpose
x:
[[1 2 3]
[4 5 6]]
x.shape: (2, 3)
y:
[[1 4]
[2 5]
[3 6]]
y.shape: (3, 2)
Reshape
[[1 2 3 4 5 6]]
x.shape: (1, 6)
y:
[[1 2 3]
[4 5 6]]
y.shape: (2, 3)
z:
[[1 2 3]
[4 5 6]]
z.shape: (2, 3)
Joining
[[0.06505159 0.94888554 0.96563203]
[0.80839735 0.30461377 0.09767211]]
(2, 3)
[[0.06505159 0.94888554 0.96563203]
[0.80839735 0.30461377 0.09767211]
[0.06505159 0.94888554 0.96563203]
[0.80839735 0.30461377 0.09767211]]
(4, 3)
[[[0.06505159 0.94888554 0.96563203]
[0.80839735 0.30461377 0.09767211]]
[[0.06505159 0.94888554 0.96563203]
[0.80839735 0.30461377 0.09767211]]]
(2, 2, 3)
Expanding / Reducing
x:
[[1 2 3]
[4 5 6]]
x.shape: (2, 3)
y:
[[[1 2 3]]
[[4 5 6]]]
y.shape: (2, 1, 3)
x:
[[[1 2 3]]
[[4 5 6]]]
x.shape: (2, 1, 3)
y:
[[1 2 3]
[4 5 6]]
y.shape: (2, 3)