欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

特征输出重要性的排序

程序员文章站 2022-07-14 13:41:26
...

几个参考:
模型输出特征重要性排序
在 Python 中使用 XGBoost 的特征重要性和特征选择
关于seaborn作图
barplot&countplot&pointplot
Python Seaborn综合指南

1.用matplotlib

import pandas as pd
from xgboost import XGBClassifier
from xgboost import plot_importance
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from matplotlib import pyplot
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedKFold
dataset = pd.read_csv("C:\\Users\\Nihil\\Documents\\pythonlearn\\data\\pima-indians-diabetes.data.csv")

X = dataset.iloc[:,0:8]
y = dataset.iloc[:,8]

X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.33,random_state=7)
model = XGBClassifier()
model.fit(X_train,y_train)
print(model.feature_importances_)
data = pd.DataFrame(model.feature_importances_)
data.columns = ['featureimportances']

pyplot.bar(range(len(model.feature_importances_)), model.feature_importances_)
pyplot.show()

特征输出重要性的排序
2.用内置函数

plot_importance(model)
pyplot.show()