【Python机器学习】决策树——树的特征重要性

利用一些有用的属性来总结树的工作原理,其中最常用的事特征重要性,它为每个特征树的决策的重要性进行排序。对于每个特征来说,它都是介于0到1之间的数字,其中0代表"根本没有用到",1代表"完美预测目标值"。特征重要性的求和为1。

将特征重要性进行可视化:

python 复制代码
import mglearn.datasets
import numpy as np
from sklearn.tree import DecisionTreeClassifier,export_graphviz
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.datasets import load_breast_cancer


def plot_importances(model):
    n_feature=cancer.data.shape[1]
    plt.barh(range(n_feature),model.feature_importances_,align='center')
    plt.yticks(np.arange(n_feature),cancer.feature_names)
    plt.xlabel('特征重要性')
    plt.ylabel('特征')

plt.rcParams['font.sans-serif'] = ['SimHei']

cancer=load_breast_cancer()
X_train,X_test,y_train,y_test=train_test_split(
    cancer.data,cancer.target,stratify=cancer.target,random_state=42
)
tree=DecisionTreeClassifier(max_depth=4,random_state=0)
tree.fit(X_train,y_train)

plot_importances(tree)
plt.show()

可以看到,"worst radius" 是最重要的特征。

如果某个特征的feature_importance_很小,不代表这个特征没有提供任何信息,只能说明这个特征没有被树选中,可能是因为另一个特征也包含的同样的信息。

与线性模型的系数不同,决策树的特征重要性一定为正数。

相关推荐
吃茄子的猫几秒前
python中global全局变量
python
Flash.kkl1 分钟前
Python基础语法
开发语言·python
veminhe8 分钟前
Python(二) 容器类型与对应操作行为
python
独自归家的兔12 分钟前
基于 cosyvoice-v3-plus 的 个人音色复刻 (华为OBS)
人工智能·华为·语音识别
Legend NO2413 分钟前
如何构建自己高质量语料库?
人工智能·非结构化数据
人工干智能17 分钟前
调用client.beta.threads.runs.create后交由OpenAI云服务器端的处理
服务器·python·llm
Hcoco_me17 分钟前
大模型面试题23:对比学习原理-从通俗理解到核心逻辑(通用AI视角)
人工智能·rnn·深度学习·学习·自然语言处理·word2vec
Java后端的Ai之路18 分钟前
【神经网络基础】-神经网络优化方法全解析
人工智能·深度学习·神经网络·机器学习
高洁0119 分钟前
深度学习—卷积神经网络(2)
人工智能·深度学习·机器学习·transformer·知识图谱
一招定胜负21 分钟前
项目案例:卷积神经网络实现食物图片分类代码详细解析
人工智能·分类·cnn