1

I would like to visualize my decision tree with export_graphviz, however I keep on getting the following error:


  File "C:\Users\User\AppData\Local\Continuum\anaconda3\envs\data_science\lib\site-packages\sklearn\utils\validation.py", line 951, in check_is_fitted
    raise NotFittedError(msg % {'name': type(estimator).__name__})

NotFittedError: This Pipeline instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

I am pretty sure my Pipeline is fitted because I call predict in my code which works just fine. Here is the code in question:

from sklearn.tree import DecisionTreeRegressor
import pandas as pd
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.tree import export_graphviz

#Parameters for model building an reproducibility
state = 13

data_age.dropna(inplace=True)
X_age = data_age.iloc[:,0:77]
y_age =  data_age.iloc[:,77]

X = X_age
y = y_age

#split between testing and training set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state= state)

# Pipeline with the regressor
regressors = [DecisionTreeRegressor(random_state = state)]
for reg in regressors:
    steps=[('regressor', reg)]
    pipeline = Pipeline(steps) #seed that controls the random grid search

#Train the model

pipeline.set_params(regressor__max_depth = 5, regressor__min_samples_split =5, regressor__min_samples_leaf = 5).fit(X_train, y_train)
pred = pipeline.predict(X_test)
pipeline.score(X_test, y_test)

export_graphviz(pipeline, out_file='tree.dot')

I know I don't really need the Pipeline here but I would still like to understand what is the problem for future reference and be able to plot a decision tree, whithin a pipeline which has been fitted.

thiR
  • 73
  • 11

2 Answers2

1

So, based on Farseer answer, the last line has to be:


#Train the model
pipeline.set_params(regressor__max_depth = 5, regressor__min_samples_split =5, regressor__min_samples_leaf = 5).fit(X_train, y_train)
pred = pipeline.predict(X_test)
pipeline.score(X_test, y_test)

#export as a .dot file
export_graphviz(regressors[0], out_file='tree.dot')

And now it works.

thiR
  • 73
  • 11
0

Signature of export_graphviz is export_graphviz(decision_tree, ...) as can be seen in documentation.

So, you should pass your decision tree as argument to export_graphviz function and not your Pipeline.

You can also see in source code, that export_grpahviz is calling check_is_fitted(decision_tree, 'tree_') method.

Farseer
  • 4,036
  • 3
  • 42
  • 61