【问题标题】:When to use which Feature Importance Method?何时使用哪种特征重要性方法?
【发布时间】:2020-08-24 16:24:39
【问题描述】:

我正在努力寻找重要的特征。我使用不同的模型,但每个模型都给我不同的结果,我无法理解为什么。我查看了哪些假设适用于每个模型,但我在这里也找不到任何东西。

我正在使用 XGBoost、逻辑回归、RFE、排列重要性和决策树。我如何测试哪一个是最好的?我可以使用任何质量指标吗?此外,对于某些模型,输出不会映射到实际特征名称,而是映射到特征 0、特征 1 等数字。如何将它们映射到我的实际特征?

#XG BOOST 

# split data into train and test sets
test_size = 0.3
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=test_size)

#instantiate model and train
model = XGBClassifier(learning_rate = 0.05, n_estimators=200, max_depth=4)
model.fit(X_train, y_train)

# make predictions for test set
y_pred = model.predict(X_test)
predictions = [round(value) for value in y_pred]

accuracy = accuracy_score(y_test, predictions)
print("Accuracy: %.2f%%" % (accuracy * 100.0))

# plot feature importance
fig, ax = plt.subplots(figsize=(10,8))
plot_importance(model,ax=ax)


#PERMUTATION IMPORTANCE
train_X, val_X, train_y, val_y = train_test_split(x, y, random_state=1)
my_model = RandomForestClassifier(n_estimators=100,
                                  random_state=0).fit(train_X, train_y)


perm = PermutationImportance(my_model, random_state=1).fit(val_X, val_y)
eli5.show_weights(perm, feature_names = val_X.columns.tolist())

# RECURSIVE FEATURE ELIMINATION
X = x
Y = y
# feature extraction
model = LogisticRegression(solver='lbfgs')
rfe = RFE(model, 3)
fit = rfe.fit(X, Y)
print("Num Features: %d" % fit.n_features_)
print("Selected Features: %s" % fit.support_)
print("Feature Ranking: %s" % fit.ranking_)

# LOG REGRESSION
model = LogisticRegression()
# fit the model
model.fit(x, y)
# get importance
importance = model.coef_[0]
# summarize feature importance
for i,v in enumerate(importance):
    print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()

# DECISION TREE
model = DecisionTreeClassifier()
# fit the model
model.fit(x, y)
# get importance
importance = model.feature_importances_
# summarize feature importance
for i,v in enumerate(importance):
    print('Feature: %0d, Score: %.5f' % (i,v))
# plot feature importance
pyplot.bar([x for x in range(len(importance))], importance)
pyplot.show()

【问题讨论】:

    标签: python python-3.x machine-learning feature-extraction feature-selection


    【解决方案1】:

    首先 - 调整模型的超参数很重要,默认值在许多情况下都可以使用。

    第二:特征重要性显示特征对模型的重要性,它不显示模型的质量。

    此外,对于某些模型,输出不会映射到实际的特征名称,而是映射到特征 0、特征 1 等数字。

    数字指的是数据中的特征列表。因此,例如,如果您的数据有 ['age', 'area', 'weather'] 列,那么 0 - 年龄,1 - 区域,2 - 天气。

    至于比较模型 - 你应该使用指标:https://scikit-learn.org/stable/modules/model_evaluation.html

    对于分类,最常用的指标是 f1-score 和准确度。

    【讨论】:

      猜你喜欢
      • 2016-10-27
      • 1970-01-01
      • 2016-09-10
      • 2020-09-11
      • 2019-04-24
      • 2017-09-24
      • 1970-01-01
      • 2017-11-14
      相关资源
      最近更新 更多