【发布时间】:2020-11-24 00:07:59
【问题描述】:
我正在网格搜索以调整堆叠估计器的超参数(来自 sklearn.ensemble 库的 StackingClassifier 对象)。我将 scikit 库用于 ML 和 RandomizedSearchCV 函数。除此之外,要调整的堆栈的基本估计器是管道(来自 imblearn.pipeline 库的管道对象),其中每个管道的第一步是来自 mlxtend 库的 ColumnSelector 对象。网格搜索旨在查看一长串变量组合,因此网格的参数分布仅通过 ColumnSelector 对象的参数“cols”。我第一次运行这段代码时,一切都运行良好,然后我将项目搁置一旁,几天后回来发现它不再工作了。代码中的所有内容都与我留下的相同,但是当我在 RandomizedSearchCV 对象上运行 fit 方法时,出现以下错误:
AttributeError:“ColumnSelector”对象没有属性“n_features_in_”
我不明白穿的是什么。我已经尝试了很多东西,甚至卸载了 Anaconda、mlxtend、imblearn,并重新安装了最新版本,但它一直在喊同样的错误。我在谷歌上搜索过,但似乎没有这方面的信息。
你能帮我解决这个问题吗?
提前致谢。
附录: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_'
【问题讨论】:
-
请提供完整的回溯,以及 mlxtend、imblearn 和 scikit-learn 的版本。
-
@BenReiniger 感谢您的关注本,我已经编辑了问题并添加了版本和回溯
标签: scikit-learn pipeline gridsearchcv imblearn mlxtend