简体   繁体   中英

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!

You are attempting to fit again your classifier with your test data:

y_pred = classifier.fit(X_test)

which is of course not possible without passing also the labels (hence the error for missing y ); I assume that what you actually want to do is to get predictions for your test data, in which case you shoud use predict , and not fit :

y_pred = classifier.predict(X_test)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM