Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH Show optional lines in plot_marginal() et al #177

Open
mayer79 opened this issue Aug 19, 2024 · 1 comment
Open

ENH Show optional lines in plot_marginal() et al #177

mayer79 opened this issue Aug 19, 2024 · 1 comment

Comments

@mayer79
Copy link
Collaborator

mayer79 commented Aug 19, 2024

Plots for categorical features show only points, not lines. This is a good default. However, for ordered categoricals (or to stress that a set of points belongs together), it would be nice to show optional lines. On the other hand, it could make sense to optionally hide the lines for numeric features.

Example

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import make_column_transformer
from sklearn.pipeline import make_pipeline
from model_diagnostics.calibration import plot_marginal

fig, axes = plt.subplots(1, 2, figsize=(10, 5))

n = 1000
x_cat_values = (list("abc"), [0, 1, 2])

for i, values in enumerate(x_cat_values):
    rng = np.random.default_rng(0)

    X = pd.DataFrame(
        {
            "x_cat": rng.choice(values, n),
            "x_num": rng.standard_normal(n),
        }
    )
    y = (X.x_cat == values[0]) + 0.5 * X.x_num + rng.standard_normal(n)

    model = make_pipeline(
        make_column_transformer(
            (OneHotEncoder(drop="first", sparse_output=False), ["x_cat"]),
            remainder="passthrough"
        ),
        LinearRegression()
    ).fit(X, y=y)

    plot_marginal(
        y, 
        y_pred=model.predict(X), 
        X=X, 
        feature_name="x_cat", 
        predict_function=model.predict, 
        ax=axes[i],
    )

image

Proposal

Add argument show_lines=None to plots. By default, it would be False for categoricals, and True otherwise.

@lorentzenchr
Copy link
Owner

lorentzenchr commented Aug 21, 2024

Seems like a reasonable extension. What about

  • Name of the argument? show_lines`
  • Default value and possible values?
    I don't like None too much as default, as the meaning is not clear. "auto"?
  • Which plot_... functions are affected?
    plot_marginal, plot_bias

PR welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants