Table of Contents

Linear Regression


In [1]:
%pdb


Automatic pdb calling has been turned ON

In [2]:
import numpy as np

In [3]:
def linear_regression(x: np.array, y: np.array):
    """
    THIS METHOD CALCULATES THE LINEAR REGRESION BASED ON THE DATA.
    
    """
    M = x.size
    
    μ = x.mean()
    σ2 = x.var()
    
    w1 = (
        (M*(x*y).sum() - x.sum()*y.sum())/
        (M*(x**2).sum() - x.sum()**2)
    )
    w0 = y.mean() - (w1/M) * x.sum()
    return w0, w1

In [4]:
x = np.array([0, 1, 2, 3, 4])
y = np.array([3, 6, 7, 8, 11])

w0, w1 = linear_regression(x, y)
print('w0:', w0, 'w1:', w1)


w0: 3.4 w1: 1.8