Note on inappropriate usage of cross_val_predict #9883

Merged
merged 7 commits into from Mar 8, 2018

Conversation

Projects
None yet
4 participants
Contributor

shaz13 commented Oct 8, 2017

Fixes #9827

  • Added a note as suggested in the issue
doc/modules/cross_validation.rst
+folds and grouped in different ways on computation in comparison to :func:`cross_val_predict`.
+Thus, any metric score on :func:`cross_val_predict` is not equivalent to the
+result of :func:`cross_val_score`. The function :func:`cross_val_predict` should
+only be used to evaluate a classifier and is not an appropriate measure of

This comment has been minimized.

Show comment Hide comment
@jnothman

jnothman Oct 8, 2017

Owner

I think I'd should not be used to evaluate a classifier, but perhaps to do some diagnostic analysis or visualisation of predictions...

@jnothman

jnothman Oct 8, 2017

Owner

I think I'd should not be used to evaluate a classifier, but perhaps to do some diagnostic analysis or visualisation of predictions...

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 8, 2017

Contributor

Oh, In that case, a small query. In the original user guide it suggests so --
at http://scikit-learn.org/stable/modules/cross_validation.html
screen shot 2017-10-08 at 10 03 46 am
Shall I remove that line of phrase or just in the notes?

@shaz13

shaz13 Oct 8, 2017

Contributor

Oh, In that case, a small query. In the original user guide it suggests so --
at http://scikit-learn.org/stable/modules/cross_validation.html
screen shot 2017-10-08 at 10 03 46 am
Shall I remove that line of phrase or just in the notes?

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 8, 2017

Contributor

"These prediction can then be used to evaluate the classifier:"

@shaz13

shaz13 Oct 8, 2017

Contributor

"These prediction can then be used to evaluate the classifier:"

This comment has been minimized.

Show comment Hide comment
@jnothman

jnothman Oct 8, 2017

Owner
Owner

jnothman commented Oct 8, 2017

This comment has been minimized.

Show comment Hide comment
@jnothman

jnothman Oct 8, 2017

Owner
Owner

jnothman commented Oct 8, 2017

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 8, 2017

Contributor

@jnothman Sure. I will replace the example with a Note defining the appropriate usage. Thanks!

Contributor

shaz13 commented Oct 8, 2017

@jnothman Sure. I will replace the example with a Note defining the appropriate usage. Thanks!

This comment has been minimized.

Show comment Hide comment
@jnothman

jnothman Oct 9, 2017

Owner

Also, I think the case of blended estimators is worth mentioning.

Owner

jnothman commented Oct 9, 2017

Also, I think the case of blended estimators is worth mentioning.

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 9, 2017

Contributor

Pushed a new commit. Please take a look @jnothman.

Contributor

shaz13 commented Oct 9, 2017

Pushed a new commit. Please take a look @jnothman.

doc/modules/cross_validation.rst
+================================================
+The function :func:`cross_val_score` takes an average of over cross-validation
+folds and is grouped in different ways on computation in comparison to :func:`cross_val_predict`.
+Any metric score on :func:`cross_val_predict` is not equivalent to the

This comment has been minimized.

Show comment Hide comment
@jnothman

jnothman Oct 9, 2017

Owner

That's not true. if all folds are of equal size, and the metric is a mean of sample-wise scores, then they will be identical.

@jnothman

jnothman Oct 9, 2017

Owner

That's not true. if all folds are of equal size, and the metric is a mean of sample-wise scores, then they will be identical.

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 9, 2017

Contributor

Will be corrected in next commit.

@shaz13

shaz13 Oct 9, 2017

Contributor

Will be corrected in next commit.

doc/modules/cross_validation.rst
+Note on inappropriate usage of cross_val_predict
+================================================
+The function :func:`cross_val_score` takes an average of over cross-validation
+folds and is grouped in different ways on computation in comparison to :func:`cross_val_predict`.

This comment has been minimized.

Show comment Hide comment
@jnothman

jnothman Oct 9, 2017

Owner

I find this vague and unclear. What does "is grouped in different ways on computation" mean?

@jnothman

jnothman Oct 9, 2017

Owner

I find this vague and unclear. What does "is grouped in different ways on computation" mean?

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 9, 2017

Contributor

I was reframing the words of this deleted note
"- -Note that the result of this computation may be slightly different from those
-obtained using :func:cross_val_score as the elements are grouped in different
-ways."
Is this not true too ?

@shaz13

shaz13 Oct 9, 2017

Contributor

I was reframing the words of this deleted note
"- -Note that the result of this computation may be slightly different from those
-obtained using :func:cross_val_score as the elements are grouped in different
-ways."
Is this not true too ?

This comment has been minimized.

Show comment Hide comment
@jnothman

jnothman Oct 9, 2017

Owner

The previous wording was readable. The new wording is not.

@jnothman

jnothman Oct 9, 2017

Owner

The previous wording was readable. The new wording is not.

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 9, 2017

Contributor

Thanks! for reviewing. Will make it readable 👍

@shaz13

shaz13 Oct 9, 2017

Contributor

Thanks! for reviewing. Will make it readable 👍

doc/modules/cross_validation.rst
+not an appropriate measure of generalisation error.
+
+The valid use of :func:`cross_val_predict` is for visualization of
+training set predictions and model blending techniques.

This comment has been minimized.

Show comment Hide comment
@jnothman

jnothman Oct 9, 2017

Owner

I think this is again vague. Try to be more specific. Look at where cross_val_predict is currently used in the library, and try to explain it in your own words.

@jnothman

jnothman Oct 9, 2017

Owner

I think this is again vague. Try to be more specific. Look at where cross_val_predict is currently used in the library, and try to explain it in your own words.

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 9, 2017

Contributor

Will do.

@shaz13

shaz13 Oct 9, 2017

Contributor

Will do.

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 9, 2017

Contributor

@jnothman Please let me know if this looks good. I have quoted the note in the comment. If everything looks good will make a push.

The result of :func:cross_val_predict may be slightly different from those
obtained using :func:cross_val_score as the elements are grouped in different
ways. The function :func:cross_val_score takes an average of over cross-validation
folds where as :func:cross_val_predict just returns the labels without taking
average of cross-validation folds. Thus, the function :func:cross_val_predict
is not an appropriate measure of generalisation error.

The :func:cross_val_predict is appropriate for visualisation of training set predictions
with y_train values to study the model on train set. Additionally, it can be used
in model blending techniques to get ensembled predictions.

Contributor

shaz13 commented Oct 9, 2017

@jnothman Please let me know if this looks good. I have quoted the note in the comment. If everything looks good will make a push.

The result of :func:cross_val_predict may be slightly different from those
obtained using :func:cross_val_score as the elements are grouped in different
ways. The function :func:cross_val_score takes an average of over cross-validation
folds where as :func:cross_val_predict just returns the labels without taking
average of cross-validation folds. Thus, the function :func:cross_val_predict
is not an appropriate measure of generalisation error.

The :func:cross_val_predict is appropriate for visualisation of training set predictions
with y_train values to study the model on train set. Additionally, it can be used
in model blending techniques to get ensembled predictions.

This comment has been minimized.

Show comment Hide comment
@jnothman

jnothman Oct 9, 2017

Owner

where as :func:cross_val_predict just returns the labels without taking
average of cross-validation folds.

Well it returns the labels (or probabilities, etc) from several distinct models undistinguished.

Owner

jnothman commented Oct 9, 2017

where as :func:cross_val_predict just returns the labels without taking
average of cross-validation folds.

Well it returns the labels (or probabilities, etc) from several distinct models undistinguished.

This comment has been minimized.

Show comment Hide comment
@jnothman

jnothman Oct 9, 2017

Owner

Maybe "Additionally, it can be used when predictions of one supervised estimator are used to train another estimator in ensemble methods" would be clearer.

Owner

jnothman commented Oct 9, 2017

Maybe "Additionally, it can be used when predictions of one supervised estimator are used to train another estimator in ensemble methods" would be clearer.

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 9, 2017

Contributor

Thanks @jnothman for the corrections. How does this revised note look?

The result of :func:cross_val_predict may be slightly different from those
obtained using :func:cross_val_score as the elements are grouped in different
ways. The function :func:cross_val_score takes an average of over cross-validation
folds where as :func:cross_val_predict just returns the labels (or probabilities, etc) from several distinct models undistinguished without taking average of cross-validation folds. Thus, the function :func:cross_val_predict is not an appropriate measure of generalisation error.

The :func:cross_val_predict is appropriate for visualisation of training set predictions with y_train values to study the model on train set. Additionally, it can be used when predictions of one supervised estimator are used to train another estimator in ensemble methods.

Contributor

shaz13 commented Oct 9, 2017

Thanks @jnothman for the corrections. How does this revised note look?

The result of :func:cross_val_predict may be slightly different from those
obtained using :func:cross_val_score as the elements are grouped in different
ways. The function :func:cross_val_score takes an average of over cross-validation
folds where as :func:cross_val_predict just returns the labels (or probabilities, etc) from several distinct models undistinguished without taking average of cross-validation folds. Thus, the function :func:cross_val_predict is not an appropriate measure of generalisation error.

The :func:cross_val_predict is appropriate for visualisation of training set predictions with y_train values to study the model on train set. Additionally, it can be used when predictions of one supervised estimator are used to train another estimator in ensemble methods.

This comment has been minimized.

Show comment Hide comment
@jnothman

jnothman Oct 9, 2017

Owner
Owner

jnothman commented Oct 9, 2017

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 10, 2017

Contributor

Thanks for the reveiw @jnothman. I have made it shorter. How does this look?

The result of :func:cross_val_predict may be different from those
obtained using :func:cross_val_score as the elements are grouped in different 
ways. The function :func:cross_val_score takes an average of over cross-validation folds where as :func:cross_val_predict simply returns the labels (or probabilities) from several distinct models undistinguished. Thus, :func:cross_val_predict is not an appropriate measure of generalisation error.

The :func:cross_val_predict is appropriate for;

  1. Visualization of training set predictions to study the model on train set.
  2. Model blending: When predictions of one supervised estimator are used to train another estimator in ensemble methods.
Contributor

shaz13 commented Oct 10, 2017

Thanks for the reveiw @jnothman. I have made it shorter. How does this look?

The result of :func:cross_val_predict may be different from those
obtained using :func:cross_val_score as the elements are grouped in different 
ways. The function :func:cross_val_score takes an average of over cross-validation folds where as :func:cross_val_predict simply returns the labels (or probabilities) from several distinct models undistinguished. Thus, :func:cross_val_predict is not an appropriate measure of generalisation error.

The :func:cross_val_predict is appropriate for;

  1. Visualization of training set predictions to study the model on train set.
  2. Model blending: When predictions of one supervised estimator are used to train another estimator in ensemble methods.

This comment has been minimized.

Show comment Hide comment
@jnothman

jnothman Oct 11, 2017

Owner
Owner

jnothman commented Oct 11, 2017

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 11, 2017

Contributor

Pushed a new commit @jnothman. Please take a look!

Contributor

shaz13 commented Oct 11, 2017

Pushed a new commit @jnothman. Please take a look!

doc/modules/cross_validation.rst
- >>> predicted = cross_val_predict(clf, iris.data, iris.target, cv=10)
- >>> metrics.accuracy_score(iris.target, predicted) # doctest: +ELLIPSIS
- 0.973...
+Note on inappropriate usage of cross_val_predict

This comment has been minimized.

Show comment Hide comment
@lesteve

lesteve Oct 13, 2017

Member

Rather than a new section, you should use the warning sphinx directive. Look at other places in the doc where it is used. Either git grep '\.\. warning' or search for .. warning :: in https://raw.githubusercontent.com/scikit-learn/scikit-learn/7dffa20363d095320ede354edf66f8b6123ba121/doc/developers/contributing.rst.

@lesteve

lesteve Oct 13, 2017

Member

Rather than a new section, you should use the warning sphinx directive. Look at other places in the doc where it is used. Either git grep '\.\. warning' or search for .. warning :: in https://raw.githubusercontent.com/scikit-learn/scikit-learn/7dffa20363d095320ede354edf66f8b6123ba121/doc/developers/contributing.rst.

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 15, 2017

Contributor

Done.

@shaz13

shaz13 Oct 15, 2017

Contributor

Done.

doc/modules/cross_validation.rst
+
+The :func:`cross_val_predict` is appropriate for:
+ - Visualization of predictions obtained from different models.
+ - Model blending: When predictions of one supervised estimator are used to train another estimator in ensemble methods.

This comment has been minimized.

Show comment Hide comment
@lesteve

lesteve Oct 13, 2017

Member

This line is quite long, find a good editor that limits automatically the width of the line, e.g. to something like 80. More generally, try to respect the convention you can see from the file you are editing.

@lesteve

lesteve Oct 13, 2017

Member

This line is quite long, find a good editor that limits automatically the width of the line, e.g. to something like 80. More generally, try to respect the convention you can see from the file you are editing.

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 15, 2017

Contributor

Done.

@shaz13

shaz13 Oct 15, 2017

Contributor

Done.

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 15, 2017

Contributor

@lesteve Made changes as suggested. Please take a look

Contributor

shaz13 commented Oct 15, 2017

@lesteve Made changes as suggested. Please take a look

This comment has been minimized.

Show comment Hide comment
doc/modules/cross_validation.rst
- 0.973...
+.. warning:: Note on inappropriate usage of cross_val_predict
+
+The result of :func:`cross_val_predict` may be different from those

This comment has been minimized.

Show comment Hide comment
@jnothman

jnothman Oct 15, 2017

Owner

Please indent

@jnothman

jnothman Oct 15, 2017

Owner

Please indent

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Oct 15, 2017

Contributor

Oops. Missed the diff html. Done.

@shaz13

shaz13 Oct 15, 2017

Contributor

Oops. Missed the diff html. Done.

This comment has been minimized.

Show comment Hide comment
@jnothman

jnothman Oct 15, 2017

Owner
Owner

jnothman commented Oct 15, 2017

This comment has been minimized.

Show comment Hide comment
@shaz13

shaz13 Jan 4, 2018

Contributor

@jnothman Ping. Is this PR good to go?

Contributor

shaz13 commented Jan 4, 2018

@jnothman Ping. Is this PR good to go?

I'm happy enough with this, but a second opinion wouldn't hurt...

LGTM, I pushed some minor changes, will merge after checking the result from Circle

@qinhanmin2014 qinhanmin2014 merged commit 2eb731b into scikit-learn:master Mar 8, 2018

2 of 4 checks passed

continuous-integration/appveyor/pr Waiting for AppVeyor build to complete
Details
lgtm analysis: Python Running analyses for revisions
Details
ci/circleci Your tests passed on CircleCI!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

This comment has been minimized.

Show comment Hide comment
@qinhanmin2014

qinhanmin2014 Mar 8, 2018

Member

Thanks @shaz13 :)

Member

qinhanmin2014 commented Mar 8, 2018

Thanks @shaz13 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment