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

时间:2019-02-04 15:15:24

标签: python scikit-learn ensemble-learning

我正在研究二分类问题,我正在套袋分类器中使用逻辑回归。

几行代码如下:-

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

我很高兴知道此模型的特征重要性指标。如果装袋分类器的估计量是对数回归,该怎么办?

当决策树用作装袋分类器的估计量时,我能够获得功能重要性。此代码如下:-

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

1 个答案:

答案 0 :(得分:2)

您不能直接推断线性分类器的功能重要性。另一方面,您可以做的是查看其系数的大小。您可以通过以下方式做到这一点:

# 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

这将大致告诉您每个系数的重要性。换句话说,值>> 0表示该系数着重于捕获阳性类别的趋势,而值<< 0表示该系数着重于阳性类别的捕获。


以下是基于您在注释中提供的值的示例代码:

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)
相关问题