BayesSearchCV TypeError:“版本”和“元组”的实例之间不支持“ <”

问题描述

我正在研究kaggle Titanic数据集。我正在尝试使用LightGBM的LGBMClassifier来确定给定的乘客是否还幸免于难。我创建了一个用于填充和处理所有数据的管道,并尝试使用BayesSearchCV优化我的LightGBM超参数。使用BayesSearchCV时收到以下错误

” TypeError:“版本”和“元组”的实例之间不支持

我不知道为什么会出现此错误,因为我可以将创建的pipleine拟合到数据中,并且它可以与Sklearn的gridsearchcv配合使用,因此我不知道这是BayesSearchCV的问题还是仅我一个。我将运行错误的管道和代码放在下面,并在错误发生的位置加了一个标记

target = 'survived'

categorical_features = ['sex',#'ticket',#'cabin','embarked']

numeric_features = ['pclass','age','sibsp','parch','fare']

train,test = train_test_split(df,test_size=0.20)

numerical_pipe = Pipeline([('imputer',SimpleImputer(strategy = 'mean'))])

categorical_pipe = Pipeline([('imputer',SimpleImputer(strategy = 'constant',fill_value = 'missing')),('onehot',OneHotEncoder(handle_unkNown = 'ignore'))])

preprocessing = ColumnTransformer(transformers = [
                ('cat',categorical_pipe,categorical_features),('num',numerical_pipe,numeric_features)])

lgb_pipe = Pipeline([
          ('preprocess',preprocessing),('classifier',LGBMClassifier())])

search_space_lgb = {'num_leaves': Integer(1,500),'max_depth': Integer(1,500)}

bayes_search_lgb = BayesSearchCV(lgb_pipe,search_space_lgb)

bs_lgb = bayes_search_lgb.fit(train[numeric_features + categorical_features],train[target]) #ERROR HERE

print(bs_lgb.best_params_)

这是错误的一部分,我认为这对于确定确切的错误是有用的。

/Applications/anaconda3/lib/@R_502_3533@/site-packages/skopt/space/space.py in rvs(self,n_samples,random_state)

762 
763         for dim in self.dimensions:
--> 764             if sp_version < (0,16):
765                 columns.append(dim.rvs(n_samples=n_samples))
766             else:

发现了另一个与我相同的错误TypeError inside the `scikit-optimize` package),但是没有一个解决方案对我有用。

解决方法

BayesSearchCV来自skopt库。您不必导入整个库,只需键入以下命令即可:

从skopt导入BayesSearchCV

此外,您可能希望使用shift + tab仔细检查参数要求。大多数错误发生在参数级别。让我知道是否可以解决问题。

,

我解决了更改skopt / space / space.py第763-768行

 for dim in self.dimensions:
        
        if sp_version < (0,16):
            columns.append(dim.rvs(n_samples=n_samples))
        else:
            columns.append(dim.rvs(n_samples=n_samples,random_state=rng))

进入

for dim in self.dimensions:
        
        try:
            columns.append(dim.rvs(n_samples=n_samples,random_state=rng))
        except:
            columns.append(dim.rvs(n_samples=n_samples))