Skip to content

Commit 2bf5e48

Browse files
committed
take best tree if possible
1 parent f46b3e3 commit 2bf5e48

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

treeplot/examples.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030

3131
ax = treeplot.plot(model_gradientboost)
3232

33+
# %% XGBOOST EXAMPLE
34+
import xgboost as xgb
35+
model_xgb = xgb.XGBClassifier(n_estimators=100, max_depth=2, random_state=0).fit(X, y)
36+
37+
ax = treeplot.plot(model_xgb)
38+
ax = treeplot.xgboost(model_xgb, plottype='vertical')
39+
3340
# %% XGBOOST EXAMPLE
3441
from xgboost import XGBClassifier
3542
model_xgb = XGBClassifier(n_estimators=100, max_depth=2, random_state=0).fit(X, y)

treeplot/treeplot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ def xgboost(model, featnames=None, num_trees=None, plottype='horizontal', figsiz
101101

102102
if plottype=='horizontal': plottype='UD'
103103
if plottype=='vertical': plottype='LR'
104-
if num_trees is None: num_trees = model.best_iteration
104+
if (num_trees is None) and hasattr(model, 'best_iteration'):
105+
num_trees = model.best_iteration
106+
elif num_trees is None:
107+
num_trees = 0
105108

106109
try:
107110
fig, ax = plt.subplots(1, 1, figsize=figsize)

0 commit comments

Comments
 (0)