我想用export_graphviz
可视化我的决策树,但是我不断遇到以下错误:
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.
我非常确定我的管道是否合适,因为我在代码中调用了predict,效果很好。这是有问题的代码:
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')
我知道我真的不需要这里的管道,但是我仍然想了解问题所在以备将来参考,并能够在已安装管道的情况下绘制决策树。
答案 0 :(得分:0)
REP
的签名为export_graphviz
,如documentation所示。
因此,您应该将决策树作为参数传递给export_graphviz(decision_tree, ...)
函数,而不是管道。
您还可以在source code中看到export_graphviz
正在调用export_grpahviz
方法。
答案 1 :(得分:0)
因此,根据Farseer的回答,最后一行必须为:
#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')
现在可以使用了。