Question

In: Computer Science

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.

Solutions

Expert Solution

Python Code:

Input Datase:------------------------------------------------------------

YearsExperience Salary
0 1.1 39343.0
1 1.3 46205.0
2 1.5 37731.0
3 2.0 43525.0
4 2.2 39891.0
5 2.9 56642.0
6 3.0 60150.0
7 3.2 54445.0
8 3.2 64445.0
9 3.7 57189.0
10 3.9 63218.0
11 4.0 55794.0
12 4.0 56957.0
13 4.1 57081.0
14 4.5 61111.0
15 4.9 67938.0
16 5.1 66029.0
17 5.3 83088.0
18 5.9 81363.0
19 6.0 93940.0
20 6.8 91738.0
21 7.1 98273.0
22 7.9 101302.0
23 8.2 113812.0
24 8.7 109431.0
25 9.0 105582.0
26 9.5 116969.0
27 9.6 112635.0
28 10.3 122391.0
29 10.5 121872.0

_---------------------------------------------------------------------------------

# Informaion of employess of company. 30 employees.
# We have employees and their salary. We need to understand
# the correlation between both the columns. We need to predict
# salaries based on the number of experience an employee has
# and will compare it with actual salary.

import pandas as pd
import matplotlib.pyplot as plt
dataset = pd.read_csv('Salary_Data.csv')
dataset.head()

YearsExperience Salary
0 1.1 39343.0
1 1.3 46205.0
2 1.5 37731.0
3 2.0 43525.0
4 2.2 39891.0

x = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values
# Splitting into Training & Testing

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size= 1/3, random_state = 0)

# Dataset Divided into 70 and 30 ratio.

# We dont need to apply Feature Scaling in Regression as
# the Library takes care of the FS itself.
# Below SLR library will take care of FS
# To fit the Simple Linear Regression
to the Training Set.
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(x_train, y_train)
# Regressor is the machine which has learnt from the training
# data. Now the
regressor is the Machine which will now give results.
# Predicting Test Set Results
y_pred =
regressor.predict(x_test)

#To retrieve the intercept:
print("Intercept of regression model :",regressor.intercept_)
#For retrieving the slope:

print("Slope of regression model :",regressor.coef_)

Intercept of regression model : 26816.19224403119 -------------------( intercept and slope ) Slope of regression model
 : [9345.94244312]

#---------------------------------------------------------------------------------------------------------------------

# graph between actual dataset value and predicted by regression model values

df = pd.DataFrame({'Actual Value': y_test.flatten(), 'Predicted Value': y_pred.flatten()})
df1 =
df.head(25)
df1
.plot(kind='bar',figsize=(12,7))
plt.grid(which='major', linestyle='-', linewidth='0.5', color='green')
plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black')

plt.show()

# Visualizing the Training Set Results
plt.scatter(x_train, y_train, color = 'red',label='Data Points')
plt.plot(x_train, regressor.predict(x_train), color = 'blue',label="Regression Line")
plt.title('Salary vs Experience (Training Set)')
plt.legend()
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()

# Visualizing the Test Results
plt.scatter(x_test, y_test, color = 'red',label='Data Points')
plt.plot(x_train, regressor.predict(x_train), color = 'blue',label="Regression Line")
plt.title('Salary vs Experience (Test Set)')
plt.legend()
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()

Screen shot:


Related Solutions

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.
How many data points (pieces of data were used to create the regression equation?
How many data points (pieces of data were used to create the regression equation)?Regression StatisticsMultiple RR SquareAdjusted R SquareStandard ErrorObservations8ANOVAdfSSMSFRegression1333311Residual6233Total7CoefficientsStandard Errort StatP-valueIntercept1031.2746663.9842840.007248Advertising (thousands of $)126.193306741.6108020.158349
Needs to be done in PYTHON A. Create a Dollar currency class with two integer attributes...
Needs to be done in PYTHON A. Create a Dollar currency class with two integer attributes and one string attribute, all of which are non-public. The int attributes will represent whole part (or currency note value) and fractional part (or currency coin value) such that 100 fractional parts equals 1 whole part. The string attribute will represent the currency name. B. Create a CIS22C Dollar derived/inherited class with one additional non-public double attribute to represent the conversion factor from/to US...
Programming Language: C++ Create a base class called Shape which has 2 attributes: X and Y...
Programming Language: C++ Create a base class called Shape which has 2 attributes: X and Y (positions on a Cartesian coordinate system). Since a shape is amorphous, set the class up so that an object of type Shape can not be instantiated. Create three derived classes of your choice whose base class is Shape. These derived classes should have accessors/mutators for their class specific attributes, as well as methods to compute the area and the perimeter of the shape. In...
Python Create a move function that is only defined in the base class called Objects. The...
Python Create a move function that is only defined in the base class called Objects. The move function will take two parameters x,y and will also return the updated x,y parameters.
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and...
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and methods for holding the information for a document and write a program to test the class. Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3. Requirements: Write a class in Python 3 named Document that has the following attributes and methods and is saved in the file Document.py. Attributes __title is a...
Description: Create a class in Python 3 named Animal that has specified attributes and methods. Purpose:...
Description: Create a class in Python 3 named Animal that has specified attributes and methods. Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3. Requirements: Write a class in Python 3 named Animal that has the following attributes and methods and is saved in the file Animal.py. Attributes __animal_type is a hidden attribute used to indicate the animal’s type. For example: gecko, walrus, tiger, etc. __name is a...
Design and develop a class named Person in Python that contains two data attributes that stores...
Design and develop a class named Person in Python that contains two data attributes that stores the first name and last name of a person and appropriate accessor and mutator methods. Implement a method named __repr__ that outputs the details of a person. Then Design and develop a class named Student that is derived from Person, the __init__ for which should receive first name and last name from the class Person and also assigns values to student id, course, and...
in java Create a class City with x and y as the class variables. The constructor...
in java Create a class City with x and y as the class variables. The constructor with argument will get x and y and will initialize the city. Add a member function getDistanceFrom() to the class that gets a city as the input and finds the distance between the two cities.
Create a Class to contain a customer order Create attributes of that class to store Company...
Create a Class to contain a customer order Create attributes of that class to store Company Name, Address and Sales Tax. Create a public property for each of these attributes. Create a class constructor without parameters that initializes the attributes to default values. Create a class constructor with parameters that initializes the attributes to the passed in parameter values. Create a behavior of that class to generate a welcome message that includes the company name. Create a Class to contain...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT