To write a program to predict the marks scored by a student using the simple linear regression model.
- Hardware – PCs
- Anaconda – Python 3.7 Installation / Jupyter notebook
- Use the standard libraries in python.
- Set variables for assigning dataset values.
- Import LinearRegression from the sklearn.
- Assign the points for representing the graph.
- Predict the regression for marks by using the representation of graph
- Compare the graphs and hence we obtain the LinearRegression for the given datas.
/*
Program to implement the simple linear regression model for predicting the marks scored.
Developed by: Rishabendran R
RegisterNumber: 212219040121
*/
#implement simple linear regression model for predicting the marks scored
import numpy as np
import pandas as pd
from sklearn.metrics import mean_absolute_error,mean_squared_error
import matplotlib.pyplot as plt
dataset = pd.read_csv('/content/student_scores.csv')
dataset.head()
dataset.tail()
#assigning hours to X & scores to Y
X = dataset.iloc[:,:-1].values
X
Y = dataset.iloc[:,1].values
Y
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)
from sklearn.linear_model import LinearRegression
reg=LinearRegression()
reg.fit(X_train,Y_train)
Y_pred = reg.predict(X_test)
Y_pred
Y_test
plt.scatter(X_train,Y_train,color="green")
plt.plot(X_train,reg.predict(X_train),color="red")
plt.title('Training set(H vs S)')
plt.xlabel("Hours")
plt.ylabel("Scores")
plt.show()
plt.scatter(X_test,Y_test,color="blue")
plt.plot(X_test,reg.predict(X_test),color="silver")
plt.title('Test set(H vs S)')
plt.xlabel("Hours")
plt.ylabel("Scores")
plt.show()
mse=mean_squared_error(Y_test,Y_pred)
print('MSE = ',mse)
mae=mean_absolute_error(Y_test,Y_pred)
print('MAE = ',mae)
rmse=np.sqrt(mse)
print('RMSE = ',rmse)
Thus the program to implement the simple linear regression model for predicting the marks scored is written and verified using python programming.