In: Computer Science
Python:
Create a class defined for Regression. Class attributes are data points for x, y, the slope and the intercept for the regression line. Define an instance method to find the regression line parameters (slope and intercept). Plot all data points on the graph. Plot the regression line on the same plot.
import numpy as np
import matplotlib.pyplot as plt
class Regression:
#constructor to initialize the values
def __init__(self, x, y):
self.x = x
self.y = y
def calculateParameters(self):
#calculate the means
x_mean = np.mean(x)
y_mean = np.mean(y)
#calculate the size of
dataset
n = np.size(x)
#calculate the slope of
dataset
self.slope = (np.sum(y*x) -
(n*y_mean*x_mean))/(np.sum(x*x) - (n*x_mean*x_mean))
#calculate the intercept
self.intercept = y_mean -
(self.slope * x_mean)
def plot_data(self):
#plotting the data and regression
line
#plot the data points
plt.scatter(x, y)
#calculate the points on line
line = [(self.slope * i) +
self.intercept for i in x]
#plot the line
plt.plot(x, line)
#give the lables
plt.xlabel("x values")
plt.ylabel("y values")
plt.title("Regression line")
plt.show()
#consider two numpy arrays
#this will be considered as dataset
#numpy array for x
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
#numpy array for x
y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])
#create object for regression
reg = Regression(x, y)
#calculate the slope and intercept
reg.calculateParameters()
#print the values of parameters
print(reg.slope)
print(reg.intercept)
#plot the data
reg.plot_data()
The dataset can be loaded from any csv file or can create simple data set to test it.