Grid search model isn't recognized as fitted for Graphviz
$begingroup$
I find this really weird, and the code is really straight forward.
What am I doing wrong ?
from sklearn.model_selection import GridSearchCV
scoring_type="accuracy"
preprocess_data(X,y,0)
p_grid = {'min_samples_split':np.arange(2,10),'min_samples_leaf': np.arange(1,10)}
Tree_opt = GridSearchCV(estimator=DecisionTreeClassifier(random_state=42), param_grid=p_grid, scoring=scoring_type, cv=10)
Tree_opt.fit(X_train,y_train)
print("Best training params: {}".format(Tree_opt.best_params_))
print("Best training Score: {}".format(Tree_opt.best_score_))
dot_data = tree.export_graphviz(Tree_opt, out_file=None,feature_names=labels,class_names=class_names,filled=True, rounded=True,special_characters=True)
graph = graphviz.Source(dot_data)
graph
print(Tree_opt.score(X_test, y_test))
And I'm getting the following error:
NotFittedError Traceback (most recent call last)
<ipython-input-14-1c7fa906f99b> in <module>
8 print("Best training params: {}".format(Tree_opt.best_params_))
9 print("Best training Score: {}".format(Tree_opt.best_score_))
---> 10 dot_data = tree.export_graphviz(Tree_opt, out_file=None,feature_names=labels,class_names=class_names,filled=True, rounded=True,special_characters=True)
11 graph = graphviz.Source(dot_data)
12
~Anaconda3libsite-packagessklearntreeexport.py in export_graphviz(decision_tree, out_file, max_depth, feature_names, class_names, label, filled, leaves_parallel, impurity, node_ids, proportion, rotate, rounded, special_characters, precision)
394 out_file.write('%d -> %d ;n' % (parent, node_id))
395
--> 396 check_is_fitted(decision_tree, 'tree_')
397 own_file = False
398 return_string = False
~Anaconda3libsite-packagessklearnutilsvalidation.py in check_is_fitted(estimator, attributes, msg, all_or_any)
949
950 if not all_or_any([hasattr(estimator, attr) for attr in attributes]):
--> 951 raise NotFittedError(msg % {'name': type(estimator).__name__})
952
953
NotFittedError: This GridSearchCV instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.
I also have this warning I don't think it's important:
C:UsersFlowAnaconda3libsite-packagessklearnmodel_selection_search.py:841: DeprecationWarning: The default of the `iid` parameter will change from True to False in version 0.22 and will be removed in 0.24. This will change numeric results when test-set sizes are unequal.
DeprecationWarning)
I think the fit works because I can get the first prints as follows:
Best training params: {'min_samples_leaf': 3, 'min_samples_split': 2}
Best training Score: 0.8809523809523809
decision-trees graphs grid-search gridsearchcv
New contributor
$endgroup$
add a comment |
$begingroup$
I find this really weird, and the code is really straight forward.
What am I doing wrong ?
from sklearn.model_selection import GridSearchCV
scoring_type="accuracy"
preprocess_data(X,y,0)
p_grid = {'min_samples_split':np.arange(2,10),'min_samples_leaf': np.arange(1,10)}
Tree_opt = GridSearchCV(estimator=DecisionTreeClassifier(random_state=42), param_grid=p_grid, scoring=scoring_type, cv=10)
Tree_opt.fit(X_train,y_train)
print("Best training params: {}".format(Tree_opt.best_params_))
print("Best training Score: {}".format(Tree_opt.best_score_))
dot_data = tree.export_graphviz(Tree_opt, out_file=None,feature_names=labels,class_names=class_names,filled=True, rounded=True,special_characters=True)
graph = graphviz.Source(dot_data)
graph
print(Tree_opt.score(X_test, y_test))
And I'm getting the following error:
NotFittedError Traceback (most recent call last)
<ipython-input-14-1c7fa906f99b> in <module>
8 print("Best training params: {}".format(Tree_opt.best_params_))
9 print("Best training Score: {}".format(Tree_opt.best_score_))
---> 10 dot_data = tree.export_graphviz(Tree_opt, out_file=None,feature_names=labels,class_names=class_names,filled=True, rounded=True,special_characters=True)
11 graph = graphviz.Source(dot_data)
12
~Anaconda3libsite-packagessklearntreeexport.py in export_graphviz(decision_tree, out_file, max_depth, feature_names, class_names, label, filled, leaves_parallel, impurity, node_ids, proportion, rotate, rounded, special_characters, precision)
394 out_file.write('%d -> %d ;n' % (parent, node_id))
395
--> 396 check_is_fitted(decision_tree, 'tree_')
397 own_file = False
398 return_string = False
~Anaconda3libsite-packagessklearnutilsvalidation.py in check_is_fitted(estimator, attributes, msg, all_or_any)
949
950 if not all_or_any([hasattr(estimator, attr) for attr in attributes]):
--> 951 raise NotFittedError(msg % {'name': type(estimator).__name__})
952
953
NotFittedError: This GridSearchCV instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.
I also have this warning I don't think it's important:
C:UsersFlowAnaconda3libsite-packagessklearnmodel_selection_search.py:841: DeprecationWarning: The default of the `iid` parameter will change from True to False in version 0.22 and will be removed in 0.24. This will change numeric results when test-set sizes are unequal.
DeprecationWarning)
I think the fit works because I can get the first prints as follows:
Best training params: {'min_samples_leaf': 3, 'min_samples_split': 2}
Best training Score: 0.8809523809523809
decision-trees graphs grid-search gridsearchcv
New contributor
$endgroup$
add a comment |
$begingroup$
I find this really weird, and the code is really straight forward.
What am I doing wrong ?
from sklearn.model_selection import GridSearchCV
scoring_type="accuracy"
preprocess_data(X,y,0)
p_grid = {'min_samples_split':np.arange(2,10),'min_samples_leaf': np.arange(1,10)}
Tree_opt = GridSearchCV(estimator=DecisionTreeClassifier(random_state=42), param_grid=p_grid, scoring=scoring_type, cv=10)
Tree_opt.fit(X_train,y_train)
print("Best training params: {}".format(Tree_opt.best_params_))
print("Best training Score: {}".format(Tree_opt.best_score_))
dot_data = tree.export_graphviz(Tree_opt, out_file=None,feature_names=labels,class_names=class_names,filled=True, rounded=True,special_characters=True)
graph = graphviz.Source(dot_data)
graph
print(Tree_opt.score(X_test, y_test))
And I'm getting the following error:
NotFittedError Traceback (most recent call last)
<ipython-input-14-1c7fa906f99b> in <module>
8 print("Best training params: {}".format(Tree_opt.best_params_))
9 print("Best training Score: {}".format(Tree_opt.best_score_))
---> 10 dot_data = tree.export_graphviz(Tree_opt, out_file=None,feature_names=labels,class_names=class_names,filled=True, rounded=True,special_characters=True)
11 graph = graphviz.Source(dot_data)
12
~Anaconda3libsite-packagessklearntreeexport.py in export_graphviz(decision_tree, out_file, max_depth, feature_names, class_names, label, filled, leaves_parallel, impurity, node_ids, proportion, rotate, rounded, special_characters, precision)
394 out_file.write('%d -> %d ;n' % (parent, node_id))
395
--> 396 check_is_fitted(decision_tree, 'tree_')
397 own_file = False
398 return_string = False
~Anaconda3libsite-packagessklearnutilsvalidation.py in check_is_fitted(estimator, attributes, msg, all_or_any)
949
950 if not all_or_any([hasattr(estimator, attr) for attr in attributes]):
--> 951 raise NotFittedError(msg % {'name': type(estimator).__name__})
952
953
NotFittedError: This GridSearchCV instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.
I also have this warning I don't think it's important:
C:UsersFlowAnaconda3libsite-packagessklearnmodel_selection_search.py:841: DeprecationWarning: The default of the `iid` parameter will change from True to False in version 0.22 and will be removed in 0.24. This will change numeric results when test-set sizes are unequal.
DeprecationWarning)
I think the fit works because I can get the first prints as follows:
Best training params: {'min_samples_leaf': 3, 'min_samples_split': 2}
Best training Score: 0.8809523809523809
decision-trees graphs grid-search gridsearchcv
New contributor
$endgroup$
I find this really weird, and the code is really straight forward.
What am I doing wrong ?
from sklearn.model_selection import GridSearchCV
scoring_type="accuracy"
preprocess_data(X,y,0)
p_grid = {'min_samples_split':np.arange(2,10),'min_samples_leaf': np.arange(1,10)}
Tree_opt = GridSearchCV(estimator=DecisionTreeClassifier(random_state=42), param_grid=p_grid, scoring=scoring_type, cv=10)
Tree_opt.fit(X_train,y_train)
print("Best training params: {}".format(Tree_opt.best_params_))
print("Best training Score: {}".format(Tree_opt.best_score_))
dot_data = tree.export_graphviz(Tree_opt, out_file=None,feature_names=labels,class_names=class_names,filled=True, rounded=True,special_characters=True)
graph = graphviz.Source(dot_data)
graph
print(Tree_opt.score(X_test, y_test))
And I'm getting the following error:
NotFittedError Traceback (most recent call last)
<ipython-input-14-1c7fa906f99b> in <module>
8 print("Best training params: {}".format(Tree_opt.best_params_))
9 print("Best training Score: {}".format(Tree_opt.best_score_))
---> 10 dot_data = tree.export_graphviz(Tree_opt, out_file=None,feature_names=labels,class_names=class_names,filled=True, rounded=True,special_characters=True)
11 graph = graphviz.Source(dot_data)
12
~Anaconda3libsite-packagessklearntreeexport.py in export_graphviz(decision_tree, out_file, max_depth, feature_names, class_names, label, filled, leaves_parallel, impurity, node_ids, proportion, rotate, rounded, special_characters, precision)
394 out_file.write('%d -> %d ;n' % (parent, node_id))
395
--> 396 check_is_fitted(decision_tree, 'tree_')
397 own_file = False
398 return_string = False
~Anaconda3libsite-packagessklearnutilsvalidation.py in check_is_fitted(estimator, attributes, msg, all_or_any)
949
950 if not all_or_any([hasattr(estimator, attr) for attr in attributes]):
--> 951 raise NotFittedError(msg % {'name': type(estimator).__name__})
952
953
NotFittedError: This GridSearchCV instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.
I also have this warning I don't think it's important:
C:UsersFlowAnaconda3libsite-packagessklearnmodel_selection_search.py:841: DeprecationWarning: The default of the `iid` parameter will change from True to False in version 0.22 and will be removed in 0.24. This will change numeric results when test-set sizes are unequal.
DeprecationWarning)
I think the fit works because I can get the first prints as follows:
Best training params: {'min_samples_leaf': 3, 'min_samples_split': 2}
Best training Score: 0.8809523809523809
decision-trees graphs grid-search gridsearchcv
decision-trees graphs grid-search gridsearchcv
New contributor
New contributor
New contributor
asked 2 days ago
Florian LabordeFlorian Laborde
152
152
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Try using Tree_opt.best_estimator_
. The grid search object passes fit and score functions through to the best estimator when refit=True
, but other (esp. more specific) methods need to be called directly from best estimator object.
$endgroup$
$begingroup$
Thanks ! the following code did great.
$endgroup$
– Florian Laborde
yesterday
add a comment |
$begingroup$
Here is the modified correct code :
from sklearn.model_selection import GridSearchCV
scoring_type="accuracy"
preprocess_data(X,y,0)
p_grid = {'min_samples_split':np.arange(2,10),'min_samples_leaf': np.arange(1,10)}
Tree_opt = GridSearchCV(estimator=DecisionTreeClassifier(random_state=42), param_grid=p_grid, scoring=scoring_type, cv=10, refit=True)
Tree_opt.fit(X_train,y_train)
print("Best training params: {}".format(Tree_opt.best_params_))
print("Best training Score: {}".format(Tree_opt.best_score_))
print("Best training Score: {}".format(Tree_opt.best_estimator_))
Tree_opt.best_estimator_.fit(X_train, y_train)
dot_data = tree.export_graphviz(Tree_opt.best_estimator_, out_file=None,feature_names=labels,class_names=class_names,filled=True, rounded=True,special_characters=True)
graph = graphviz.Source(dot_data)
graph
Indeed we need to call on .best_estimator_ instance
New contributor
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "557"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Florian Laborde is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f47046%2fgrid-search-model-isnt-recognized-as-fitted-for-graphviz%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Try using Tree_opt.best_estimator_
. The grid search object passes fit and score functions through to the best estimator when refit=True
, but other (esp. more specific) methods need to be called directly from best estimator object.
$endgroup$
$begingroup$
Thanks ! the following code did great.
$endgroup$
– Florian Laborde
yesterday
add a comment |
$begingroup$
Try using Tree_opt.best_estimator_
. The grid search object passes fit and score functions through to the best estimator when refit=True
, but other (esp. more specific) methods need to be called directly from best estimator object.
$endgroup$
$begingroup$
Thanks ! the following code did great.
$endgroup$
– Florian Laborde
yesterday
add a comment |
$begingroup$
Try using Tree_opt.best_estimator_
. The grid search object passes fit and score functions through to the best estimator when refit=True
, but other (esp. more specific) methods need to be called directly from best estimator object.
$endgroup$
Try using Tree_opt.best_estimator_
. The grid search object passes fit and score functions through to the best estimator when refit=True
, but other (esp. more specific) methods need to be called directly from best estimator object.
answered 2 days ago
Ben ReinigerBen Reiniger
18419
18419
$begingroup$
Thanks ! the following code did great.
$endgroup$
– Florian Laborde
yesterday
add a comment |
$begingroup$
Thanks ! the following code did great.
$endgroup$
– Florian Laborde
yesterday
$begingroup$
Thanks ! the following code did great.
$endgroup$
– Florian Laborde
yesterday
$begingroup$
Thanks ! the following code did great.
$endgroup$
– Florian Laborde
yesterday
add a comment |
$begingroup$
Here is the modified correct code :
from sklearn.model_selection import GridSearchCV
scoring_type="accuracy"
preprocess_data(X,y,0)
p_grid = {'min_samples_split':np.arange(2,10),'min_samples_leaf': np.arange(1,10)}
Tree_opt = GridSearchCV(estimator=DecisionTreeClassifier(random_state=42), param_grid=p_grid, scoring=scoring_type, cv=10, refit=True)
Tree_opt.fit(X_train,y_train)
print("Best training params: {}".format(Tree_opt.best_params_))
print("Best training Score: {}".format(Tree_opt.best_score_))
print("Best training Score: {}".format(Tree_opt.best_estimator_))
Tree_opt.best_estimator_.fit(X_train, y_train)
dot_data = tree.export_graphviz(Tree_opt.best_estimator_, out_file=None,feature_names=labels,class_names=class_names,filled=True, rounded=True,special_characters=True)
graph = graphviz.Source(dot_data)
graph
Indeed we need to call on .best_estimator_ instance
New contributor
$endgroup$
add a comment |
$begingroup$
Here is the modified correct code :
from sklearn.model_selection import GridSearchCV
scoring_type="accuracy"
preprocess_data(X,y,0)
p_grid = {'min_samples_split':np.arange(2,10),'min_samples_leaf': np.arange(1,10)}
Tree_opt = GridSearchCV(estimator=DecisionTreeClassifier(random_state=42), param_grid=p_grid, scoring=scoring_type, cv=10, refit=True)
Tree_opt.fit(X_train,y_train)
print("Best training params: {}".format(Tree_opt.best_params_))
print("Best training Score: {}".format(Tree_opt.best_score_))
print("Best training Score: {}".format(Tree_opt.best_estimator_))
Tree_opt.best_estimator_.fit(X_train, y_train)
dot_data = tree.export_graphviz(Tree_opt.best_estimator_, out_file=None,feature_names=labels,class_names=class_names,filled=True, rounded=True,special_characters=True)
graph = graphviz.Source(dot_data)
graph
Indeed we need to call on .best_estimator_ instance
New contributor
$endgroup$
add a comment |
$begingroup$
Here is the modified correct code :
from sklearn.model_selection import GridSearchCV
scoring_type="accuracy"
preprocess_data(X,y,0)
p_grid = {'min_samples_split':np.arange(2,10),'min_samples_leaf': np.arange(1,10)}
Tree_opt = GridSearchCV(estimator=DecisionTreeClassifier(random_state=42), param_grid=p_grid, scoring=scoring_type, cv=10, refit=True)
Tree_opt.fit(X_train,y_train)
print("Best training params: {}".format(Tree_opt.best_params_))
print("Best training Score: {}".format(Tree_opt.best_score_))
print("Best training Score: {}".format(Tree_opt.best_estimator_))
Tree_opt.best_estimator_.fit(X_train, y_train)
dot_data = tree.export_graphviz(Tree_opt.best_estimator_, out_file=None,feature_names=labels,class_names=class_names,filled=True, rounded=True,special_characters=True)
graph = graphviz.Source(dot_data)
graph
Indeed we need to call on .best_estimator_ instance
New contributor
$endgroup$
Here is the modified correct code :
from sklearn.model_selection import GridSearchCV
scoring_type="accuracy"
preprocess_data(X,y,0)
p_grid = {'min_samples_split':np.arange(2,10),'min_samples_leaf': np.arange(1,10)}
Tree_opt = GridSearchCV(estimator=DecisionTreeClassifier(random_state=42), param_grid=p_grid, scoring=scoring_type, cv=10, refit=True)
Tree_opt.fit(X_train,y_train)
print("Best training params: {}".format(Tree_opt.best_params_))
print("Best training Score: {}".format(Tree_opt.best_score_))
print("Best training Score: {}".format(Tree_opt.best_estimator_))
Tree_opt.best_estimator_.fit(X_train, y_train)
dot_data = tree.export_graphviz(Tree_opt.best_estimator_, out_file=None,feature_names=labels,class_names=class_names,filled=True, rounded=True,special_characters=True)
graph = graphviz.Source(dot_data)
graph
Indeed we need to call on .best_estimator_ instance
New contributor
New contributor
answered yesterday
Florian LabordeFlorian Laborde
152
152
New contributor
New contributor
add a comment |
add a comment |
Florian Laborde is a new contributor. Be nice, and check out our Code of Conduct.
Florian Laborde is a new contributor. Be nice, and check out our Code of Conduct.
Florian Laborde is a new contributor. Be nice, and check out our Code of Conduct.
Florian Laborde is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Data Science Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f47046%2fgrid-search-model-isnt-recognized-as-fitted-for-graphviz%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown