Search code examples
scikit-learnreshaperoc

dtype='numeric' is not compatible with arrays of bytes/strings.Convert your data to numeric values explicitly instead


I have these data I want to use for a logistic regression problem. shape of the data:

((108, 2),##train input
 (108,),##train output
 (35, 2), ##val input
 (35,),##val output
 (28, 2),##test input
 (28,),##test output
 (171, 3), ## all data
 

I did this:

'''
X = X_train.reshape(-2,2)
y = y_train.reshape(-1,1)
model_lr = LogisticRegression()
res = model_lr.fit(X,y)
X_test = np.array(X_test,dtype = float)
test = X_test.reshape(-2,2)
test = np.array(test,dtype = float)
pred = model_lr.predict(test)
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve

output_test = y_test.reshape(-1,1)
output_test = np.array(output_test,dtype = float)


logit_roc_auc = roc_auc_score(output_test, model_lr.predict(test))

'''

and I have this error message:

logit_roc_auc = roc_auc_score(output_test, model_lr.predict(test))
ValueError: dtype='numeric' is not compatible with arrays of bytes/strings.Convert your data to numeric values explicitly instead.

can anybody help? thanks

I tried reshaping the output variable, but I didn't succeed.


Solution

  • roc_auc_score should be able to handle an array of strings. But computing an ROC curve generally requires y_pred to be an array of floats.

    Print your output_test and model_lr.predict(test) and make sure they look like the following—you'll probably see you need to switch to model_lr.predict_proba(test):

    from sklearn.metrics import roc_auc_score
    
    y_true = ["A", "A", "A", "B", "B", "B"]
    y_pred = [0.2, 0.3, 0.6, 0.4, 0.7, 0.8]
    
    print(roc_auc_score(y_true, y_pred))
    # 0.8888