Repository navigation
Conversation
There was a problem hiding this comment.
I tried the branch with the following:
Details
from sklearn.inspection import DecisionBoundaryDisplay
from sklearn.naive_bayes import GaussianNB
import numpy as np
import matplotlib.pyplot as plt
X = np.concatenate(
[
np.random.multivariate_normal(mean=[-3, 3], cov=np.diag([2, 2]), size=100),
np.random.multivariate_normal(mean=[-3, -3], cov=np.diag([1.5, 0.5]), size=100),
np.random.multivariate_normal(mean=[3, 0], cov=np.diag([0.5, 1.5]), size=100),
]
)
y = np.concatenate([np.zeros(100), np.ones(100), np.ones(100) * 2])
estimator = GaussianNB(var_smoothing=1e-2).fit(X, y)
cmap = "gist_rainbow"
fig, axs = plt.subplots(2, 2, figsize=(10, 10))
for ax, (xlim, ylim) in zip(
axs.flatten(),
[
(None, None),
((-15, 15), (-15, 15)),
((-4, -1), (1, 4)),
((-1, 2), (-5, -2)),
],
):
disp = DecisionBoundaryDisplay.from_estimator(
estimator,
X,
response_method="predict_proba",
plot_method="pcolormesh",
multiclass_colors=cmap,
alpha=0.8,
xlim=xlim,
ylim=ylim,
ax=ax,
)
_ = disp.ax_.scatter(X[:, 0], X[:, 1], c=y, edgecolor="k", cmap=cmap)
ax.set_title(f"xlim={xlim}, ylim={ylim}")and it yields:
Good:
- it's possible to both zoom out and zoom in as expected;
- the resulting colors are not impacted by zooming in / zooming out. I think this is nice;
- the subsequent scatter plot does not cancel the limits passed to the display.
Not so good:
- we can see that zooming in results in large pcolormesh pixels. I suppose that similar zoom-induced resolution degradation would also happen with other the plot methods.
I am not sure how we could get the best of both words: stable colors across zoom levels while preserving the same resolution at any zoom level (without computing gigabytes of unrendered response values).
Maybe the zoom induced resolution degradation is not such a big deal. If we cannot devise any easy solution to this problem, +1 for merging as it is.
Co-authored-by: Lucy Liu <[email protected]>
Co-authored-by: Lucy Liu <[email protected]>
|
Thank you for the feedback!
Good point, I'll try to come up with one.
I don't see a simple solution right away, but |
Since this is only for purpose of inferring @@ -589,11 +589,19 @@ class DecisionBoundaryDisplay:
elif is_clusterer(estimator) and hasattr(estimator, "labels_"):
n_classes = len(np.unique(estimator.labels_))
else:
- target_type = type_of_target(response)
+ # Use original X (not grid) to infer number of classes/labels, ensuring all
+ # classes/labels are captured even if some fall outside the grid boundaries
+ response_all, _ = _get_response_values(
+ estimator,
+ X,
+ response_method=prediction_method,
+ pos_label=class_of_interest,
+ )
+ target_type = type_of_target(response_all)
if target_type in ("binary", "continuous"):
n_classes = 2
elif target_type == "multiclass":
- n_classes = len(np.unique(response))
+ n_classes = len(np.unique(response_all))
else:
raise ValueError(
"Number of classes or labels cannot be inferred from " |
That sounds reasonable. Especially since we might need to calculate these again anyway for the scatter plot integration planned in #33094 |
|
It turns out to be a bit more complex again: Since we currently just store the number of classes, the colors are not consistent when zooming in, because |
|
Ah, actually, the |
|
In the end it was even easier for the Note: This PR does not address the following cases (which will be addressed in their own PRs): I will still look into adding an explicit test for the color consistency when zooming in. |
|
New can of worms: I don't want to mix these things up, so I think it makes sense to pause this PR until I tackled #33115 and #33094, then I can check if @lucyleeow's suggestion can be integrated more easily to solve the pixel issue. |
Reference Issues/PRs
Towards #27462 (supersedes #31693 )
What does this implement/fix? Explain your changes.
Add
xlimandylimparameters tofrom_estimatorandplotmethods to allow setting the boundaries for the plot. Also added corresponding tests.See the discussion in #31693 for the reason of extending the grid first (instead of simply overwriting the boundaries).
@lucyleeow @ogrisel @StefanieSenger
AI usage disclosure
I used AI assistance for:
Any other comments?