cerlymarco / linear-tree

A python library to build Model Trees with Linear Models at the leaves.
MIT License
338 stars 54 forks source link

export to graphviz -AttributeError: 'LinearTreeRegressor' object has no attribute 'n_features_' #1

Closed ricmarchao closed 3 years ago

ricmarchao commented 3 years ago

Hi

thanks for writing this great package!

I was trying to display the decision tree with graphviz I get this error

AttributeError: 'LinearTreeRegressor' object has no attribute 'nfeatures'

from lineartree import LinearTreeRegressor from sklearn.linear_model import LinearRegression

reg = LinearTreeRegressor(base_estimator=LinearRegression()) reg.fit(train[x_cols], train["y"])

from graphviz import Source from sklearn import tree

graph = Source( tree.export_graphviz(reg, out_file=None,feature_names=train.columns))

cerlymarco commented 3 years ago

Given a fitted instance...

reg = LinearTreeRegressor(base_estimator=LinearRegression())
reg.fit(X, y)

you can simply obtain a pydot.Dot object calling:

reg.model_to_dot()

you can simply display a tree structure in this way:

reg.plot_model()

as you can see from the examples in the notebook folder

ricmarchao commented 3 years ago

ah alright thanks ! I didn't look at the examples