Sklearn metrics roc_auc_score gives the wrong result when using anaconda environment
See original GitHub issueDescribe the bug
Same input, Same machine, but roc_auc_score gives different results.
Steps/Code to Reproduce
import numpy as np
from sklearn.metrics import roc_auc_score
X = np.array([[1., 1., 0., 1., 0., 0., 1., 0., 0., 0., 0.],
[1., 1. ,0., 1., 0., 0., 0., 0., 1., 0., 0.],
[1.,1. ,0. ,1. ,0. ,0. ,0. ,0. ,1. ,0. ,0.],
[1., 1. ,0. ,0. ,0. ,1. ,0., 0. ,1. ,0. ,0.],
[1., 1. ,0. ,0. ,0. ,1. ,0. ,1. ,0. ,0. ,0.],
[1., 1. ,1. ,0. ,0. ,0. ,0. ,0. ,1. ,0. ,0.],
[1., 1. ,0. ,0. ,1. ,0. ,0. ,1. ,0. ,0. ,1.],
[1., 1. ,0. ,0. ,0. ,1. ,0. ,1. ,0. ,0. ,0.],
[1., 1. ,0. ,0. ,1. ,0. ,1. ,0. ,0. ,0. ,1.],
[1., 1. ,0. ,1. ,0. ,0. ,1. ,0. ,0. ,0. ,0.],
[1., 1. ,0. ,0. ,1. ,0. ,0. ,1. ,0. ,0. ,0.]])
w = np.array([1,1,0.8,0.6,0.6,0.6,0.8,0.8,0.4,0.4,0])
y_pred = X.dot(w)
print('y_pred =', y_pred)
y_binary = np.array([ 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0])
score1 = roc_auc_score(y_binary, y_pred)
y_copy = np.array([3.4, 3., 3., 3., 3.4, 3.2, 3.4, 3.4, 3.4, 3.4, 3.4]) #(copy directly the output from y_pred)
score2 = roc_auc_score(y_binary, y_copy)
print('score1 =', score1)
print('score2 =', score2)
Expected Results
Expect score1 and score2 are the same, and equal 0.75
Actual Results
y_pred = [3.4 3. 3. 3. 3.4 3.2 3.4 3.4 3.4 3.4 3.4]
score1 = 0.5833333333333333
score2 = 0.75
Versions
System: python: 3.8.3 (default, Jul 2 2020, 16:21:59) [GCC 7.3.0] executable: /home/hoatrinh/anaconda3/bin/python machine: Linux-4.15.0-128-generic-x86_64-with-glibc2.10
Python dependencies: pip: 20.1.1 setuptools: 49.2.0.post20200714 sklearn: 0.23.1 numpy: 1.18.5 scipy: 1.4.1 Cython: 0.29.21 pandas: 1.0.5 matplotlib: 3.2.2 joblib: 0.16.0 threadpoolctl: 2.1.0
Built with OpenMP: True Linux-4.15.0-128-generic-x86_64-with-glibc2.10 Python 3.8.3 (default, Jul 2 2020, 16:21:59) [GCC 7.3.0] NumPy 1.18.5 SciPy 1.4.1 Scikit-Learn 0.23.1 Imbalanced-Learn 0.7.0
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top GitHub Comments
This is indeed an issue with floating point:
If you store the thresholds value of the former case, you will observe the following:
So you have some floating-point error. So the error is introduced by
X.dot(w)
and not the computation of the ROC AUC. I am not totally sure what could introduce that the floating-point error could be different. One reason could be that NumPy was not built with the same compiler maybe.Closing since this not related to scikit-learn