Search code examples
pythonmachine-learningscikit-learnlogistic-regression

scikit-learn - TypeError: fit() missing 1 required positional argument: 'y'


import numpy as np
import pandas as pd

dataset=pd.read_csv("/Users/rushirajparmar/Downloads/Social_network_Ads.csv",error_bad_lines = False)


X = dataset.iloc[:,[2,3]].values.  
Y = dataset.iloc[:,4].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.25,random_state = 0) 

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression()
classifier.fit(X_train,Y_train)

y_pred = classifier.fit(X_test)

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(Y_test, y_pred)

I just started practising LogisticRegression where I am getting this error.I can not understand what is wrong.I tried searching it on internet but it didn't help

y_pred = classifier.fit(X_test).values.ravel()

TypeError: fit() missing 1 required positional argument: 'y'

Below is the link for dataset:

https://github.com/Avik-Jain/100-Days-Of-ML-Code/blob/master/datasets/Social_Network_Ads.csv

Thanks in advance!


Solution

  • You have already fitted the training data in classifier.fit(X_train,Y_train). classifier being your model, now you want to predict the y values (y_pred) for the test X data. Hence what you need to do is

    y_pred = classifier.predict(X_test)
    

    But what you are doing is

    y_pred = classifier.fit(X_test)
    

    Hence you are getting the error fit() missing 1 required positional argument: 'y' because while fitting you also need the dependent variable y here.

    Just replace .fit by .predict in the above mentioned line.