Search code examples
python-3.xmachine-learninglogistic-regression

LogisticRegression classifier


I need to use Logistic Regression classifier I have dataset the length of each column 2000 this is all my code:

from statistics import mode
import pandas as pd
from sklearn.model_selection import KFold
from sklearn.metrics import plot_confusion_matrix
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
from sklearn.linear_model import LogisticRegression

# Importing the datasets
###Social_Network_Ads
datasets = pd.read_csv('C:/Users/n3.csv',header=None)
X = datasets.iloc[:, 0:5].values
Y = datasets.iloc[:, 5].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.25, random_state = 0)
# instantiate the model (using the default parameters)
model = LogisticRegression()
# fit the model with data
model.fit(X_Train, Y_Train)
predicted = cross_val_predict(mode, X_Train, Y_Train, cv=5) 
train_acc = model.score(X_Train, Y_Train)
print("The Accuracy for Training Set is {}".format(train_acc*100))

But in I got on this error:

TypeError: Cannot clone object '<function mode at 0x000000FD6579B9D0>' (type <class 'function'>): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' method.

How solve this?


Solution

  • Change this line

    predicted = cross_val_predict(mode, X_Train, Y_Train, cv=5) 
    

    to

    predicted = cross_val_predict(model, X_Train, Y_Train, cv=5) 
    

    You have a simple typo. You want to pass your estimator to the function but instead you passed mode which is imported from statistics. That's why the error tells you that it can not clone an object of type function. You are passing a function but it expects an estimator.