随笔 - 384  文章 - 0  评论 - 35  阅读 - 142万

xgboost如何画决策树

暂时还没有搞清楚xgboost中每一个树的权重是怎么样的,以及每个树的结果和最终的结果之间的关系是怎么样的?后面再补上,

下面如何xgboost中的决策树

复制代码
# -*- coding: utf-8 -*-
"""
Created on Tue Mar  9 16:16:56 2021

@author: Administrator
"""

#%%导入模块
import pandas as pd 
import numpy as np
from scipy import stats
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
plt.rc("font",family="SimHei",size="12")  #解决中文无法显示的问题


#%%导入数据
creditcard = pd.read_csv('D:/信用卡欺诈检测/creditcard.csv/creditcard.csv')
creditcard.info()

import xgboost as xgb
from xgboost import XGBClassifier 
from xgboost import plot_tree
import matplotlib.pyplot as plt

X = creditcard.iloc[:,0:-1]
y = creditcard.Class
model = XGBClassifier(max_depth=4, n_estimators=200, learn_rate=0.1)
model.fit(X, y)


def ceate_feature_map(features):
    outfile = open('xgb.fmap', 'w')
    i = 0
    for feat in features:
        outfile.write('{0}\t{1}\tq\n'.format(i, feat))
        i = i + 1
    outfile.close()
'''
X_train.columns在第一段代码中也已经设置过了。
特别需要注意:列名字中不能有空格。
'''
ceate_feature_map(X.columns)

plot_tree(model, num_trees=199, fmap='xgb.fmap')
fig = plt.gcf()
fig.set_size_inches(150, 100)
#plt.show()
fig.savefig('tree.png')
复制代码

 

 下面简单介绍一下plot_tree参数

 

posted on   小小喽啰  阅读(688)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示