zoukankan      html  css  js  c++  java
  • 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}	{1}	q
    '.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参数

  • 相关阅读:
    免费申请域名
    分享学习linux网站
    二分法
    node 解决存储xss风险报告
    cf987f AND Graph
    loj2587 「APIO2018」铁人两项
    luogu3830 [SHOI2012]随机树
    luogu3343 [ZJOI2015]地震后的幻想乡
    bzoj2560 串珠子
    luogu3317 [SDOI2014]重建
  • 原文地址:https://www.cnblogs.com/cgmcoding/p/14507860.html
Copyright © 2011-2022 走看看