【问题标题】:Using sklearn RFE with an estimator from another package将 sklearn RFE 与另一个包中的估计器一起使用
【发布时间】:2019-01-11 17:36:30
【问题描述】:

是否可以将 sklearn 递归特征消除 (RFE) 与来自另一个包的估计器一起使用?

具体来说,我想使用 statsmodels 包中的 GLM 并将其包装在 sklearn RFE 中?

如果是,请您举一些例子吗?

【问题讨论】:

    标签: scikit-learn statsmodels rfe


    【解决方案1】:

    是的,这是可能的。您只需要创建一个继承sklearn.base.BaseEstimator 的类,确保它具有fitpredict 方法,并确保其fit 方法通过coef_feature_importances_ 属性公开特征重要性。这是一个类的简化示例:

    import numpy as np
    from sklearn.datasets import make_classification
    from sklearn.base import BaseEstimator
    from sklearn.linear_model import LogisticRegression
    from sklearn.feature_selection import RFE
    
    class MyEstimator(BaseEstimator):
      def __init__(self):
        self.model = LogisticRegression()
    
      def fit(self, X, y, **kwargs):
        self.model.fit(X, y)
        self.coef_ = self.model.coef_
    
      def predict(self, X):
        result = self.model.predict(X)    
        return np.array(result)
    
    if __name__ == '__main__':
      X, y = make_classification(n_features=10, n_redundant=0, n_informative=7, n_clusters_per_class=1)
      estimator = MyEstimator()
      selector = RFE(estimator, 5, step=1)
      selector = selector.fit(X, y)
      print(selector.support_)
      print(selector.ranking_)
    

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 2014-09-13
      • 2013-12-11
      • 2013-02-17
      • 2018-04-21
      相关资源
      最近更新 更多