import matplotlib.pyplot as plt
import numpy as np
def linear_function(m, x, b):
    """
    This function solve a basic linear regression problem
    """
    return m*x + b
resolution = 100 # The number of "x" variables in our set
m = 10 # Set the inclination of our slope
b = 5 # Set the point where we want to touch axis y
x = np.linspace(-10.0, 10.0, num=resolution) # A list from -10 to 10 with 100 "x" values
y = linear_function(m, x, b)
figure, axis = plt.subplots()
axis.plot(x, y) # Plot the "x", "y" (abscissa and ordinate)
axis.grid() # Print a faded grid all over the plot
# Set some "x", "y" axis details
axis.axhline(y=0, color='red')
axis.axvline(x=0, color='red')
def power_function(x):
    """
    This function solve a basic power function
    """
    return 2*x**8 - x**4 + 3*x**2 + 4
resolution = 100 # The number of "x" variables in our set
x = np.linspace(-10.0, 10.0, num=resolution) # A list from -10 to 10 with 100 "x" values
y = power_function(x)
figure, axis = plt.subplots()
axis.plot(x, y) # Plot the "x", "y" (abscissa and ordinate)
axis.grid() # Print a faded grid all over the plot
# Set some "x", "y" axis details
axis.axhline(y=0, color='red')
axis.axvline(x=0, color='red')
def trigonometrical_function(x):
    """
    This function solve a basic trigonometrical function
    """
    return np.cos(x)
resolution = 100 # The number of "x" variables in our set
x = np.linspace(-10.0, 10.0, num=resolution) # A list from -10 to 10 with 100 "x" values
y = trigonometrical_function(x)
figure, axis = plt.subplots()
axis.plot(x, y) # Plot the "x", "y" (abscissa and ordinate)
axis.grid() # Print a faded grid all over the plot
# Set some "x", "y" axis details
axis.axhline(y=0, color='red')
axis.axvline(x=0, color='red')
def exponential_function(x):
    """
    This function solve a basic exponential function
    """
    return pow(np.e, x)
resolution = 100 # The number of "x" variables in our set
x = np.linspace(-1.0, 1.0, num=resolution) # A list from -1 to 1 with 100 "x" values
y = exponential_function(x)
figure, axis = plt.subplots()
axis.plot(x, y) # Plot the "x", "y" (abscissa and ordinate)
axis.grid() # Print a faded grid all over the plot
# Set some "x", "y" axis details
axis.axhline(y=0, color='red')
axis.axvline(x=0, color='red')
f = lambda x: np.log2(x)
resolution = 100
x = np.linspace(0.01, 8.0, num=resolution)
y = f(x)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.grid()
ax.axhline(y=0, color='red')
ax.axvline(x=0, color='red')
def H(x):
  y = np.zeros(len(x))
  for id_x, x in enumerate(x):
    if x >= 0:
      y[id_x] = 1.0
  return y
resolution = 100
x = np.linspace(-10.0, 10.0, num=resolution)
y = H(x)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.grid()
N = 1000
def f(x):
    return pow(x, 2)
c = 2
x = np.linspace(-3, 3, num=N)
y = f(x) + c
fig, ax = plt.subplots()
ax.plot(x, y)
ax.grid()
ax.axhline(y=0, color='red')
ax.axvline(x=0, color='red')
N = 1000
def f(x):
    return np.sin(x)
c = 2
x = np.linspace(-15, 15, num=N)
y = f(1/c * x)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.grid()
ax.axhline(y=0, color='red')
ax.axvline(x=0, color='red')
def f(x):
  return x**3
N = 1000
x = np.linspace(-15, 15, num=N)
y = - f(x)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.grid()
ax.axhline(y=0, color='red')
ax.axvline(x=0, color='red')
import numpy as np
import matplotlib.pyplot as plt
def g(x):
  return x**2
def f(x):
  return np.sin(x)
x = np.linspace(-10, 10, num=1_000)
f_o_g = f(g(x))
g_o_f = g(f(x))
#plt.plot(x, g_o_f)
plt.plot(x, f_o_g)
plt.grid()
import numpy as np
import matplotlib.pyplot as plt
def estimate_b0_b1(x, y):
    mean_x, mean_y = np.mean(x), np.mean(y)
    sum_xy = np.sum((x - mean_x) * (y - mean_y))
    sum_xx = np.sum(pow(x - mean_x, 2))
    b1 = sum_xy / sum_xx
    b0 = mean_y - (b1 * mean_x)
    return (b0, b1)
# Mean Squared Error
def mean_squared_error(y, y_hat):
    n = len(y_hat)
    sigma = 0
    for i in range(n):
        sigma += pow(y_hat[i] - y[i], 2)
    
    return sigma / n
def plot_regression(x, y, b):
    plt.scatter(x, y, color='red', marker='o', s=30)
    y_hat = b[0] + b[1]*x
    mse = mean_squared_error(y, y_hat).round(5)
    print("y = {}x + {}".format(b[1].round(5), b[0].round(5)))
    print("MSE =", mse)
    plt.plot(x, y_hat, color='blue')
    plt.xlabel('Income')
    plt.ylabel('Outcome')
    plt.grid()
    plt.show()
def main():    
    df =np.array([
        [0, 100],
        [20, 150],
        [30, 200],
        [40, 180],
        [50, 250],
        [60, 230]])
    x = df[:, 0]
    y = df[:, 1]
    b = estimate_b0_b1(x, y)
    plot = plot_regression(x, y, b)
    
if __name__ == "__main__":
    main()
from math import *
import sympy as sp
def derivative(expr):
    x = sp.Symbol('x')
    function = sp.Derivative(expr, evaluate=True)
    print(f'The derivative of f(x) is:     {function}')
def run():
    expr = "(5*x**2)+(3*x)"
    derivative(expr)
if __name__ == '__main__':
    run()
from matplotlib import cm # To manage colors
import numpy as np
import matplotlib.pyplot as plt
def z(x, y):
    return np.sin(x) + (2 * np.cos(y))
res = 100
x = np.linspace(-4, 4, num=res)
y = np.linspace(-4, 4, num=res)
x, y = np.meshgrid(x, y)
z = z(x, y)
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
surf = ax.plot_surface(x, y, z, cmap=cm.cool)
fig.colorbar(surf)
fig2, ax2 = plt.subplots()
level_map = np.linspace(np.min(z), np.max(z), num=500)
cp = ax2.contour(x, y, z, levels=level_map, cmap=cm.cool)
fig2.colorbar(cp)
from math import *
import sympy as sp
def derivative(expr, variables):
    for i in expr:
        print("\n", i)
        for der_respect in variables:
            var = sp.Symbol(f'{der_respect}')
            function = sp.Derivative(i, var, evaluate=True)
            print(f'The partial derivative df/d{der_respect} =  {function}')
def run():
    variables = ('x', 'y', 'z')
    # Enter your function
    expr = (("4*x^3 + 6*x^2*y^6 + 5*y + 3"), ("y*sin(x*y)"))
    derivative(expr, variables)
if __name__ == '__main__':
    run()
from matplotlib import cm # Colors managing
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
def f(x, y):
    return x**2 + y**2
res = 100
x = np.linspace(-4, 4, num=res)
y = np.linspace(-4, 4, num=res)
x, y = np.meshgrid(x, y)
z = f(x, y)
surf = ax.plot_surface(x, y, z, cmap=cm.jet)
fig.colorbar(surf)
level_map = np.linspace(np.min(z), np.max(z), res)
plt.contourf(x, y, z, levels=level_map, cmap=cm.jet)
plt.colorbar()
plt.title("Gradient descent")
p = np.random.rand(2) * 8 - 4 # Generates two random values
plt.plot(p[0], p[1], 'o', c='k')
h = 0.01
lr = 0.01
def derivate(cp, p):
    return (f(cp[0], cp[1]) - f(p[0],p[1])) / h
def gradient(p):
    grad = np.zeros(2)
    for idx, val in enumerate(p):
        cp = np.copy(p)
        cp[idx] = cp[idx] + h
        dp = derivate(cp, p)
        grad[idx] = dp
    return grad
for i in range(1000):
    p = p - lr * gradient(p)
    if (i % 10 == 0):
        plt.plot(p[0], p[1], 'o', c='red')
plt.plot(p[0], p[1], 'o', c='white')
print(p)