In the previous section, we have created a variable name ‘regressor’, which learnt the mathematical relationship of our x variable and y variable.
We can now predict the result of testing set using the ‘regressor’
Predicting Result with Simple Linear Regression Model
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv(‘Data.csv’)
x = dataset.iloc[:,:-1].values
#Splitting training set and testing set
from sklearn.cross_validation import train_test_split
xtrain, xtest, ytrain, ytest =train_test_split(x,y,test_size=0.25)
#Training and Fitting model
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
#Predicting using the Model
y_prediction = regressor.predict(xtest)
- In the section where we split the data into training set and testing set, we have created the xtest and ytest variables.
- ytest is the TRUE result that we have observed with xtest as the independent variable.
- With ‘regressor’ from the previous section, we have drew the mathematical relationship of xtrain and ytrain. Now, we are going to see whether the mathematical relationship can help us predict the value of ytest using xtest.
- We are going to assign the predicted value as y_prediction.
- If the Simple Linear Regression Model is good, then the y_prediction values should be close to the y_test values.
- The table below shows the value of predicted value. The predicted value of Y is very close the to true value of Y which is excellent.
ytest (TRUE VALUE)
y_prediction (Pedicted Value)
Other Sections on Linear Regression :
Step 4.) Predicting the Testing Set Data