Search code examples
machine-learningscikit-learnlogistic-regressioncross-validationgridsearchcv

Sklearn Pipeline - trying to count the number of times an estimator is called


I'm trying to count the number of times LogisticRegression is called in this pipeline, so I extended the class and overrode .fit(). It was supposed to be simple but it generates this weird error:

TypeError: float() argument must be a string or a number, not 'MyLogistic'

where MyLogistic is the new class. You should be able to reproduce the whole thing if you copy and paste the code.

from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import (GridSearchCV, StratifiedKFold)
from sklearn.feature_selection import SelectFromModel
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_iris
import numpy as np

class MyLogistic(LogisticRegression):
    __call_counter = 0
    def fit(X, y, sample_weight=None):
        print("MyLogistic fit is called.")
        MyLogistic._MyLogistic__call_counter += 1
        # fit() returns self.
        return super().fit(X, y, sample_weight)

# If I use this "extension", everything works fine.
#class MyLogistic(LogisticRegression):
#    pass
    
initial_logistic = MyLogistic(solver="liblinear", random_state = np.random.RandomState(18))
final_logistic = LogisticRegression(solver="liblinear", random_state = np.random.RandomState(20))
# prefit = False by default
select_best = SelectFromModel(estimator = initial_logistic, threshold = -np.inf)

select_k_best_pipeline = Pipeline(steps=[
    ('first_scaler', StandardScaler(with_mean = False)),
    # initial_logistic will be called from select_best, prefit = false by default.
    ('select_k_best', select_best),
    ('final_logit', final_logistic)
])

select_best_grid = {'select_k_best__estimator__C' : [0.02, 0.03],
                    'select_k_best__max_features': [1, 2],
                    'final_logit__C' : [0.01, 0.5, 1.0]}

skf = StratifiedKFold(n_splits = 3, shuffle = True, random_state = 17)

logit_best_searcher = GridSearchCV(estimator = select_k_best_pipeline, param_grid = select_best_grid, cv = skf, 
                               scoring = "roc_auc", n_jobs = 6, verbose = 4)

X, y = load_iris(return_X_y=True)
logit_best_searcher.fit(X, y > 0)
print("Best hyperparams: ", logit_best_searcher.best_params_)

Solution

  • You just forgot to put self as the first parameter of the fit signature. So the call is getting X=self, and when trying to check the input X it at some point tries to convert to float, hence the error message.

    There's still some weirdness around the parallelization; I get the counter equal to 1. Setting n_jobs=1 instead, I get the correct 37 for the counter (2x2x3 hyperparameter candidates on x3 folds, +1 for the final refit).