RandomizedSearchCV: All estimators failed to fit

  gridsearchcv, python, scikit-learn

I am currently working on the "French Motor Claims Datasets freMTPL2freq" Kaggle competition (https://www.kaggle.com/floser/french-motor-claims-datasets-fremtpl2freq). Unfortunately I get a "NotFittedError: All estimators failed to fit" error whenever I am using RandomizedSearchCV and I cannot figure out why that is.
Any help is much appreciated.

import numpy as np
import statsmodels.api as sm
import scipy.stats as stats
from matplotlib import pyplot as plt
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import KBinsDiscretizer
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import mean_poisson_deviance
from sklearn.metrics import mean_squared_error
from sklearn.ensemble import VotingRegressor
from sklearn.ensemble import StackingRegressor
from sklearn.metrics import mean_gamma_deviance
from sklearn.metrics import mean_squared_error
from xgboost import XGBRegressor

data_freq = pd.read_csv('freMTPL2freq.csv')
data_freq['Area'] = data_freq['Area'].str.replace(''','')
data_freq['VehBrand'] = data_freq['VehBrand'].str.replace(''','')
data_freq['VehGas'] = data_freq['VehGas'].str.replace(''','')
data_freq['Region'] = data_freq['Region'].str.replace(''','')

    data_freq['frequency'] = data_freq['ClaimNb'] / data_freq['Exposure']
    y = data_freq['frequency']
    X = data_freq.drop(['frequency', 'ClaimNb', 'IDpol'], axis = 1)
    X_train, X_val, y_train, y_val = train_test_split(X,y, test_size=0.2, shuffle = True, random_state = 42)
    pt_columns = ['VehPower', 'VehAge', 'DrivAge', 'BonusMalus', 'Density']
    cat_columns = ['Area', 'Region', 'VehBrand', 'VehGas']

    from xgboost import XGBRegressor
    ct = ColumnTransformer([('pt', 'passthrough', pt_columns),
                       ('ohe', OneHotEncoder(), cat_columns)])
    pipe_xgbr = Pipeline([('cf_trans', ct),
                      ('ssc', StandardScaler(with_mean = False)),
    ('xgb_regressor', XGBRegressor())

    param = {'xgb_regressor__n_estimators':[3, 5],
        'xgb_regressor__max_depth':[3, 5, 7],
        'xgb_regressor__learning_rate':[0.1, 0.5],
        'xgb_regressor__colsample_bytree':[0.5, 0.8],
        'xgb_regressor__subsample':[0.5, 0.8]

    rscv = RandomizedSearchCV(pipe_xgbr, param_distributions = param, n_iter = 2, scoring = mean_squared_error, n_jobs = -1, cv = 5)
    rscv.fit(X_train, y_train, sample_weight = X_train['Exposure'])```

Source: Python Questions

One Reply to “RandomizedSearchCV: All estimators failed to fit”