Skip to content

Add calibration curve to plotting module #8425

Closed

Description

@amueller

I think it would be nice to have something like

def plot_calibration_curve(y_true, prob_y_pred, n_bins=5, ax=None):
    prop_true, prop_pred = calibration_curve(y_true, prob_y_pred, n_bins=n_bins)
    if ax is None:
        ax = plt.gca()
    ax.plot([0, 1], [0, 1], ':', c='k')
    ax.plot(prop_pred, prop_true)

    ax.set_xlabel("predicted probability")
    ax.set_ylabel("fraction of positive samples")
    ax.set_aspect("equal")

I got confused for a bit because the order of the return arguments is not the order of the arguments to plot. I think a plot function even just labeling the axes right would decrease mental effort (yes, I'm lazy).

Activity

  1. added
    Easy
    on Feb 21, 2017
  2. amueller commented on Feb 21, 2017

    @amueller
    MemberAuthor

    should plot functions return the artist? or the axes? hm...

  3. amueller commented on Feb 21, 2017

    @amueller
    MemberAuthor
    def plot_calibration_curve(y_true, y_prob, n_bins=5, ax=None, hist=True, normalize=False):
        prob_true, prob_pred = calibration_curve(y_true, y_prob, n_bins=n_bins, normalize=normalize)
        if ax is None:
            ax = plt.gca()
        if hist:
            if normalize:
                y_prob = (y_prob - y_prob.min()) / (y_prob.max() - y_prob.min())
            ax.hist(y_prob, weights=np.ones_like(y_prob) / len(y_prob), alpha=.4,
                   bins=np.maximum(10, n_bins))
        ax.plot([0, 1], [0, 1], ':', c='k')
        curve = ax.plot(prob_pred, prob_true, marker="o")
    
        ax.set_xlabel("predicted probability")
        ax.set_ylabel("fraction of positive samples")
    
        ax.set(aspect='equal')
        return curve

    wow this took me an hour... and I'm not friends with aspect='equal' any more

  4. rishikksh20 commented on Feb 22, 2017

    @rishikksh20
    Contributor
  5. lesteve commented on Feb 22, 2017

    @lesteve
    Member

    should plot functions return the artist? or the axes? hm...

    I wasn't aware of this until recently but this is the recommended signature from http://matplotlib.org/faq/usage_faq.html#coding-styles:

    def my_plotter(ax, data1, data2, param_dict):
        """
        A helper function to make a graph
    
        Parameters
        ----------
        ax : Axes
            The axes to draw to
    
        data1 : array
           The x data
    
        data2 : array
           The y data
    
        param_dict : dict
           Dictionary of kwargs to pass to ax.plot
    
        Returns
        -------
        out : list
            list of artists added
        """
        out = ax.plot(data1, data2, **param_dict)
        return out

    I guess this is close enough to what you are doing in #8425 (comment).

  6. amueller commented on Feb 22, 2017

    @amueller
    MemberAuthor

    There is (or will be if none of the PRs have been merge yet) sklearn.plot

  7. rishikksh20 commented on Feb 22, 2017

    @rishikksh20
    Contributor

    @amueller May be it's will be because I haven't found plot directory inside sklearn.

  8. amueller commented on Mar 3, 2017

    @amueller
    MemberAuthor

    hm yeah I'm behind on everything :-/

  9. jeff3456 commented on Mar 22, 2017

    @jeff3456

    Hi I am new to contributing to scikit-learn but I would like to work on this! Is anyone else working on this?

  10. jnothman commented on Mar 22, 2017

    @jnothman
    Member

    I don't think a PR has been opened, though @rishikksh20 has clearly expressed some interest

  11. rishikksh20 commented on Mar 23, 2017

    @rishikksh20
    Contributor

    Actually I like to work on this but till now sklearn plot module haven't committed I guess.

  12. jnothman commented on Mar 23, 2017

    @jnothman
    Member
  13. added and removed
    Easy
    on Apr 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions