0
$\begingroup$

While visualising our dataset in linear regression model's training set and test set, why do we keep the same regression line in both the training set and the test set, just change the scattering point in test data like shown below in the code:

#visualising training set
plt.scatter(X_train,y_train,color='red')
plt.plot(X_train,regressor.predict(X_train),color='blue')
plt.show()

#visualising test set
plt.scatter(X_test,y_test,color='red')
plt.plot(X_train,regressor.predict(X_train),color='blue')
plt.show()
$\endgroup$
2
$\begingroup$

Well, simply - this is the whole purpose of splitting your data into training and testing subsets.

Training set is used to estimate the parameters of the model.

Testing set is used to check how accurate the model constructed using the training set is.

In the case of linear regression the model estimates the intercept and slope of the line. But there is really one model - the model estimated using the training set and, hence, one line. The person who wrote the code in your question is displaying this one model in two ways: by comparing it with the data points from the training set, and then by comparing it with the data points from the test set.

First plot is used to check how well the model fits the data it was trained on.

Second plot is used to check the accuracy of the model on the data it hasn't "seen" yet.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.