Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chance_level_kw in RocCurveDisplay raises an error when using valid matplotlib args #30015

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

Closed
JosephBARBIERDARNAL opened this issue Oct 6, 2024 · 3 comments Β· Fixed by #30023
Closed
Labels

Comments

@JosephBARBIERDARNAL
Copy link
Contributor

JosephBARBIERDARNAL commented Oct 6, 2024

Describe the bug

When passing additional keyword arguments to the random classifier's line via the chance_level_kw argument, some arguments raise an error even though they are valid matplotlib.pyplot.plot() arguments. The error occurs with the c and ls arguments.

The reason is that in scikit-learn/sklearn/metrics/_plot/roc_curve.py, the following code exists:

chance_level_line_kw = {
    "label": "Chance level (AUC = 0.5)",
    "color": "k",
    "linestyle": "--",
}

if chance_level_kw is not None:
    chance_level_line_kw.update(**chance_level_kw)

Matplotlib raises an error when both color and c, or linestyle and ls are specified (this happens with other arguments too, but these are not relevant here since scikit-learn does not set values for them).

This behavior may also occur with other future classes, especially CapCurveDisplay (in development #28972).

A quick fix might look like this:

if 'ls' in chance_level_kw:
    chance_level_kw['linestyle'] = chance_level_kw['ls']
    del chance_level_kw['ls']

if 'c' in chance_level_kw:
    chance_level_kw['color'] = chance_level_kw['c']
    del chance_level_kw['c']

chance_level_line_kw = {
    "label": "Chance level (AUC = 0.5)",
    "color": "k",
    "linestyle": "--",
}

if chance_level_kw is not None:
    chance_level_line_kw.update(**chance_level_kw)

Steps/Code to Reproduce

from sklearn import metrics

display = metrics.RocCurveDisplay.from_predictions(
    y_true=[0, 0, 1, 1],
    y_pred=[0.1, 0.4, 0.35, 0.8],
    plot_chance_level=True,
    chance_level_kw={'ls': '--'}
)

Expected Results

Screenshot 2024-10-06 at 15 18 34

Actual Results

TypeError: Got both 'linestyle' and 'ls', which are aliases of one another

Versions

System:
    python: 3.12.5 (main, Aug  6 2024, 19:08:49) [Clang 15.0.0 (clang-1500.3.9.4)]
executable: /Users/josephbarbier/Desktop/scikit-learn/sklearn-env/bin/python
   machine: macOS-14.6.1-arm64-arm-64bit

Python dependencies:
      sklearn: 1.6.dev0
          pip: 24.2
   setuptools: 69.5.1
        numpy: 2.1.2
        scipy: 1.14.1
       Cython: 3.0.11
       pandas: None
   matplotlib: 3.9.2
       joblib: 1.4.2
threadpoolctl: 3.5.0

Built with OpenMP: True

threadpoolctl info:
       user_api: openmp
   internal_api: openmp
    num_threads: 10
         prefix: libomp
       filepath: /opt/homebrew/Cellar/libomp/18.1.8/lib/libomp.dylib
        version: None
@JosephBARBIERDARNAL JosephBARBIERDARNAL added Bug Needs Triage Issue requires triage labels Oct 6, 2024
@glemaitre glemaitre removed the Needs Triage Issue requires triage label Oct 7, 2024
@glemaitre
Copy link
Member

Indeed this is a bug.

We should at least the support the alias for the default style meaning only. This should not make the code too much cumbersome.

@JosephBARBIERDARNAL
Copy link
Contributor Author

I am willing to open a PR for this, as well as correct the problem in #28972 . Is it preferable for this to be a separate PR?

@glemaitre
Copy link
Member

Yep a separate PR would be nice. I think that we can solve the bug in the different display that we have thought. Not all will have such default style so we should have only a subset.

@JosephBARBIERDARNAL Feel free to ping me in the PR for review.

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