ML Lab 07
ML Lab 07
Lab 07
Semester 8th
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()
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
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.