Question

In: Computer Science

Create a class defined for Regression. Class attributes are data points for x, y, the slope and the intercept for the regression line.

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.


Solutions

Expert Solution

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.


Related Solutions

In python- Create a class defined for Regression. Class attributes are data points for x, y,...
In 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.
show graphically and explain how the x-intercept, the y-intercept and the slope of the budget line...
show graphically and explain how the x-intercept, the y-intercept and the slope of the budget line changes for each of the following scenarios a. The price of X changes b. the price of y changes c. Money income changes
Graph the line with y-intercept ?5 and slope ?2.
Graph the line with y-intercept ?5 and slope ?2.
How are the slope and intercept of a simple linear regression line calculated? What do they...
How are the slope and intercept of a simple linear regression line calculated? What do they tell us about the relationship between the two variables? Give example of problem.
How are the slope and intercept of a simple linear regression line calculated? What do they...
How are the slope and intercept of a simple linear regression line calculated? What do they tell us about the relationship between the two variables? Also, give an example.
1. The equation of the line with an x-intercept of 33 and a y-intercept of 44...
1. The equation of the line with an x-intercept of 33 and a y-intercept of 44 can be written in the form y=mx+b where the number m is:     the number b is:     Enter each answer as a reduced fraction (like 5/3, not 10/6) or as an integer (like 4 or -2). 2. You have filled your car with a full tank of gas, and could travel 485 miles. For every 17 miles you drive, you use 1 gallon of gass...
Using the following data create a log-log plot. What is the slope and resulting y-intercept? Show...
Using the following data create a log-log plot. What is the slope and resulting y-intercept? Show calculations. Mass Current 0.02 0 0.15 0.1 0.161 0.2 0.199 0.3 0.298 0.4 0.522 0.5 0.84 0.6 1.298 0.7 1.801 0.8 2.372 0.9 2.571 1
1)draw a line with an undefined slope and negative x intercept . 2)draw a line with...
1)draw a line with an undefined slope and negative x intercept . 2)draw a line with a negative slope and positive y - intercept. 3)line l has positive slope and a positive x intercept .Line m has negative slope and a negative y-intercept .Can line l intersect line m in cuadrant III? justify your answer please help
There is a regression line Y = 10 + 11X. The SEE for the slope is...
There is a regression line Y = 10 + 11X. The SEE for the slope is 60. The sample size is 10. The standard deviation of Y is 100 and the standard deviation of X is 5. Do a hypothesis test. Calculate the t-statistic, state the critical value, and state what this means with regards to the relation between X and Y.
1. The slope coefficient for a regression of Y on X is
Consider the data in the table below.YX5810555491969105952798Answer the following questions to two decimal places.1. The slope coefficient for a regression of Y on X is2. The constant of a regression of Y on X is3. The residual for the first observation in the table is4. The correlation of the residuals and X is
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT