# Machine Learning Calculus


```python
from sympy import *
import numpy as np
import math

x = Symbol('x')
y = Symbol('y')
```


```python
limit(1/x**2,x,0)
```




    oo




```python
# Find the limit of Functions
from sympy import Limit, Symbol, S

Limit(1/x, x, S.Infinity)
```




    Limit(1/x, x, oo, dir='-')




```python
# To find the value
l = Limit(1/x, x, S.Infinity)
l.doit()
```




    0




```python
# Differentiation
diff(15*x**100-3*x**12+5*x-46)
```




    1500*x**99 - 36*x**11 + 5




```python
# Constant
diff(99)
```




    0




```python
# Multiplication by Constant
diff(3*x)
```




    3




```python
# Power Rule
diff(x**3)
```




    3*x**2




```python
# Sum Rule
diff(x**2+3*x)
```




    2*x + 3




```python
# Difference Rule
diff(x**2-3*x)
```




    2*x - 3




```python
# Product Rule
diff(x**2*x)
```




    3*x**2




```python
# Chain Rule
diff(ln(x**2))
```




    2/x




```python
# Example
diff(9*(x+x**2))
```




    18*x + 9




```python
# Matrix Calculus
a = diff(3*x**2*y,x)
b = diff(3*x**2*y,y)
c = diff(2*x+8*y**7,x)
d = diff(2*x+y**8,y)
```


```python
print(a)
print(b)
print(c)
print(d)
```

    6*x*y
    3*x**2
    2
    8*y**7



```python
import numpy as np
Matrix_Calculus = np.matrix([[a, c], [b,d]])
Matrix_Calculus 
```




    matrix([[6*x*y, 2],
            [3*x**2, 8*y**7]], dtype=object)




```python
# Chain Rule
diff(ln(sin(x**3)**2))
```




    6*x**2*cos(x**3)/sin(x**3)




```python
# Sigmoid
# d/dx S(x)=S(x)(1−S(x)) 
diff(1/(1+math.e**-x),x)
```




    1.0*2.71828182845905**(-x)/(1 + 2.71828182845905**(-x))**2




```python
# Integral
import sympy
sympy.init_printing()
sympy.integrate(2*x, (x, 1, 0))
```


```python
# Jacobian
from sympy import sin, cos, Matrix
from sympy.abc import rho, phi

X = Matrix([rho*cos(phi), rho*sin(phi), rho**2])
Y = Matrix([rho, phi])

X.jacobian(Y)
```




$$\left[\begin{matrix}\cos{\left (\phi \right )} & - \rho \sin{\left (\phi \right )}\\\sin{\left (\phi \right )} & \rho \cos{\left (\phi \right )}\\2 \rho & 0\end{matrix}\right]$$




```python
# Example Calculating the Jacobian
# Equation
eq = x**2*y + 3/4*x*y + 10
eq
```


```python
# Jacobian matrix 
x, y, z = symbols('x y z')
Matrix([sin(x) + y, cos(y) + x, z]).jacobian([x, y, z])
```




$$\left[\begin{matrix}\cos{\left (x \right )} & 1 & 0\\1 & - \sin{\left (y \right )} & 0\\0 & 0 & 1\end{matrix}\right]$$




```python
# (x,y)=(0,0)
x1 = -y
x2 = x - 2*y * (2-x**2)
J = sympy.Matrix([x1,x2])
J.jacobian([x,y])
```




$$\left[\begin{matrix}0 & -1\\4 x y + 1 & 2 x^{2} - 4\end{matrix}\right]$$




```python
J.jacobian([x,y]).subs([(x,0), (y,0)])
```




$$\left[\begin{matrix}0 & -1\\1 & -4\end{matrix}\right]$$




```python
# Derivatives of x
de_x = diff(x**2*y + 3/4*x*y + 10, x)
de_x
```


```python
# Derivatives of y
de_y = diff(x**2*y + 3/4*x*y + 10, y)
de_y
```


```python
# Example
F = sympy.Matrix([de_x,de_y])
F.jacobian([x,y])
```




$$\left[\begin{matrix}2 y & 2 x + 0.75\\2 x + 0.75 & 0\end{matrix}\right]$$




```python
F.jacobian([x,y]).subs([(x,0), (y,0)])
```




$$\left[\begin{matrix}0 & 0.75\\0.75 & 0\end{matrix}\right]$$




```python
# Hessian
from sympy import Function, hessian, pprint
from sympy.abc import x, y
f = Function('f')(x, y)
g1 = Function('g')(x, y)
g2 = x**2+y**2
pprint(hessian(f, (x,y), [g1, g2]))
```

    ⎡                   ∂               ∂            ⎤
    ⎢     0        0    ──(g(x, y))     ──(g(x, y))  ⎥
    ⎢                   ∂x              ∂y           ⎥
    ⎢                                                ⎥
    ⎢     0        0        2⋅x             2⋅y      ⎥
    ⎢                                                ⎥
    ⎢                     2               2          ⎥
    ⎢∂                   ∂               ∂           ⎥
    ⎢──(g(x, y))  2⋅x   ───(f(x, y))   ─────(f(x, y))⎥
    ⎢∂x                   2            ∂y ∂x         ⎥
    ⎢                   ∂x                           ⎥
    ⎢                                                ⎥
    ⎢                     2               2          ⎥
    ⎢∂                   ∂               ∂           ⎥
    ⎢──(g(x, y))  2⋅y  ─────(f(x, y))   ───(f(x, y)) ⎥
    ⎢∂y                ∂y ∂x              2          ⎥
    ⎣                                   ∂y           ⎦



```python
import numpy as np
import matplotlib.pyplot as plt

def tanh(x):
    return np.tanh(x)

X = np.linspace(-5, 5, 100)
plt.plot(X, tanh(X),'b')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Neural Networks - Activation Function')
plt.grid()
plt.text(4, 0.8, r'$\sigma(x)=\tanh{(x)}}$', fontsize=16)
plt.show()
```


    <Figure size 640x480 with 1 Axes>



```python
def sigma(x):
    return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))

X = np.linspace(-5, 5, 100)
plt.plot(X, sigma(X),'b')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Sigmoid Function')
plt.grid()
plt.text(4, 0.8, r'$\sigma(x)=\frac{e^{x}-e^{-x}}{e^{x}+e^{-x}}$', fontsize=16)
plt.show()
```


```python
def derivatives_sigma(x):
    return 1 / (np.cosh(x))**2

X = np.linspace(-5, 5, 100)
plt.plot(X, derivatives_sigma(X),'b')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Sigmoid Function')
plt.grid()
plt.text(4, 0.8, r'$\sigma(x)=\frac{1}{cosh^2(x)}$', fontsize=16)
plt.show()
```


```python
def derivatives_sigma(x):
    return 4 / (np.exp(x)+np.exp(-x))**2

X = np.linspace(-5, 5, 100)
plt.plot(X, derivatives_sigma(X),'b')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Sigmoid Function')
plt.grid()
plt.text(4, 0.8, r'$\sigma(x)=\frac{4}{(e^{x}+e^{-x})^2}$', fontsize=16)
plt.show()
```


```python
# Newton_Raphson
# f(x) - the function of the polynomial
from sympy import *

def f(x):
    function = x**3 - x - 1
    return function

def derivative(x): #function to find the derivative of the polynomial
    derivative = diff(f(x), x)
    return derivative

def Newton_Raphson(x):
	return (x - (f(x) / derivative(x)))

Newton_Raphson(x)
```


```python
# Advanced Chain Rule
# F(y)=ln(1−5y2+y3)
from sympy import *

y = symbols('y')
F = symbols('F', cls=Function)


Derivative(ln(1 - 5*y**2 + y**3)).doit()
```


```python
diff(ln(1 - 5*y**2 + y**3))
```

0a36b0285ea61d4d4ecec7ae70d004cc67e31755
ipynb
Jupyter Notebook
ML_Calculus.ipynb
damonclifford/Mathematics_for_Machine_Learning
ecdb6b28ce6361dda4e810df829f2d3ed7e7e193
MIT
2019-02-05T04:35:23.000Z
2022-03-21T19:05:15.000Z
ML_Calculus.ipynb
damonclifford/Mathematics_for_Machine_Learning
ecdb6b28ce6361dda4e810df829f2d3ed7e7e193
MIT
ML_Calculus.ipynb
damonclifford/Mathematics_for_Machine_Learning
ecdb6b28ce6361dda4e810df829f2d3ed7e7e193
MIT
2019-08-20T14:50:07.000Z
2022-03-19T19:24:02.000Z
Qwen/Qwen-72B
1. YES
2. YES