何时使用哪种特征重要性方法?

问题描述 投票:1回答:1

我正在尝试找到重要的特征。我使用的模型不同,但是每个模型都能给我带来不同的结果,我无法理解为什么。我查看了每种模型适用的假设,但在这里我也找不到任何假设。

我正在使用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个回答
1
投票

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

第二个:要素重要性显示了模型要素对模型的重要性,但未显示模型的质量。

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

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

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

对于分类,最常用的度量标准是f1分数和准确性。

© www.soinside.com 2019 - 2024. All rights reserved.