01
Import¶
Basics¶
np.array([1, 2, 3, 4, 5])
np.arange(1, 100, 10) ## start, step, step
np.linspace(1, 100, 10) ## start, step, no of values
## idk
np.zeros(10)
np.ones(10)
## random
np.random.random(10)
np.random.randn(10)
Array Operations¶
Indexing¶
np.vectorize¶
Kinda like a for loop
Stats¶
Calculus¶
## analytic calculus (for symbolic, use sympy)
dydx = np.gradient(y, x )
y_int = np.cumsum(y) * (x[1]-x[0])
Multi-Dimensional¶
a = np.array([
[1, 2, 3],
[4, 5, 6]
])
a = np.random.randn(3, 3)
a.ravel() ## returns a 1d array
a[0] ## first row
a[:,0] ## first column
Mesh Grid¶
Linear Algebra¶
Matrix¶
Solve systems of equations¶
a = np.array([
[3, 2, 1],
[5, -5, 4],
[6, 0, 1]
])
b = np.array([
4,
3,
0
])
x = np.linalg.solve(a, b) ## ax = b
Eigenvalues¶
Find-Replace¶
if
prediction['Rating'] = np.where(
prediction['Rating'].to_numpy() > 100,
100,
prediction['Rating'].to_numpy()
)
if-else
if-elseif-else
conditions = [
prediction['Rating'].to_numpy() > 100,
prediction['Rating'].to_numpy() > 50,
prediction['Rating'].to_numpy() > 20
]
values = [
100,
50,
20
]
default = 0
prediction['Rating'] = np.select(
conditions,
values,
default = default
)
nested
conditions = [
(prediction['Rating'].to_numpy() > 100 & prediction['Rating'].to_numpy() % 2 == 0),
(prediction['Rating'].to_numpy() > 100 & prediction['Rating'].to_numpy() % 3 == 0),
(prediction['Rating'].to_numpy() > 100 & prediction['Rating'].to_numpy() % 4 == 0),
prediction['Rating'].to_numpy() > 50,
prediction['Rating'].to_numpy() > 20
]
values = [
102,
103,
104,
50,
20
]
default = 0
prediction['Rating'] = np.select(
conditions,
values,
default = default
)
Rounding¶
Round to Integer¶
Round to \(n\) places