Skip to content

Add a sklearn.plot Module #13448

Closed
Closed

Description

@thomasjpfan

Here is an overview of the work done so far relating to plotting:

To help control the scope of sklearn.plot, I propose we only do plotting on the axes level and not the figure level. The user would pass in the axes as a keyword. As a convenience, the default of axes will be None. Only in this case, will the plotting function generate a axes/figure to plot on.

Activity

  1. qinhanmin2014 commented on Mar 14, 2019

    @qinhanmin2014
    Member

    Thanks for opening this issue, ping @jnothman @amueller @GaelVaroquaux according to gitter
    #8425 is not related to Decision Regions of Classifiers,?
    I prefer to move plot_tree and plot_partial_dependence to sklearn.plot and solve #13335 in 0.21 (maybe introduce a function to plot the decision boundary, since it's important and not easy for beginners IMO). What do others think?

    To help control the scope of sklearn.plot, I propose we only do plotting on the axes level and not the figure level. The user would pass in the axes as a keyword. As a convenience, the default of axes will be None. Only in this case, will the plotting function generate a axes/figure to plot on.

    Good idea, but not consistent with existing functions (plot_tree and plot_partial_dependence), right?

  2. jnothman commented on Mar 14, 2019

    @jnothman
    Member
  3. thomasjpfan commented on Mar 15, 2019

    @thomasjpfan
    MemberAuthor

    Good idea, but not consistent with existing functions (plot_tree and plot_partial_dependence), right?

    @qinhanmin2014 plot_tree does not seem to adjust the figure. plot_partial_dependence does make multiple plots based on features. Although, it can be refactored into an axes level plot. A user would need to call plot_partial_dependence multiple times giving it different axes (and features).

    Can you give reasons why you want to limit it to axes?

    @jnothman Seaborn has documentation that clearly separates "figure-level" plot and "axes-level" plots. If we can properly document this behavior in scikit-learn, it is possible to have these "figure-level" plots. My biggest concern with "figure-level" plots, is they are harder to maintain and test. There will be elements from one axes that may overlap with another axes. Although, we can work around this by structuring figures in such a way that overlapping happens less often.

    In terms of testing, we can go the way of seaborn and test directly matplotlib objects or the yellowbrick way where we do pixel level testing. I tend to favor the testing the matplotlib objects.

  4. GaelVaroquaux commented on Mar 15, 2019

    @GaelVaroquaux
    Member

    My 2 cents:

    • +1 for containing the functions accessing matplotlib in a common subpackage, or, on a module in each subpackage (sklearn.linear_models.plot, sklearn.ensemble.plot).

    • As mentioned by @thomasjpfan, only accessing axes makes it easier to test.

      Also, a long time ago, there was discussion in the ecosystem to give other plotting backends an "Axes"-like object for compatibility. I don't know where that went. A quick Googling doesn't show much. The closest is plotly.tools.mpl_to_plotly, which doesn't really need this restriction, so I think that argument is vain.

  5. albertcthomas commented on Mar 15, 2019

    @albertcthomas
    Contributor

    maybe introduce a function to plot the decision boundary, since it's important and not easy for beginners IMO

    I agree but I also think that showing the users how to plot results such as decision boundaries is one of the goals of the examples.

    If I want a quick first plot of a result, plotting functions are great, especially for complicated plots such as plotting a tree, but I very often like to tweak the plot to my needs and for that reason I prefer to rely on existing examples and modify the code.

  6. NicolasHug commented on Mar 19, 2019

    @NicolasHug
    Member

    Regarding the name of the module, IMO inspect is more versatile than plot:

  7. qinhanmin2014 commented on Mar 19, 2019

    @qinhanmin2014
    Member

    IMO inspect is more versatile than plot

    no strong opinion, will vote +1 for both name. Maybe plot is more straightforward?

    Again I'm interested in creating the new module before 0.21 and move plot_tree and plot_partial_dependence there. Ideally we should also reach consensus on the API (e.g., axes level/ figure level).

  8. NicolasHug commented on Mar 19, 2019

    @NicolasHug
    Member

    Other point in favor of inspect:

    We may want inspect tools that offer plotting as an option. For example, print the characteristics of a tree (number of nodes, leaves, split points, etc.) and optionally plot it with matplotlib.


    I would be in favor of using axes instead of figures, as suggested (sigh, I'll need to change PDPs again). It's easier for us to support and test. It's a little more work for the users, but it also allows for more control.

  9. GaelVaroquaux commented on Mar 19, 2019

    @GaelVaroquaux
    Member
  10. thomasjpfan commented on Mar 19, 2019

    @thomasjpfan
    MemberAuthor

    "inspect" is loaded in Python (it's a module in the standard library). I
    would avoid using the same name.

    I propose model_inspection. It goes well with our model_selection name.

  11. NicolasHug commented on Mar 19, 2019

    @NicolasHug
    Member

    We might want to inspect something that's not a model (encoder, preprocessor, grid search results...)

    inspection then?

  12. jnothman commented on Mar 20, 2019

    @jnothman
    Member
  13. adrinjalali commented on Mar 20, 2019

    @adrinjalali
    Member

    FWIW, I'd also prefer plot to inspect, since it's more intuitive for most users to find it. People are more likely trying to plot their models than inspect their models (when searching on search engines for instance, or looking at the possible autocomplete options on their IDE).

  14. qinhanmin2014 commented on Mar 20, 2019

    @qinhanmin2014
    Member

    Gael's suggestion of a public plot submodule on each subpackage is worth considering.

    If so, where shall we put plot_decision_boundary?

    Regarding #12599, @NicolasHug I doubt whether partial_dependence should be in the new module. (i.e., ensemble.partial_dependence + plot.plot_partial_dependence)

  15. GaelVaroquaux commented on Mar 20, 2019

    @GaelVaroquaux
    Member
  16. 31 remaining items

  17. qinhanmin2014 commented on Apr 3, 2019

    @qinhanmin2014
    Member

    And @amueller we’ll support passing axes, so users can easily adjust the plot after they call the plotting functions?

  18. amueller commented on Apr 3, 2019

    @amueller
    Member

    @qinhanmin2014 no, many things can not easily be changed afterwards, and we don't need to think about all the formatting necessarily ourselves, but we should allow users to plot something again. It will basically happen any time you do a plot. And having to subsample datasets every time I want to do a plot is a bit annoying. And if I later change my mind I will still need to recompute.
    Basically the point is that you can't usually anticipate what exactly you want in your exploratory analysis, and having to recompute everything to change a visualization doesn't seem great.

  19. qinhanmin2014 commented on Apr 4, 2019

    @qinhanmin2014
    Member

    @qinhanmin2014 no, many things can not easily be changed afterwards, and we don't need to think about all the formatting necessarily ourselves, but we should allow users to plot something again. It will basically happen any time you do a plot. And having to subsample datasets every time I want to do a plot is a bit annoying. And if I later change my mind I will still need to recompute.

    Yes, I know it might be useful, but the main issue here is that we don't have a clean way to support this functionality, and I guess most plotting functions do not require much calculation?

  20. jnothman commented on Apr 4, 2019

    @jnothman
    Member
  21. amueller commented on Apr 4, 2019

    @amueller
    Member

    @qinhanmin2014 I don't think "we don't have a good API yet" is a good reason.
    And partial dependence, permutation importance, learning curves, validation curves and results of GridSearchCV and RandomizedSearchCV are all common examples that take a lot of computation. Though for gridsearchcv and randomizedsearchcv the obvious thing would be to pass either the object or the cv_results_, doing the work inside the plotting function in those cases seems nonsensical. I'm not entirely sure about learning curves and validation curves tbh.

  22. amueller commented on Apr 4, 2019

    @amueller
    Member

    @jnothman I think @GaelVaroquaux wanted to keep the matplotlib dependency confined to a module and that was one of the main motivations? I don't really have very coherent thoughts about this yet.

  23. qinhanmin2014 commented on Apr 4, 2019

    @qinhanmin2014
    Member

    And partial dependence, permutation importance, learning curves, validation curves and results of GridSearchCV and RandomizedSearchCV are all common examples that take a lot of computation.

    Thanks, I now realized that I'm wrong :)
    Though I'm still unable to understand why it's important to provide users with a way to plot without recomputing. But if others think so and there's a good way, I'll vote +1.

    I like that we're discussing this with a little more grounding, but tbh I'm
    still not entirely convinced that we even need to have plot in the import
    path at all. After all, we seem to have plot_ as a prefix for the
    functions. The question also relates to plot_tree: why should it be
    separated from other export and textual visualisation code?

    Yes this can also be an option. If so, we can mention that all the functions which start with plot_ requires matplotlib. Another advantage of this option is that we don't need to move existing functions.

  24. thomasjpfan commented on Apr 16, 2019

    @thomasjpfan
    MemberAuthor

    Going over this discussion, I agree with not adding a sklearn.plot module, and use the prefix plot_ to signal a matplotlib requirement.

    For example, in #12599, partial_dependence and plot_partial_dependence will be placed in inspection.

  25. NicolasHug commented on Apr 16, 2019

    @NicolasHug
    Member

    Ok, unless someone disagrees with this in the following days, I'm going to update the PDP PR and:

    • put both partial_dependence and plot_partial_dependence in sklearn.inspection
    • make plot_partial_dependence return a bunch with the fig and ax objects as attributes (right now it returns them in a tuple). This way, we'll be able to keep these 2 functions backward compatible when we implement the second option from Add a sklearn.plot Module #13448 (comment)
  26. qinhanmin2014 commented on Apr 17, 2019

    @qinhanmin2014
    Member

    Can we make the final decision here?
    Proposal agreed by @jnothman , @NicolasHug and me (apologies if I'm wrong): sklearn.XXX.plot_YYY (support from sklearn.XXX import plot_YYY). We'll mention that all the functions which start with plot_ requires matplotlib.
    A major advantage of this proposal is that we don't need to move existing functions.

  27. agramfort commented on Apr 17, 2019

    @agramfort
    Member
  28. jnothman commented on Apr 18, 2019

    @jnothman
    Member
  29. NicolasHug commented on Apr 18, 2019

    @NicolasHug
    Member

    FYI, I'm adding sklearn.utils.check_matplotlib_support in #12599

    def check_matplotlib_support(caller_name):
        try:
            import matplotlib
        except ImportError as e:
            raise ImportError(
                "{} requires matplotlib. You can install matplotlib with "
                "`pip install matplotlib`".format(caller_name)
            ) from e
  30. qinhanmin2014 commented on Apr 18, 2019

    @qinhanmin2014
    Member

    FYI, I'm adding sklearn.utils.check_matplotlib_support in #12599

    That's great! Thanks.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions