Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_plot_tree(self):
gbm = lgb.LGBMClassifier(n_estimators=10, num_leaves=3, silent=True)
gbm.fit(self.X_train, self.y_train, verbose=False)
self.assertRaises(IndexError, lgb.plot_tree, gbm, tree_index=83)
ax = lgb.plot_tree(gbm, tree_index=3, figsize=(15, 8), show_info=['split_gain'])
self.assertIsInstance(ax, matplotlib.axes.Axes)
w, h = ax.axes.get_figure().get_size_inches()
self.assertEqual(int(w), 15)
self.assertEqual(int(h), 8)
def test_plot_tree(self):
gbm = lgb.LGBMClassifier(n_estimators=10, num_leaves=3, silent=True)
gbm.fit(self.X_train, self.y_train, verbose=False)
self.assertRaises(IndexError, lgb.plot_tree, gbm, tree_index=83)
ax = lgb.plot_tree(gbm, tree_index=3, figsize=(15, 8), show_info=['split_gain'])
self.assertIsInstance(ax, matplotlib.axes.Axes)
w, h = ax.axes.get_figure().get_size_inches()
self.assertEqual(int(w), 15)
self.assertEqual(int(h), 8)
out_file=None,
feature_names=self.features,
class_names=classes,
rounded=True,
precision=True,
filled=True,
)
)
display(SVG(graph.pipe(format="svg")))
elif isinstance(self.model, xgb.XGBModel):
return xgb.plot_tree(self.model)
elif isinstance(self.model, lgb.sklearn.LGBMModel):
return lgb.plot_tree(self.model)
elif isinstance(self.model, cb.CatBoost):
return self.model.plot_tree(tree_idx=tree_num, pool=self.pool)
elif isinstance(self.model, sklearn.ensemble.BaseEnsemble):
estimator = self.model.estimators_[tree_num]
graph = Source(
sklearn.tree.export_graphviz(
estimator,
out_file=None,
feature_names=self.features,
class_names=classes,
rounded=True,
precision=True,
filled=True,
out_file=None,
feature_names=self.features,
class_names=classes,
rounded=True,
precision=True,
filled=True,
)
)
display(SVG(graph.pipe(format="svg")))
elif isinstance(self.model, xgb.XGBModel):
return xgb.plot_tree(self.model)
elif isinstance(self.model, lgb.sklearn.LGBMModel):
return lgb.plot_tree(self.model)
elif isinstance(self.model, cb.CatBoost):
return self.model.plot_tree(tree_idx=tree_num, pool=self.pool)
elif isinstance(self.model, sklearn.ensemble.BaseEnsemble):
estimator = self.model.estimators_[tree_num]
graph = Source(
sklearn.tree.export_graphviz(
estimator,
out_file=None,
feature_names=self.features,
class_names=classes,
rounded=True,
precision=True,
filled=True,
out_file=None,
feature_names=self.features,
class_names=classes,
rounded=True,
precision=True,
filled=True,
)
)
display(SVG(graph.pipe(format="svg")))
elif isinstance(self.model, xgb.XGBModel):
return xgb.plot_tree(self.model)
elif isinstance(self.model, lgb.sklearn.LGBMModel):
return lgb.plot_tree(self.model)
elif isinstance(self.model, sklearn.ensemble.BaseEnsemble):
estimator = self.model.estimators_[tree_num]
graph = Source(
sklearn.tree.export_graphviz(
estimator,
out_file=None,
feature_names=self.features,
class_names=classes,
rounded=True,
precision=True,
filled=True,
)
)
verbose_eval=10)
print('Plotting metrics recorded during training...')
ax = lgb.plot_metric(evals_result, metric='l1')
plt.show()
print('Plotting feature importances...')
ax = lgb.plot_importance(gbm, max_num_features=10)
plt.show()
print('Plotting split value histogram...')
ax = lgb.plot_split_value_histogram(gbm, feature='f26', bins='auto')
plt.show()
print('Plotting 54th tree...') # one tree use categorical feature to split
ax = lgb.plot_tree(gbm, tree_index=53, figsize=(15, 15), show_info=['split_gain'])
plt.show()
print('Plotting 54th tree with graphviz...')
graph = lgb.create_tree_digraph(gbm, tree_index=53, name='Tree54')
graph.render(view=True)