Numpy 101
- Numerical Python
- array
Pandasβ
- Dataframe
- analagous to Excel/SQL
- Datarames are built on top of numpy
- array only contains our type and are fixed state
Arrayβ
{[1,2,3],[4,5,6]}
- ndim: 2 - dimension
- shape:(2,3) - 2 arrays with 3 each
- size: 6
Commands
import numpy as np
np.array({[0,1,2,3]})
np.array(['a','b'])
Creationsβ
np.ones
np.zeroes
np.arrange(range)
np.linspace(computation)
np.reshape
np.default_rng(seed)
slicing/indexing
myarray[0]
myarray[-1]
- last avluemyarray[:5]
- slicing
Slicingβ
{
[1,2,3],
[4,5,6],
[7,8,9]
}
- to get [3,6,9] -
myarray[:,2]
- to get [1,2,3] -
myarray[0,:]
Protips
Operationsβ
- multiplying and named arrays:
{[1,2],[2,9]}
product = array[0]
quantity = array[1]
product * quantity
Filteringβ
sales_array= [1,0,1,0]
mask = sales_array != 0
sales_array[mask]
whereβ
np.where(my_array!=0, "with stock", "no stock")
Vectorization
- Optimized C Code
- Python list versus numpy array
- 86 times faster
- always use numpy methods
Broadcasting
- auto computing trying to fit smaller arrays to big array
- not compatible shapes will throw an error, pnly one dimension needs to watch
{
[1,2,3],
[4,5,6],
[7,8,9]
}
+
{
[1]
}
- if you add the two objects below via numpy it will extend the array [1], to be 3x3 of [1]