Tirth Bharatiya
Tirth Bharatiya

Reputation: 41

predict() missing 1 required positional argument: 'X' in sklearn LinearRegression

I am trying to predict the salary using simple linear regression. Where X is year of experience and y is salary.

This is my code


# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Salary_Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values


# Splitting the dataset into the Training set and Test set
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 = 0)

# Fitting Simple Linear Regression to the Training Set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression
regressor(X_train, y_train)

# Predicting the Test set results
Y_pred = regressor.predict(X_test)

This is my error

Y_pred = regressor.predict(X_test)
Traceback (most recent call last):

  File "<ipython-input-28-e33267d5ef4e>", line 1, in <module>
    Y_pred = regressor.predict(X_test)

TypeError: predict() missing 1 required positional argument: 'X'

What am I doing wrong? How can I resolve this issue /error ?

Upvotes: 1

Views: 19469

Answers (2)

CAFEBABE
CAFEBABE

Reputation: 4101

It should be as follows:

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

# Predicting the Test set results
Y_pred = regressor.predict(X_test)

Upvotes: 0

warped
warped

Reputation: 9481

# Fitting Simple Linear Regression to the Training Set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()  # <-- you need to instantiate the regressor like so 
regressor.fit(X_train, y_train) # <-- you need to call the fit method of the regressor

# Predicting the Test set results
Y_pred = regressor.predict(X_test)

Upvotes: 2

Related Questions