# Why do i need to write regressor.predict(x_train)?

Im currently learning data science and i was unable to understand a particular part in linear regression model. The following is my code -

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

x = dataset.iloc[:,:-1]
y = dataset.iloc[:,-1].values

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=1)

from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(x_train, y_train)

y_predict = regressor.predict(x_test)
print(y_predict)

plt.scatter(x_train, y_train, color = 'red')
plt.plot(x_train, regressor.predict(x_train) , color = 'blue' )
plt.title('Salary Vs Number Of Years Of Experience Of Training Set')
plt.xlabel('Years Of Experience')
plt.ylabel('Salary')

plt.scatter(x_test, y_test, color = 'red')
plt.plot(x_train, regressor.predict(x_train) , color = 'blue' )
plt.title('Salary Vs Number Of Years Of Experience Of Training Set')
plt.xlabel('Years Of Experience')
plt.ylabel('Salary')


The following is the csv document I am not able to understand the predict function here , why is it written predict(x_train) and not predict(y_train) as in plt.plot(x_train, regressor.predict(x_train) , color = 'blue' ) , x_train is already mentioned before regressor.predict , should not y_test be put . Can you explain how predict function is used? Thank you

• The predict method uses the trained model to predict the output for new inputs. Since it predicts values given new inputs, it takes the features as an input (x_train) and predicts the corresponding y values. Jun 6, 2022 at 11:11