简体   繁体   English

套袋分类器在逻辑回归中的功能重要性

[英]Feature importance in logistic regression with bagging classifier

I am working on a binary classification problem which I am using the logistic regression within bagging classifer. 我正在研究一个二进制分类问题,我在装袋分类器中使用逻辑回归。

Few lines of code are as follows:- 几行代码如下:

    model = BaggingClassifier(LogisticRegression(), 
                  n_estimators=10, 
                  bootstrap = True, random_state = 1)
    model.fit(X,y,sample_weights)

I am intrested in knowing feature importance metric for this model. 我很高兴知道此模型的功能重要性指标。 How can this be done if estimator for bagging classifer is logistic regression? 如果装袋分类器的估计量是对数回归,该怎么办?

I am able to get the feature importance when decision tree is used as an estimator for bagging classifer. 当决策树用作分类器的估计器时,我能够获得功能重要性。 The code for this is as follows:- 此代码如下:-

    feature_importances = np.mean([tree.feature_importances_ for tree in  model.estimators_], axis=0)

You can't infer the feature importance of the linear classifiers directly. 您无法直接推断线性分类器的功能重要性。 On the other hand, what you can do is see the magnitude of its coefficient. 另一方面,您可以做的是查看其系数的大小。 You can do that by: 您可以通过以下方式做到这一点:

# Get an average of the model coefficients
model_coeff = np.mean([lr.coef_ for lr in model.estimators_], axis=0)
# Multiply the model coefficients by the standard deviation of the data
coeff_magnitude = np.std(X, 0) * model_coeff

This will tell you roughly how important each coefficient is. 这将大致告诉您每个系数的重要性。 In other words, a value >> 0 indicates tendency of that coefficient to focus on capturing the positive class and a value << 0 indicates that that coefficient is focusing on the positive class. 换句话说,值>> 0表示该系数专注于捕获阳性类别的趋势,而值<< 0表示该系数专注于阳性类别的趋势。


Here is a sample code based on the values you have provided in the comments: 这是基于您在注释中提供的值的示例代码:

X_train = np.random.rand(2000, 3)
X_train.shape
# (2000, 3)
model_coeff = [[2.233232, 1.22435, 1.433434]]
coeff_magnitude = np.std(X_train, 0) * model_coeff
coeff_magnitude.shape
# (1, 3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM