将 sklearn RFE 与来自另一个包的估算器一起使用 [英] Using sklearn RFE with an estimator from another package

查看:56
本文介绍了将 sklearn RFE 与来自另一个包的估算器一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to use sklearn Recursive Feature Elimination(RFE) with an estimator from another package?

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

Specifically, I want to use GLM from statsmodels package and wrap it in sklearn RFE?

如果有,请举例说明?

推荐答案

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

Yes, it is possible. You just need to create a class that inherit sklearn.base.BaseEstimator, make sure it has fit & predict methods, and make sure its fit method expose feature importance through either coef_ or feature_importances_ attribute. Here is a simplified example of a class:

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_)

这篇关于将 sklearn RFE 与来自另一个包的估算器一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆