I'm using sklearn roc_auc_score to evaluate a model from PubChem where the label is a string 'Active' or 'Inactive' and I keep ending up with a ValueError when it tries to convert the string to a float.
I'm calling the method
sklearn.metrics._ranking.roc_auc_score(y_true, y_score, ...)
both y_true and y_score are ndarray like ['Active', 'Inactive', 'Active' ...]
the dtype for both arrays is 'object', because I load them with a pandas dataframe, and convert columns (Series) with to_numpy. (I tried dtype as 'str', but it gets treated as object anyway)
And I get
ValueError: could not convert string to float: 'Active'
Looking into the roc_auc_score method I see what's happening: It first makes these 2 calls to prepare the input arrays:
y_true = check_array(y_true, ensure_2d=False, dtype=None)
y_score = check_array(y_score, ensure_2d=False)
Note that the first call passes in dtype=None. This is the only reason it succeeds where the 2nd call fails. The default for dtype is "numeric" (a string).
Now, when check_array finds the dtype of the original array to be 'object' and the dtype passed in is 'numeric', it tries to convert the object values into floats. Hence the exception: 'Active' cannot be converted to float.
So the first check_array works fine, but the 2nd check_array fails.
My question under all of this: how does roc_auc_score ever deal with binary classifications that are strings? What am I doing wrong?


