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

add notebook on the cause of miscalibration #7

Merged
merged 30 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
pixi run check-generated-predictions
pixi run build-calibration-curve
pixi run different-calibration-curves
pixi run causes-miscalibration

- name: Test if we can build the documentation
if: matrix.os == 'ubuntu-latest'
Expand Down
1 change: 1 addition & 0 deletions book/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ root: intro
chapters:
- file: content/notebooks/build_calibration_curve
- file: content/notebooks/different_calibration_curves
- file: content/notebooks/causes_miscalibration
61 changes: 61 additions & 0 deletions content/python_files/causes_miscalibration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# %%
import numpy as np


def xor_generator(n_samples=1_000, seed=0):
rng = np.random.default_rng(seed)
X = rng.uniform(low=-3, high=3, size=(n_samples, 2))
y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0)
return X, y
ogrisel marked this conversation as resolved.
Show resolved Hide resolved


# %%
import matplotlib.pyplot as plt

X, y = xor_generator(seed=0)
_, ax = plt.subplots()
ax.scatter(*X.T, c=y, cmap="coolwarm", alpha=0.5)
ax.set(
xlim=(-3, 3),
ylim=(-3, 3),
xlabel="Feature 1",
ylabel="Feature 2",
title="XOR problem",
aspect="equal",
)

# %%
from sklearn.preprocessing import SplineTransformer, PolynomialFeatures
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline

model = make_pipeline(
SplineTransformer(n_knots=20),
PolynomialFeatures(degree=2, interaction_only=True),
LogisticRegression(),
)
model.fit(X, y)
glemaitre marked this conversation as resolved.
Show resolved Hide resolved

# %%
from sklearn.inspection import DecisionBoundaryDisplay

_, ax = plt.subplots()
DecisionBoundaryDisplay.from_estimator(
model, X, ax=ax, cmap="coolwarm", response_method="predict_proba"
)
ax.scatter(*X.T, c=y, cmap="coolwarm", alpha=0.5)
ax.set(
xlim=(-3, 3),
ylim=(-3, 3),
xlabel="Feature 1",
ylabel="Feature 2",
title="XOR problem",
aspect="equal",
)

# %%
from sklearn.calibration import CalibrationDisplay

CalibrationDisplay.from_estimator(model, X, y, strategy="quantile", n_bins=10)

# %%
2 changes: 2 additions & 0 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ generate-predictions = { cmd = "python _generate_predictions.py", cwd = "content
build-calibration-curve = { cmd = "ipython build_calibration_curve.py", cwd = "content/python_files" }
check-generated-predictions = { cmd = "python check_generated_predictions.py", cwd = "tests" }
different-calibration-curves = { cmd = "ipython different_calibration_curves.py", cwd = "content/python_files" }
causes-miscalibration = { cmd = "ipython causes_miscalibration.py", cwd = "content/python_files" }


[dependencies]
jupyterlab = ">=4.2.4,<5"
Expand Down