0% found this document useful (0 votes)
28 views

ML Lab 07

The document is a lab report for implementing linear regression on a dataset containing salaries and years of experience. Two approaches are taken: using plain Python code to calculate the slope and intercept, and using scikit-learn to fit a linear regression model and evaluate its performance. Both methods are able to successfully fit the linear regression line and make predictions on the relationship between salary and experience.

Uploaded by

kashif anwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

ML Lab 07

The document is a lab report for implementing linear regression on a dataset containing salaries and years of experience. Two approaches are taken: using plain Python code to calculate the slope and intercept, and using scikit-learn to fit a linear regression model and evaluate its performance. Both methods are able to successfully fit the linear regression line and make predictions on the relationship between salary and experience.

Uploaded by

kashif anwar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Machine Learning Lab

Lab 07

Semester 8th

Name M Haroon Safdar

Roll No. 190986

Submission Date 04-05-2023

Class BEMTS-F-19-A
Linear Regression
Objective:
Implement the linear regression algorithm on dataset of your own choice.

Solution:
We chose a dataset which includes salaries and years of experience.

Code:
Using no libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
data=pd.read_csv("C:\\Users\\hp\\Desktop\\Salary_Data.csv")
X=data.iloc[:,0]
Y=data.iloc[:,1]
plt.scatter(X, Y)
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()

def linear_regression(X, Y):


n = len(X)
sum_x = np.sum(X)
sum_y = np.sum(Y)
sum_xy = np.sum(X * Y)
sum_x_squared = np.sum(X ** 2)
# Calculate slope and intercept
slope = (n * sum_xy - sum_x * sum_y) / (n * sum_x_squared - sum_x ** 2)
intercept = (sum_y - slope * sum_x) / n

return slope, intercept

# Fit the line


slope, intercept = linear_regression(X, Y)
y_pred=slope * X + intercept
# Plot the line
plt.scatter(X, Y)
plt.plot(X, y_pred, color='red')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()

With libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score

# Load the dataset


data=pd.read_csv("C:\\Users\\hp\\Desktop\\Salary_Data.csv")
X=data.iloc[:,0].values.reshape(-1,1)
y=data.iloc[:,1].values.reshape(-1,1)
# Create a LinearRegression object
model = LinearRegression()

# Train the model on the data


model.fit(X, y)

# Make predictions on data


y_pred = model.predict(X)

# Calculate mean squared error


mse = mean_squared_error(y, y_pred)
r2 = r2_score(y, y_pred)
print('MSE=',mse,'\nR2-score=',r2)
# Plot the results
plt.scatter(X,y, color='red')
plt.plot(X, y_pred, color='blue')
plt.xlabel('Experience')
plt.ylabel('Salary')
plt.show()

Conclusion:
In conclusion, we successfully implemented the linear regression algorithm on a dataset of
salaries and years of experience using both plain Python code and libraries. We were able to plot
the data, calculate the slope and intercept of the regression line, and fit and train a
LinearRegression object to make predictions and evaluate the model's performance using mean
squared error and r2-score. Linear regression is a powerful tool for predicting the relationship
between two variables, and with further exploration and analysis, this implementation could be
extended to other datasets and problems.

You might also like