【python画图_特征重要性(barh、bar)】

我们建立模型之后,想要直观了解模型中字段的重要性,一般我们使用barh/bar来绘制图形。这里我们以决策树为例,使用feature_importances_计算特征重要性。

(关于特征重要性的具体计算逻辑,我们会在后面的文章中做详细介绍)

import numpy as np
import pandas as pd
from pandas import DataFrame as df
from sklearn.datasets import load_boston
from sklearn.datasets import load_breast_cancer
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
# 提取数据
data = df(load_breast_cancer().data, columns=load_breast_cancer().feature_names)
data['target'] =  load_breast_cancer().target
x_train, x_test, y_train, y_teest = train_test_split(data.iloc[:,:-1], data['target'], test_size=0.2,random_state=1)

# 建模
from sklearn.tree import DecisionTreeClassifier
DT = DecisionTreeClassifier(random_state=1, max_depth=4) # 深度设置为4,默认按照gini进行分裂
DT.fit(x_train, y_train)

###### 1、feature_importances_(适用于决策树、随机森林、GBDT、xgboost、lightgbm)
# 重要性
features_import = df(x_train.columns, columns=['feature'])
features_import['importance'] = DT.feature_importances_  # 默认按照gini计算特征重要性
features_import.sort_values('importance', inplace=True)
# 绘图
from matplotlib import pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 显示中文黑体
# plt.rcParams['axes.unicode_minus'] = False # 负值显示
plt.barh(features_import['feature'], features_import['importance'], height=0.7, color='#008792', edgecolor='#005344') # 更多颜色可参见颜色大全
plt.xlabel('feature importance') # x 轴
plt.ylabel('features') # y轴
plt.title('Feature Importances') # 标题
for a,b in zip( features_import['importance'],features_import['feature']): # 添加数字标签
   print(a,b)
   plt.text(a+0.001, b,'%.3f'%float(a)) # a+0.001代表标签位置在柱形图上方0.001处
plt.show()


'''
    plt.bar():正常柱状图;
    plt.barh():横向的柱状图;
Args:(plt.bar)    
    x:x坐标
    height:高度
    width=0.8:宽度,取值在0~1之间;
    bottom:柱状图的起始位置,也就是y轴的起始坐标
    align='center':柱状图的中心位置,'edge'边缘
    color:柱状图颜色
    edgecolor:边框颜色
    linewidth:边框宽度
    tick_label:下标标签
    log:柱状图y周使用科学计算方法,bool类型
    orientation:柱状图是竖直还是水平,竖直:“vertical”,水平条:“horizontal”
'''

【python画图_特征重要性(barh、bar)】_第1张图片

你可能感兴趣的:(机器学习,python,决策树,机器学习)