Machine Learning – Tutorial 9

Regression – How to program the Best Fit Line

Next part of the equation works out the y intercept…

So Y intercept (b) equals the mean of the Ys minus the slope (m) times the mean of the Xs…easy.

# Import Libs
from statistics import mean
import numpy as np
import matplotlib.pyplot as plt
# To set charts to save as images we need to change the default behaviour
from matplotlib import style # inport style to change default behaviour of plot
style.use('ggplot') # use ggplot

# Define values
xs = np.array([1,2,3,4,5,6], dtype=np.float64) # dtype lets you set the data type. Not needed for this example but useful in future
ys = np.array([5,4,6,5,6,7], dtype=np.float64)

def best_fit_slope_and_intercept(xs, ys): # defining function to calculate slope (m) - passing values of xs and ys
    m = ( ((mean(xs)*mean(ys)) - mean(xs * ys)) / # bracket space at the start and space slash at the end allows for a carridge return in the code
        ((mean(xs)**2)-mean(xs**2)))  ## **2 raises to the power of 2
    b = mean(ys) - (m * mean(xs))
    return m, b # add in b to be returned as well as m

m, b = best_fit_slope_and_intercept(xs,ys) # define both usinf the function
print(m, b)

#calculate the line
regression_line = [(m*x)+b for x in xs] # one line for loop to create the line for illustration

#plot the data
plt.scatter(xs, ys)
plt.plot(xs, regression_line)
plt.savefig('ML_Tutorial9.png', bbox_inches='tight') #Sets the output to save an image # exports the image

Leave a Reply