12

Similar question: What's the advantage of a trailing underscore in Python naming?. This addresses advantages/disadvantages, whereas this addresses the reasoning behind doing it, both broadly and specifically to sklearn.

I am looking through the sklearn documentation, and I noticed that the sklearn.model_selection.GridSearchCV attributes all end in underscore. For example:

  • cv_results_
  • best_params_
  • best_score_

Why is this? What does the underscore do? Please be as broad as possible in your answer (i.e. don't just refer to sklearn's GridSearchCV.

I'm assuming this isn't just an sklearn thing, and I have no idea what the appropriate tag is for this so I'm tagging sklearn. Please correct the tags (or me!).

8
  • 1
    The underscore itself has no meaning. It's most likely author's preference for naming convention (of the result(s)). There are many authors that write separate modules in sklearn.
    – Jon
    Commented Mar 6, 2018 at 19:41
  • @Jon have you ever come across this convention before? If so, have you observed any informal reason for it?
    – quanty
    Commented Mar 6, 2018 at 20:02
  • Possible duplicate of What's the advantage of a trailing underscore in Python naming? Commented Mar 7, 2018 at 4:57
  • 1
    I now added a passage from the sklearn developer guide which addresses it. They have a specific interpretation for this.
    – Marcus V.
    Commented Mar 7, 2018 at 7:38
  • 2
    @quanty: I would say it depends on your intent. If it was specific for sklearn, then it is no duplicate. If you are interested in general Python, then Vivek Kumar is right, and it is a duplicate. If it is the first, then I think my answer is the more correct one though ;)
    – Marcus V.
    Commented Mar 7, 2018 at 13:46

1 Answer 1

25

For sklearn, there is a specific interpretation. Check the sklearn developer guideline, which has a note on this. The convention is used for attributes of estimators that have a meaningful value after fit() was called.

These are then used to for instance check if the estimator was fitted, see for instance here:

class LinearModel(six.with_metaclass(ABCMeta, BaseEstimator)):
    """Base class for Linear Models"""

    [...]

    def _decision_function(self, X):
        check_is_fitted(self, "coef_")

   [...]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.