Talip7/scikit-learn-issues-multilabel huggingface dataset:
Potentially unsafe cast in sklearn.utils.multiclass.type_of_target

### Discussed in https://github.com/scikit-learn/scikit-learn/discussions/25835

<div type='discussions-op-text'>

<sup>Originally posted by **thomasryck** March 13, 2023</sup>
Hi

I was using the method type_of_target from the multiclass file.
And I had some warnings because of the following [line](https://github.com/scikit-learn/scikit-learn/blob/e305e800a419e766a312174159e15b071c3fe994/sklearn/utils/multiclass.py#L380).
Indeed as there is a cast into int32, and I have some int64 and float64 arrays, the cast is not safe.
I was asking myself if it could be a could idea to modify the test and to test by taking the floor of the array in place of doing a cast.
I do not know if it is really better, for now I just think that it could avoid not safe cast (that are not risky in that case).
And I do not know what is the best regarding time and memory.</div>

COMMENT:
Copying my comment from the discussion ...

I guess it could be possible to adapt this code indeed. It seems like we could put the logic we want in the `_is_integral_float` function and reuse `_is_integral_float` consistently:
https://github.com/scikit-learn/scikit-learn/blob/e305e800a419e766a312174159e15b071c3fe994/sklearn/utils/multiclass.py#L122-L123

Googling a bit, it seems like `arr % 1 == 0` might be the best option. Doing some quick benchmarks would potentially help decide whether it is better than `np.floor(arr) == arr`.

Comment: the cast is not safe even from `float64` to `int64`, on my machine `np.dtype(int)` is `int64`

Labels: Bug
Potentially unsafe cast in sklearn.utils.multiclass.type_of_target

### Discussed in https://github.com/scikit-learn/scikit-learn/discussions/25835

<div type='discussions-op-text'>

<sup>Originally posted by **thomasryck** March 13, 2023</sup>
Hi

I was using the method type_of_target from the multiclass file.
And I had some warnings because of the following [line](https://github.com/scikit-learn/scikit-learn/blob/e305e800a419e766a312174159e15b071c3fe994/sklearn/utils/multiclass.py#L380).
Indeed as there is a cast into int32, and I have some int64 and float64 arrays, the cast is not safe.
I was asking myself if it could be a could idea to modify the test and to test by taking the floor of the array in place of doing a cast.
I do not know if it is really better, for now I just think that it could avoid not safe cast (that are not risky in that case).
And I do not know what is the best regarding time and memory.</div>

COMMENT:
I will try to run some benchmarks next week.

Labels: Bug
Potentially unsafe cast in sklearn.utils.multiclass.type_of_target

### Discussed in https://github.com/scikit-learn/scikit-learn/discussions/25835

<div type='discussions-op-text'>

<sup>Originally posted by **thomasryck** March 13, 2023</sup>
Hi

I was using the method type_of_target from the multiclass file.
And I had some warnings because of the following [line](https://github.com/scikit-learn/scikit-learn/blob/e305e800a419e766a312174159e15b071c3fe994/sklearn/utils/multiclass.py#L380).
Indeed as there is a cast into int32, and I have some int64 and float64 arrays, the cast is not safe.
I was asking myself if it could be a could idea to modify the test and to test by taking the floor of the array in place of doing a cast.
I do not know if it is really better, for now I just think that it could avoid not safe cast (that are not risky in that case).
And I do not know what is the best regarding time and memory.</div>

COMMENT:
Can be close seems fixed in 1.3.0

Labels: Bug
Potentially unsafe cast in sklearn.utils.multiclass.type_of_target

### Discussed in https://github.com/scikit-learn/scikit-learn/discussions/25835

<div type='discussions-op-text'>

<sup>Originally posted by **thomasryck** March 13, 2023</sup>
Hi

I was using the method type_of_target from the multiclass file.
And I had some warnings because of the following [line](https://github.com/scikit-learn/scikit-learn/blob/e305e800a419e766a312174159e15b071c3fe994/sklearn/utils/multiclass.py#L380).
Indeed as there is a cast into int32, and I have some int64 and float64 arrays, the cast is not safe.
I was asking myself if it could be a could idea to modify the test and to test by taking the floor of the array in place of doing a cast.
I do not know if it is really better, for now I just think that it could avoid not safe cast (that are not risky in that case).
And I do not know what is the best regarding time and memory.</div>

COMMENT:
OK, thanks for following up, let's close this one!

To be honest, I don't think the scikit-learn code has changed and I am wondering if this due to numpy changes.

In any case, feel free to reopen and to add a snippet reproducing the issue if you bump into this again.

Labels: Bug
Visual improvements for ROC and precision-recall plots

### Describe the workflow you want to enable

Hello,

I would like to suggest the following improvements:

- [x] (1) Add x and y axis limit: [0, 1], in sklearn axes currently start at ~-0.1 (#26366)
- [x] (2) Modify the plotting frame: either remove the top and right lines to see the curve better when values are close to 1, or plot the frame with a dotted line (#26367)
- [x] (3). Fix aspect ratio to squared, since the two axes are the same scale. (#26366)
- [x] (4). Add title (not planned)

### Describe your proposed solution

Hello,

While working on ROC and precision-recall plots, I came up with the following suggestions:

1. Add x and y axis limit: [0, 1], in sklearn axes currently start at ~-0.1
2. Modify the plotting frame: either remove the top and right lines to see the curve better when values are close to 1, or plot the frame with a dotted line
3. Fix aspect ratio to squared, since the two axes are the same scale.
4. Add title

```
        ax.set_xlim((-0.01, 1.01))
        ax.set_ylim((-0.01, 1.01))
        for s in ['right', 'left', 'top', 'bottom']:
            ax.spines[s].set_visible(False)
        ax.axvline(1, color='black', linewidth=0.5, ls='dotted')
        ax.axhline(1, color='black', linewidth=0.5, ls='dotted')
        ax.axvline(0, color='black', linewidth=0.5, ls='dotted')
        ax.axhline(0, color='black', linewidth=0.5, ls='dotted')
        ax.set_aspect(1)
        ax.set_title(f'Precision-Recall Curve')
```

### Example 1 Edge case
#### PR
Before:
![image](https://user-images.githubusercontent.com/1184396/226746221-40b40d56-74a4-49c1-82aa-4a3f3366abe2.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746245-3e0a0ffd-28f0-4851-8a41-04629750bd2c.png)

#### ROC
Before:
![image](https://user-images.githubusercontent.com/1184396/226746270-ecc2a1ce-48dc-4682-8acd-86a4d06dfcf0.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746289-8ba8c906-f8f8-4d74-9269-b12c16d339fa.png)

### Example 2 - Edge case

#### PR
Before:
![image](https://user-images.githubusercontent.com/1184396/226745897-de69cf76-f520-499f-b8b6-4e9ede9e7ba2.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226745936-5fc34fff-eff3-4a70-8b99-b7c731984723.png)

#### ROC
Before:
![image](https://user-images.githubusercontent.com/1184396/226745998-d1788d4a-fd0a-4293-bef5-170a71c0a098.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746031-e93dff40-2487-4776-a5b8-baeef9a36e5d.png)



### Describe alternatives you've considered, if relevant

_No response_

### Additional context

_No response_

COMMENT:
> This is something that we provided [here](https://inria.github.io/scikit-learn-mooc/python_scripts/metrics_classification.html) using a `DummyClassifier`.

Instead of internally calling a `DummyClassifier`, it suffices to draw a constant line equals to the prevalence of the positive label, as shown in said notebook.

Labels: Enhancement
Visual improvements for ROC and precision-recall plots

### Describe the workflow you want to enable

Hello,

I would like to suggest the following improvements:

- [x] (1) Add x and y axis limit: [0, 1], in sklearn axes currently start at ~-0.1 (#26366)
- [x] (2) Modify the plotting frame: either remove the top and right lines to see the curve better when values are close to 1, or plot the frame with a dotted line (#26367)
- [x] (3). Fix aspect ratio to squared, since the two axes are the same scale. (#26366)
- [x] (4). Add title (not planned)

### Describe your proposed solution

Hello,

While working on ROC and precision-recall plots, I came up with the following suggestions:

1. Add x and y axis limit: [0, 1], in sklearn axes currently start at ~-0.1
2. Modify the plotting frame: either remove the top and right lines to see the curve better when values are close to 1, or plot the frame with a dotted line
3. Fix aspect ratio to squared, since the two axes are the same scale.
4. Add title

```
        ax.set_xlim((-0.01, 1.01))
        ax.set_ylim((-0.01, 1.01))
        for s in ['right', 'left', 'top', 'bottom']:
            ax.spines[s].set_visible(False)
        ax.axvline(1, color='black', linewidth=0.5, ls='dotted')
        ax.axhline(1, color='black', linewidth=0.5, ls='dotted')
        ax.axvline(0, color='black', linewidth=0.5, ls='dotted')
        ax.axhline(0, color='black', linewidth=0.5, ls='dotted')
        ax.set_aspect(1)
        ax.set_title(f'Precision-Recall Curve')
```

### Example 1 Edge case
#### PR
Before:
![image](https://user-images.githubusercontent.com/1184396/226746221-40b40d56-74a4-49c1-82aa-4a3f3366abe2.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746245-3e0a0ffd-28f0-4851-8a41-04629750bd2c.png)

#### ROC
Before:
![image](https://user-images.githubusercontent.com/1184396/226746270-ecc2a1ce-48dc-4682-8acd-86a4d06dfcf0.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746289-8ba8c906-f8f8-4d74-9269-b12c16d339fa.png)

### Example 2 - Edge case

#### PR
Before:
![image](https://user-images.githubusercontent.com/1184396/226745897-de69cf76-f520-499f-b8b6-4e9ede9e7ba2.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226745936-5fc34fff-eff3-4a70-8b99-b7c731984723.png)

#### ROC
Before:
![image](https://user-images.githubusercontent.com/1184396/226745998-d1788d4a-fd0a-4293-bef5-170a71c0a098.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746031-e93dff40-2487-4776-a5b8-baeef9a36e5d.png)



### Describe alternatives you've considered, if relevant

_No response_

### Additional context

_No response_

COMMENT:
Please open a PR per point. As mentioned the change should be done for the PR curve and the ROC curve.

Labels: Enhancement
Visual improvements for ROC and precision-recall plots

### Describe the workflow you want to enable

Hello,

I would like to suggest the following improvements:

- [x] (1) Add x and y axis limit: [0, 1], in sklearn axes currently start at ~-0.1 (#26366)
- [x] (2) Modify the plotting frame: either remove the top and right lines to see the curve better when values are close to 1, or plot the frame with a dotted line (#26367)
- [x] (3). Fix aspect ratio to squared, since the two axes are the same scale. (#26366)
- [x] (4). Add title (not planned)

### Describe your proposed solution

Hello,

While working on ROC and precision-recall plots, I came up with the following suggestions:

1. Add x and y axis limit: [0, 1], in sklearn axes currently start at ~-0.1
2. Modify the plotting frame: either remove the top and right lines to see the curve better when values are close to 1, or plot the frame with a dotted line
3. Fix aspect ratio to squared, since the two axes are the same scale.
4. Add title

```
        ax.set_xlim((-0.01, 1.01))
        ax.set_ylim((-0.01, 1.01))
        for s in ['right', 'left', 'top', 'bottom']:
            ax.spines[s].set_visible(False)
        ax.axvline(1, color='black', linewidth=0.5, ls='dotted')
        ax.axhline(1, color='black', linewidth=0.5, ls='dotted')
        ax.axvline(0, color='black', linewidth=0.5, ls='dotted')
        ax.axhline(0, color='black', linewidth=0.5, ls='dotted')
        ax.set_aspect(1)
        ax.set_title(f'Precision-Recall Curve')
```

### Example 1 Edge case
#### PR
Before:
![image](https://user-images.githubusercontent.com/1184396/226746221-40b40d56-74a4-49c1-82aa-4a3f3366abe2.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746245-3e0a0ffd-28f0-4851-8a41-04629750bd2c.png)

#### ROC
Before:
![image](https://user-images.githubusercontent.com/1184396/226746270-ecc2a1ce-48dc-4682-8acd-86a4d06dfcf0.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746289-8ba8c906-f8f8-4d74-9269-b12c16d339fa.png)

### Example 2 - Edge case

#### PR
Before:
![image](https://user-images.githubusercontent.com/1184396/226745897-de69cf76-f520-499f-b8b6-4e9ede9e7ba2.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226745936-5fc34fff-eff3-4a70-8b99-b7c731984723.png)

#### ROC
Before:
![image](https://user-images.githubusercontent.com/1184396/226745998-d1788d4a-fd0a-4293-bef5-170a71c0a098.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746031-e93dff40-2487-4776-a5b8-baeef9a36e5d.png)



### Describe alternatives you've considered, if relevant

_No response_

### Additional context

_No response_

COMMENT:
Okay will do that soon.

Labels: Enhancement
Visual improvements for ROC and precision-recall plots

### Describe the workflow you want to enable

Hello,

I would like to suggest the following improvements:

- [x] (1) Add x and y axis limit: [0, 1], in sklearn axes currently start at ~-0.1 (#26366)
- [x] (2) Modify the plotting frame: either remove the top and right lines to see the curve better when values are close to 1, or plot the frame with a dotted line (#26367)
- [x] (3). Fix aspect ratio to squared, since the two axes are the same scale. (#26366)
- [x] (4). Add title (not planned)

### Describe your proposed solution

Hello,

While working on ROC and precision-recall plots, I came up with the following suggestions:

1. Add x and y axis limit: [0, 1], in sklearn axes currently start at ~-0.1
2. Modify the plotting frame: either remove the top and right lines to see the curve better when values are close to 1, or plot the frame with a dotted line
3. Fix aspect ratio to squared, since the two axes are the same scale.
4. Add title

```
        ax.set_xlim((-0.01, 1.01))
        ax.set_ylim((-0.01, 1.01))
        for s in ['right', 'left', 'top', 'bottom']:
            ax.spines[s].set_visible(False)
        ax.axvline(1, color='black', linewidth=0.5, ls='dotted')
        ax.axhline(1, color='black', linewidth=0.5, ls='dotted')
        ax.axvline(0, color='black', linewidth=0.5, ls='dotted')
        ax.axhline(0, color='black', linewidth=0.5, ls='dotted')
        ax.set_aspect(1)
        ax.set_title(f'Precision-Recall Curve')
```

### Example 1 Edge case
#### PR
Before:
![image](https://user-images.githubusercontent.com/1184396/226746221-40b40d56-74a4-49c1-82aa-4a3f3366abe2.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746245-3e0a0ffd-28f0-4851-8a41-04629750bd2c.png)

#### ROC
Before:
![image](https://user-images.githubusercontent.com/1184396/226746270-ecc2a1ce-48dc-4682-8acd-86a4d06dfcf0.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746289-8ba8c906-f8f8-4d74-9269-b12c16d339fa.png)

### Example 2 - Edge case

#### PR
Before:
![image](https://user-images.githubusercontent.com/1184396/226745897-de69cf76-f520-499f-b8b6-4e9ede9e7ba2.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226745936-5fc34fff-eff3-4a70-8b99-b7c731984723.png)

#### ROC
Before:
![image](https://user-images.githubusercontent.com/1184396/226745998-d1788d4a-fd0a-4293-bef5-170a71c0a098.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746031-e93dff40-2487-4776-a5b8-baeef9a36e5d.png)



### Describe alternatives you've considered, if relevant

_No response_

### Additional context

_No response_

COMMENT:
/take

Labels: Enhancement
Visual improvements for ROC and precision-recall plots

### Describe the workflow you want to enable

Hello,

I would like to suggest the following improvements:

- [x] (1) Add x and y axis limit: [0, 1], in sklearn axes currently start at ~-0.1 (#26366)
- [x] (2) Modify the plotting frame: either remove the top and right lines to see the curve better when values are close to 1, or plot the frame with a dotted line (#26367)
- [x] (3). Fix aspect ratio to squared, since the two axes are the same scale. (#26366)
- [x] (4). Add title (not planned)

### Describe your proposed solution

Hello,

While working on ROC and precision-recall plots, I came up with the following suggestions:

1. Add x and y axis limit: [0, 1], in sklearn axes currently start at ~-0.1
2. Modify the plotting frame: either remove the top and right lines to see the curve better when values are close to 1, or plot the frame with a dotted line
3. Fix aspect ratio to squared, since the two axes are the same scale.
4. Add title

```
        ax.set_xlim((-0.01, 1.01))
        ax.set_ylim((-0.01, 1.01))
        for s in ['right', 'left', 'top', 'bottom']:
            ax.spines[s].set_visible(False)
        ax.axvline(1, color='black', linewidth=0.5, ls='dotted')
        ax.axhline(1, color='black', linewidth=0.5, ls='dotted')
        ax.axvline(0, color='black', linewidth=0.5, ls='dotted')
        ax.axhline(0, color='black', linewidth=0.5, ls='dotted')
        ax.set_aspect(1)
        ax.set_title(f'Precision-Recall Curve')
```

### Example 1 Edge case
#### PR
Before:
![image](https://user-images.githubusercontent.com/1184396/226746221-40b40d56-74a4-49c1-82aa-4a3f3366abe2.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746245-3e0a0ffd-28f0-4851-8a41-04629750bd2c.png)

#### ROC
Before:
![image](https://user-images.githubusercontent.com/1184396/226746270-ecc2a1ce-48dc-4682-8acd-86a4d06dfcf0.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746289-8ba8c906-f8f8-4d74-9269-b12c16d339fa.png)

### Example 2 - Edge case

#### PR
Before:
![image](https://user-images.githubusercontent.com/1184396/226745897-de69cf76-f520-499f-b8b6-4e9ede9e7ba2.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226745936-5fc34fff-eff3-4a70-8b99-b7c731984723.png)

#### ROC
Before:
![image](https://user-images.githubusercontent.com/1184396/226745998-d1788d4a-fd0a-4293-bef5-170a71c0a098.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746031-e93dff40-2487-4776-a5b8-baeef9a36e5d.png)



### Describe alternatives you've considered, if relevant

_No response_

### Additional context

_No response_

COMMENT:
@stephanecollot Sure, thank you very much. But I may directly modify the styles of the original frame instead of making it invisible and draw a new frame.

Labels: Enhancement
Visual improvements for ROC and precision-recall plots

### Describe the workflow you want to enable

Hello,

I would like to suggest the following improvements:

- [x] (1) Add x and y axis limit: [0, 1], in sklearn axes currently start at ~-0.1 (#26366)
- [x] (2) Modify the plotting frame: either remove the top and right lines to see the curve better when values are close to 1, or plot the frame with a dotted line (#26367)
- [x] (3). Fix aspect ratio to squared, since the two axes are the same scale. (#26366)
- [x] (4). Add title (not planned)

### Describe your proposed solution

Hello,

While working on ROC and precision-recall plots, I came up with the following suggestions:

1. Add x and y axis limit: [0, 1], in sklearn axes currently start at ~-0.1
2. Modify the plotting frame: either remove the top and right lines to see the curve better when values are close to 1, or plot the frame with a dotted line
3. Fix aspect ratio to squared, since the two axes are the same scale.
4. Add title

```
        ax.set_xlim((-0.01, 1.01))
        ax.set_ylim((-0.01, 1.01))
        for s in ['right', 'left', 'top', 'bottom']:
            ax.spines[s].set_visible(False)
        ax.axvline(1, color='black', linewidth=0.5, ls='dotted')
        ax.axhline(1, color='black', linewidth=0.5, ls='dotted')
        ax.axvline(0, color='black', linewidth=0.5, ls='dotted')
        ax.axhline(0, color='black', linewidth=0.5, ls='dotted')
        ax.set_aspect(1)
        ax.set_title(f'Precision-Recall Curve')
```

### Example 1 Edge case
#### PR
Before:
![image](https://user-images.githubusercontent.com/1184396/226746221-40b40d56-74a4-49c1-82aa-4a3f3366abe2.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746245-3e0a0ffd-28f0-4851-8a41-04629750bd2c.png)

#### ROC
Before:
![image](https://user-images.githubusercontent.com/1184396/226746270-ecc2a1ce-48dc-4682-8acd-86a4d06dfcf0.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746289-8ba8c906-f8f8-4d74-9269-b12c16d339fa.png)

### Example 2 - Edge case

#### PR
Before:
![image](https://user-images.githubusercontent.com/1184396/226745897-de69cf76-f520-499f-b8b6-4e9ede9e7ba2.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226745936-5fc34fff-eff3-4a70-8b99-b7c731984723.png)

#### ROC
Before:
![image](https://user-images.githubusercontent.com/1184396/226745998-d1788d4a-fd0a-4293-bef5-170a71c0a098.png)

After:
![image](https://user-images.githubusercontent.com/1184396/226746031-e93dff40-2487-4776-a5b8-baeef9a36e5d.png)



### Describe alternatives you've considered, if relevant

_No response_

### Additional context

_No response_

COMMENT:
making a PR for number 3 - Fix aspect ratio to squared, since the two axes are the same scale.

Labels: Enhancement