Я пытаюсь сохранить дерево решений 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
файл