我正在测试用于功能选择的嵌入式方法。
我了解(也许我误会了)使用嵌入式方法,我们可以在训练模型时获得最佳功能(基于功能的重要性)。
是的,我想获得训练后的模型的得分(该模型经过训练以选择特征)。
我正在用套索方法测试分类问题。
当我尝试获得分数时,出现错误,需要再次拟合模型。
为什么要使用嵌入式方法,为什么要分两个阶段进行呢?为什么我们不能同时选择最佳功能来训练模型?
from sklearn.linear_model import Lasso, LogisticRegression
from sklearn.feature_selection import SelectFromModel
estimator = LogisticRegression(C=1, penalty='l1', solver='liblinear')
selection = SelectFromModel(estimator)
selection.fit(x_train, y_train)
print(estimator.score(x_test, y_test))
错误:
sklearn.exceptions.NotFittedError: This LogisticRegression instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.
拟合的估计量返回为selection.estimator_
(请参阅docs);因此,拟合后selection
,您只需执行以下操作:
selection.estimator_.score(x_test, y_test)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句