HAO*_*HEN 5 python classification feature-selection scikit-learn
我想通过使用sklearn对文本进行分类。首先,我使用词袋训练数据,词袋的功能确实很大,超过10000个功能,因此我通过使用SVD将该功能减少到100。
但是在这里我想添加一些其他功能,例如单词数量,肯定单词数量,代词数量等。额外的功能仅少了10个功能,而与100个单词袋功能相比确实很小
在这种情况下,我提出两个问题:
虽然很感兴趣,但我不知道主要问题的答案。与此同时,我可以帮助解决第二个问题。
拟合模型后,您可以通过属性访问特征重要性model.feature_importances_
我使用以下函数来标准化重要性并以更漂亮的方式显示它。
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns # (optional)
def showFeatureImportance(model):
#FEATURE IMPORTANCE
# Get Feature Importance from the classifier
feature_importance = model.feature_importances_
# Normalize The Features
feature_importance = 100.0 * (feature_importance / Feature_importance.max())
sorted_idx = np.argsort(feature_importance)
pos = np.arange(sorted_idx.shape[0]) + .5
#plot relative feature importance
plt.figure(figsize=(12, 12))
plt.barh(pos, feature_importance[sorted_idx], align='center', color='#7A68A6')
plt.yticks(pos, np.asanyarray(X_cols)[sorted_idx])
plt.xlabel('Relative Importance')
plt.title('Feature Importance')
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1950 次 |
| 最近记录: |