To write a program to implement the simple linear regression model for predicting the marks scored.
- Hardware โ PCs
- Anaconda โ Python 3.7 Installation / Moodle-Code Runner
- Import the standard Libraries.
- Set variables for assigning dataset values.
- Import linear regression from sklearn.
- Assign the points for representing in the graph
- Predict the regression for marks by using the representation of the graph.
- Compare the graphs and hence we obtained the linear regression for the given datas.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df=pd.read_csv('student_scores.csv')
#displaying the content in datafile
df.head()
df.tail()
# Segregating data to variables
X = df.iloc[:,:-1].values
X
Y=df.iloc[:,1].values
Y
#splitting train and test data
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
regressor=LinearRegression()
regressor.fit(X_train,Y_train)
Y_pred=regressor.predict(X_test)
#displaying predicted values
Y_pred
#displaying actual values
Y_test
#graph plot for training data
plt.scatter(X_train,Y_train,color="pink")
plt.plot(X_train,regressor.predict(X_train),color="red")
plt.title("Hours vs Scores (Training Set)")
plt.xlabel("Hours")
plt.ylabel("Scores")
plt.show()
#graph plot for test data
plt.scatter(X_test,Y_test,color="orange")
plt.plot(X_train,regressor.predict(X_train),color="yellow")
plt.title("Hours vs Scores (Test Set)")
plt.xlabel("Hours")
plt.ylabel("Scores")
plt.show()
##Contents in the data file (head, tail):
Thus the program to implement the simple linear regression model for predicting the marks scored is written and verified using python programming.