Skip to content

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

@mayer79

Description

@mayer79

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions