MLearning
/
Calculus
- 1 Supervised 7
-
Classifier S
-
Linear model S
-
Basis expansion S
-
Regularization S
-
Decision tree S
-
Random forest S
-
Logistic regression S
- 2 Datasets 5
-
Iris species S
-
Diabetes S
-
Breast cancer S
-
Simulated data S
-
Tmdb S
- 3 Numpy 4
-
Matrices S
-
Operations S
-
Standard deviation S
-
Multiplication S
- 4 Pandas 5
-
Read data S
-
Data cleaning S
-
Find values S
-
Group rows S
-
Merge S
- 5 Matplotlib 2
-
Subplots S
-
Pyplot S
- 6 Algorithms 4
-
K nearest neighbors S
-
Linear regression S
-
Gradient descent S
-
Decision tree S
- 7 Calculus 2
-
Derivatives S
-
Integrals S
S
R
Q
ML Calculus Derivatives
Instant speed, limit of average speeds Derivative, change rate of y with respect to x f(x) = ax^2 f'(x) = 2axLinear-regressionSlope
Increments method
p21 Instant speed at time = 4 is the limit as dt approaches 0\(
s_4(t) = 16*t^2
\enspace then \enspace
\lim_{\Delta t \to 0} \frac{\Delta s} {\Delta t} = 128
\)
\(
s_4'(t) = 128
\)

""" Devivatives / Increments method
An average speed of 30 mi/hr does not necessary means an
exact speed for 3 hours.
In the case of a ball droped near the surface of the earth,
the formula for distance traveled is:
s = 16t^2 ft/sec
The instant speed is not the quontient of distance and time,
it is the limit of average speeds at exactly t=4.
Using increments method, the numbers seem getting closer to:
s4 = 128 ft/sec
"""
# Average speed aproximation
s1 = 16*5**2 - 16*4**2 # from 4 to 5 seconds
s2 = (16*4.1**2 - 16*4**2) / 0.1 # from 4 to 4.1 seconds
s3 = (16*4.1**2 - 16*4**2) / 0.01 # from 4 to 4.01 seconds
# -------------------------------------------------------------------
def speed(t1, rate=1):
t2 = t1 + rate
s = 16 * (t2**2 - t1**2) / rate # The method of increments
s = round(s, 1)
return (t1, t2, s)
# -------------------------------------------------------------------
print("Manual aproximation:")
print("4 to 5s: speed =", s1)
print("4 to 4.1s: speed =", s2)
print("4 to 4.01s: speed = ", s3, "\n")
print("Increment method speed() function:")
print('%s to %ss: %s ft/sec' % speed(4, 1))
print('%s to %ss: %s ft/sec' % speed(4, 0.1))
print('%s to %ss: %s ft/sec' % speed(4, 0.01))
print('%s to %ss: %s ft/sec' % speed(4, 0.001))
print('%s to %ss: %s ft/sec' % speed(4, 0.0001))
print('%s to %ss: %s ft/sec' % speed(4, 0.00001))
"""
Manual aproximation:
4 to 5s: speed = 144
4 to 4.1s: speed = 129.5999999999998
4 to 4.01s: speed = 1295.999999999998
Increment method speed() function:
4 to 5s: 144.0 ft/sec
4 to 4.1s: 129.6 ft/sec
4 to 4.01s: 128.2 ft/sec
4 to 4.001s: 128.0 ft/sec
4 to 4.0001s: 128.0 ft/sec
4 to 4.00001s: 128.0 ft/sec
"""
➥ Differentiation
Differentiation
p29 The process of using increment method to get the derivative is called differentiation.\(
f(x) = ax^2
\enspace then \enspace
\lim_{\Delta x \to 0} \frac{\Delta y} {\Delta x} = 2ax
\)
\(
f'(x) = 2ax
\)

""" Derivatives / Differentiation
The process of using increment method to get the derivative
is called differentiation.
Derived function for f(x) = ax^2 is f'(x) and is pronounced "f prime of x"
It means instantaneous rate of change of y with respect to x at value x1
Increment method:
y + Dy = a(x + Dx)^2
y + Dy = ax^2 + 2axDx + a(Dx^2)
Dy = 2axDx + a(Dx^2)
Dy/Dx = 2ax + aDx
As Dx approaches the limit Dx -> 0, the derived is:
f'(x) = 2ax
"""
from sympy import *
t = Symbol('t')
s = 16*t**2 # Speed of a falling object
s_derivative = s.diff(t)
r = Symbol('r')
A = pi*r**2 # Circle area
A_derivative = A.diff(r)
x = Symbol('x')
f = x**2 # Function f(x)
f_derivative = f.diff(x)
assert 32*t == s_derivative
assert 2*pi*r == A_derivative
assert 2*x == f_derivative
print(f"Falling speed: s = {s} s' = {s_derivative}")
print(f"Circle area: A = {A} A' = {A_derivative}")
print(f"Function: f ={f} f' = {f_derivative}")
"""
Falling speed: s = 16*t**2 s' = 32*t
Circle area: A = pi*r**2 A' = 2*pi*r
Function: f =x**2 f' = 2*x
"""
Polynomials
p34 Functions that are a sum or difference of two or more monomials.\(
f(x) = ax^2 + bx + c
\enspace then \enspace
\lim_{\Delta x \to 0} \frac{\Delta y} {\Delta x} = 2ax + b
\)
\(
f'(x) = 2ax + b
\)

""" Devivatives / Polynomials Differentiation
Differentiation of simple polynomials takes into account
each term:
y = ax^2 + bx + c
y' = 2ax + b
y'' = 2a
"""
from sympy import *
x = Symbol('x')
a = Symbol('a')
b = Symbol('b')
c = Symbol('c')
y = a*x**2 + b*x + c
y_derivative = y.diff(x)
y_derivative_2 = y_derivative.diff(x)
assert 2*a*x + b == y_derivative
assert 2*a == y_derivative_2
print("Function:", y)
print("First derivative:", y_derivative)
print("Second derivative:", y_derivative_2)
"""
Function: a*x**2 + b*x + c
First derivative: 2*a*x + b
Second derivative: 2*a
"""
➥ The Slope
The Slope
The slope or gradient of a function in (x,y) point is the derivative.
""" Derivatives / Slope
Plot instant speeds (sloe or gradients) for different params.
Falling object speed:
s(t) = 16t^2
"""
import numpy as np
import matplotlib.pyplot as plt
a = 16
X = np.arange(-5, 5, 0.1)
Y = a*(X**2)
for t in range(2, 6):
y = a*(t**2)
m = 2*a*t
b = y - m*t
T = np.arange(t, t+2, 0.1) # 20 values sequence
plt.plot(t, y, 'rx') # points
plt.plot(T, m*T + b, label=f"s({t}) = {m}") # gradients
print(f"Instant speed for t = {t} is {m}")
plt.title('s(t) = 16t^2')
plt.xlabel('t (seconds)')
plt.ylabel('s(t)')
plt.grid(True)
plt.plot(X, Y) # function line
plt.legend(loc='upper left')
plt.show()

➥ Questions