尽管已安装GridSearchCV,但仍未安装GridSearchCV时发生错误

时间:2020-06-03 10:26:48

标签: python-3.x scikit-learn decision-tree grid-search

因此,我训练了决策树分类器模型,并使用GridSearchCV输出来绘制树图。这是我的决策树模型代码:

from sklearn.tree import DecisionTreeClassifier

# function for decision tree using grid search cross validation to find optimal hyperparameters
def decisionTree(X_train, X_test, y_train, y_test, cv=10):
    dt = DecisionTreeClassifier(random_state=0)
    params = {'max_depth': [3,4,5,6], 'min_samples_leaf': [0.02,0.03,0.04,0.06,0.08,0.3,0.4]}
    dt_clf = GridSearchCV(estimator=dt, param_grid=params, scoring='f1', cv=cv, return_train_score=True)
    dt_clf.fit(X_train,y_train)
    print("Best parameters set found on Cross Validation:\n\n", dt_clf.best_params_)
    print("\nCross Validation F1 score:\n\n", dt_clf.best_score_.round(3))

    # predict test set  
    y_pred = dt_clf.predict(X_test)
    print('\nTest set scores:')
    return {'Accuracy': accuracy_score(y_test, y_pred).round(3), 'Precision': precision_score(y_test, y_pred).round(3), 
        'Recall': recall_score(y_test, y_pred).round(3), 'F1': f1_score(y_test, y_pred).round(3)}, dt_clf

scores, dt_clf = decisionTree(X_train, X_test, y_train, y_test, cv=10) 
scores

结果:

Best parameters set found on Cross Validation:

 {'max_depth': 5, 'min_samples_leaf': 0.03}

Cross Validation F1 score:

 0.753

Test set scores:
{'Accuracy': 0.833, 'Precision': 0.793, 'Recall': 0.852, 'F1': 0.821}

但是,当我使用graphiz_export时,它表示尚未安装GridSearchCV:

from sklearn.tree import export_graphviz 

dot_data = export_graphviz(dt_clf,
feature_names=list(X_train.columns),
class_names=['No Heart Disease', 'Heart Disease'],
out_file=None,
filled=True,
rounded=True,
special_characters=True)

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

我似乎无法弄清楚问题出在哪里!

1 个答案:

答案 0 :(得分:2)

1。您错过了其他地方的东西,因为该对象确实合适。 要使用check_is_fitted()进行检查。

2。您需要将最佳估计量传递给export_graphviz(),而不是Gridsearch,即export_graphviz(dt_clf.best_estimator_)


示例:

from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import pandas as pd, numpy as np
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import accuracy_score,precision_score, recall_score, f1_score
from sklearn.tree import export_graphviz 
from sklearn.utils.validation import check_is_fitted

data = load_iris()

X = data.data
y=data.target
tmp = y!=2
y = y[tmp]
X = X[tmp, :]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

# function for decision tree using grid search cross validation to find optimal hyperparameters
def decisionTree(X_train, X_test, y_train, y_test, cv=10):
    dt = DecisionTreeClassifier(random_state=0)
    params = {'max_depth': [3,4,5,6], 'min_samples_leaf': [0.02,0.03,0.04,0.06,0.08,0.3,0.4]}
    dt_clf = GridSearchCV(estimator=dt, param_grid=params, scoring='f1', cv=cv, return_train_score=True)
    dt_clf.fit(X_train,y_train)
    print("Best parameters set found on Cross Validation:\n\n", dt_clf.best_params_)
    print("\nCross Validation F1 score:\n\n", dt_clf.best_score_.round(3))

    # predict test set  
    y_pred = dt_clf.predict(X_test)
    print('\nTest set scores:')
    return {'Accuracy': accuracy_score(y_test, y_pred).round(3), 'Precision': precision_score(y_test, y_pred).round(3), 
        'Recall': recall_score(y_test, y_pred).round(3), 'F1': f1_score(y_test, y_pred).round(3)}, dt_clf

scores, dt_clf = decisionTree(X_train, X_test, y_train, y_test, cv=10) 

check_is_fitted(dt_clf)
# this passes

dot_data = export_graphviz(dt_clf.best_estimator_,
feature_names=list(data.feature_names),
class_names=['No Heart Disease', 'Heart Disease'],
out_file=None,
filled=True,
rounded=True,
special_characters=True)