gpt4 book ai didi

python - 从 XGBoost 保存树

转载 作者:行者123 更新时间:2023-12-03 08:51:48 24 4
gpt4 key购买 nike

我正在尝试将 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)时,它工作得很好,我只得到了一棵树...

有人知道如何将我的树输出为 .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 docs

关于python - 从 XGBoost 保存树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58457929/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com