python-3.xscikit-learnmetricsmedicalmedical-imaging

f1_score() takes 2 positional arguments but 3 positional arguments (and 1 keyword-only argument) were given


 from sklearn.metrics import f1_score


 F1_score = f1_score(lab2d, pred2d, [0, 1, 2, 3], average=None)
print("Validation Dice Coefficient.... ")
print("Background:", F1_score[0])
print("CSF:", F1_score[1])
print("GM:", F1_score[2])
print("WM:", F1_score[3])
 ```

This is the error

f1_score() takes 2 positional arguments but 3 positional arguments (and 1  keyword-only argument) were given

I tried to pass the arguments in diffrerent ways but still got the same error


Solution

  • As the documentation for version 1.2.0 states, you need to call the function like

    from sklearn.metrics import f1_score
    y_true = [0, 1, 2, 0, 1, 2]
    y_pred = [0, 2, 1, 0, 0, 1]
    f1_score(y_true, y_pred, average='macro')
    

    Please note that it accepts 3 arguments: y_true, y_pred and keyword argument average. You are trying to put one extra argument in: [0, 1, 2, 3]. Either it or one of the previous two arguments (lab2d or pred2d) should be removed.

    In older versions, however, it was possible to use labels argument on this position without naming it explicitly. If that's the case, you might try to write this line like

    f1_score(lab2d, pred2d, labels=[0, 1, 2, 3], average=None)