AttributeError:“ ColumnSelector”对象没有属性“ n_features_in_”是什么意思?

时间:2020-08-03 22:17:12

标签: scikit-learn pipeline gridsearchcv imblearn mlxtend

我正在进行网格搜索以调整堆栈估计器(来自sklearn.ensemble库的StackingClassifier对象)的超参数。我将scikit库用于ML和RandomizedSearchCV函数。除此之外,要调整的堆栈的基本估计量是管道(imblearn.pipeline库中的Pipeline对象),其中每个管道的第一步是mlxtend库中的ColumnSelector对象。网格搜索旨在查看一长串变量组合,因此网格的参数分布仅遍及ColumnSelector对象的“ cols”参数。第一次运行此代码时,一切运行良好,然后我搁置该项目,几天后返回,发现它不再工作。代码中的所有内容与我留下的内容相同,但是当我在RandomizedSearchCV对象上运行适合的方法时,出现以下错误:

AttributeError:“ ColumnSelector”对象没有属性“ n_features_in _”

我不明白那是什么。我已经尝试了很多方法,甚至卸载了Anaconda,mlxtend,imblearn,并使用最新版本进行了重新安装,但是它一直在喊同样的错误。我在Google上进行了搜索,但似乎没有有关此信息。

您能帮我解决这个问题吗?

谢谢。


附录:scikit版本为0.23.1,mlxtend版本为0.17.3,不平衡学习版本为0.7.0。

下面是完整的追溯,对象gr2对应于一个RandomizedSearchCV对象,该对象旨在调整堆栈分类器。我想指出的是,如果我使用mlxtend中的StackingClassifier对象,则一切正常,但此对象没有参数cv,该参数确实具有sklearn.ensemble的StackingClassifier,而我需要它才能具有更好的性能(在一切正常之前,我有过。)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-94-9d8f412d45a3> in <module>
----> 1 gr2.fit(x_train,y_train)

~\anaconda3\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
     71                           FutureWarning)
     72         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 73         return f(**kwargs)
     74     return inner_f
     75 

~\anaconda3\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups, **fit_params)
    763             refit_start_time = time.time()
    764             if y is not None:
--> 765                 self.best_estimator_.fit(X, y, **fit_params)
    766             else:
    767                 self.best_estimator_.fit(X, **fit_params)

~\anaconda3\lib\site-packages\sklearn\ensemble\_stacking.py in fit(self, X, y, sample_weight)
    423         self._le = LabelEncoder().fit(y)
    424         self.classes_ = self._le.classes_
--> 425         return super().fit(X, self._le.transform(y), sample_weight)
    426 
    427     @if_delegate_has_method(delegate='final_estimator_')

~\anaconda3\lib\site-packages\sklearn\ensemble\_stacking.py in fit(self, X, y, sample_weight)
    147             for est in all_estimators if est != 'drop'
    148         )
--> 149         self.n_features_in_ = self.estimators_[0].n_features_in_
    150 
    151         self.named_estimators_ = Bunch()

~\anaconda3\lib\site-packages\sklearn\pipeline.py in n_features_in_(self)
    623     def n_features_in_(self):
    624         # delegate to first step (which will call _check_is_fitted)
--> 625         return self.steps[0][1].n_features_in_
    626 
    627     def _sk_visual_block_(self):

AttributeError: 'ColumnSelector' object has no attribute 'n_features_in_'

1 个答案:

答案 0 :(得分:0)

sklearn一直在使用属性n_features_in_添加功能数量检查。似乎mlxtend尚未将其添加到其ColumnSelector中,因此出现了错误(请注意,sklearn的{​​{1}}没有自己的属性{{1} },而是委派给第一步,就像您在回溯末尾的代码注释中看到的那样。

理想情况下,使用Pipeline提交问题,以将n_features_in_(以及相关的检查)添加到mlxtend。但是与此同时,我想到了两种解决方法:

  1. n_features_in_具有一个ColumnSelector,无论如何它可能比普通的mlxtend更受欢迎,并且具有您想要的StackingClassifierCV参数。 可能永远不会寻找StackingClassifier属性并解决问题(只要cv从未尝试调用其getter ...)
  2. 使用n_features_in_的{​​{1}}可能比使用Pipeline的{​​{1}}更好。这样看来,您根本不需要sklearn
  3. 降级您的ColumnTransformer可能足以避免mlxtend的检查。