簡體   English   中英

從 XGBoost 保存樹

[英]Saving the tree from XGBoost

我正在嘗試將 XGBoost 的決策樹保存為.png文件。 當我用我的隨機森林做到這一點時它工作得很好,但它不適用於我的 XGBoost

我有以下代碼:

import xgboost as xgb
from sklearn.tree import export_graphviz

import warnings
warnings.filterwarnings('ignore')

from sklearn.metrics import mean_absolute_error
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error
from math import sqrt

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
from sklearn import metrics

from sklearn.preprocessing import LabelEncoder


# Using Skicit-learn to split data into training and testing sets
from sklearn.model_selection import train_test_split

df = pd.read_csv("data_clean.csv")
del df["Unnamed: 0"]

df = df[["gross_square_feet","block","land_square_feet","lot","age_of_building","borough","residential_units","commercial_units","total_units","sale_price"]]

df['borough'] = df['borough'].astype('category')


X, y = df.iloc[:,:-1],df.iloc[:,-1]

one_hot_encoded_X = pd.get_dummies(X)
print("# of columns after one-hot encoding: {0}".format(len(one_hot_encoded_X.columns)))

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(one_hot_encoded_X, y, test_size=0.25, random_state=1337)

from xgboost import XGBRegressor

print(np.shape(X_train), np.shape(X_test))

xg_model = XGBRegressor(n_estimators=500,
                        learning_rate=0.075,
                        max_depth = 7,
                        min_child_weight = 5,
                        eval_metric = 'rmse',
                        seed = 1337,
                        objective = 'reg:squarederror')
xg_model.fit(X_train, y_train, early_stopping_rounds=10,
             eval_set=[(X_test, y_test)], verbose=False)

# make predictions
predictions = xg_model.predict(X_test)

image = xgb.to_graphviz(xg_model)
export_graphviz(image, out_file='treexgb.dot', 
                rounded = True, proportion = False, 
                precision = 2, filled = True)


# Convert to png using system command (requires Graphviz)
from subprocess import call
call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])

當我執行此代碼時,我收到以下錯誤:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-2cde9f840069> in <module>()
      3 export_graphviz(image, out_file='treexgb.dot', 
      4                 rounded = True, proportion = False,
----> 5                 precision = 2, filled = True)
      6 
      7 

F:\Softwares\Anaconda\lib\site-packages\sklearn\tree\export.py in export_graphviz(decision_tree, out_file, max_depth, feature_names, class_names, label, filled, leaves_parallel, impurity, node_ids, proportion, rotate, rounded, special_characters, precision)
    390                 out_file.write('%d -> %d ;\n' % (parent, node_id))
    391 
--> 392     check_is_fitted(decision_tree, 'tree_')
    393     own_file = False
    394     return_string = False

F:\Softwares\Anaconda\lib\site-packages\sklearn\utils\validation.py in check_is_fitted(estimator, attributes, msg, all_or_any)
    760 
    761     if not hasattr(estimator, 'fit'):
--> 762         raise TypeError("%s is not an estimator instance." % (estimator))
    763 
    764     if not isinstance(attributes, (list, tuple)):

TypeError: digraph {
    graph [rankdir=UT]
    0 [label="gross_square_feet<2472.5"]
    23 -> 47 [label="yes, missing" color="#0000FF"]
    ...

    112 [label="leaf=22460.7168"]
} is not an estimator instance.

但是,當我執行xgb.to_graphviz(xg_model)它工作得非常好,我只得到一棵樹......

有人知道如何將我的樹 output 作為.png文件嗎?

嘗試這個:

format = 'png' #You should try the 'svg'

image = xgb.to_graphviz(xg_model)

#Set a different dpi (work only if format == 'png')
image.graph_attr = {'dpi':'400'}

image.render('filename', format = format)

資源:

Graphviz 文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM