我的数据集有超过200个变量,我正在对其运行一个分类模型,这导致了模型过拟合。有哪些建议可以减少特征的数量?我开始使用特征重要性(Feature Importance),但是由于变量数量如此之大,我无法可视化它。有没有办法可以针对给定的变量绘制或展示这些值?
以下是我尝试的代码:
F_Select = ExtraTreesClassifier(n_estimators=50)F_Select.fit(X_train,y_train)print(F_Select.feature_importances_)
回答:
你可以尝试按从大到小的顺序绘制特征重要性,看看哪些特征能够捕捉到一定比例(比如95%)的方差,类似于PCA中使用的碎石图。理想情况下,这应该是一个较小的特征数量:
import matplotlib.pyplot as pltfrom sklearn import *model = ensemble.RandomForestClassifier()model.fit(features, labels)model.feature_importances_importances = np.sort(model.feature_importances_)[::-1]cumsum = np.cumsum(importances)plt.bar(range(len(importances)), importances)plt.plot(cumsum)