0
votes

I try to use classification_report from sklearn.metrics:

sklearn.metrics.classification_report(y_true, y_pred, labels=None, target_names=None, sample_weight=None, digits=2, output_dict=False)

As input for prediction and label i've got one list each with the following form:

for pred:

[array([0, 0, 0, 3, 0, 3, 2, 2, 1, 1, 2, 0, 2, 3, 0, 2, 2, 2, 2, 3, 3, 2,
       2, 2, 2, 2, 2, 1, 0, 3, 2, 2, 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 2, 2,
       2, 1, 0, 0, 0, 2, 2, 0, 2, 0, 2, 1, 0, 2, 2, 3, 0, 2, 0, 2])]

for true:

[array([2, 3, 3, 2, 3, 3, 2, 3, 2, 2, 3, 3, 3, 2, 3, 2, 3, 2, 3, 2, 3, 3,
       2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 3, 3, 2, 2, 2, 2, 2, 3, 2,
       2, 2, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 2, 2, 3, 2, 2, 2, 2, 2])]

for the sklearn-function above i need a simple list. The array produces an error:

ValueError: multiclass-multioutput is not supported

I tried .tolist() already but didn't work for me.

I am searching a possibility to convert my array-list [?] to a simple list.

Thanks for your help.

2
Please use code blocks for your code (the {} icon). - 9769953
Perhaps you need the first element of your single-element list instead: y_true[0] and y_pred[0], have you tried that? - 9769953
why are you sending a 1 length list with an array inside it? Just send the arrays directly? - Paritosh Singh
"I am searching a possibility to convert my array-list [?] to a simple list." You can pass numpy arrays in without a problem. But for some reason you are wrapping the array's inside a list. So instead of pred = [np.array([0, 0, ...,])] do pred = np.array([0, 0, ...,]). If you can show the code generating pred and true it might be more obvious where you mistake is that has put them into a single element list. - Dan
@VomDorfe see my answer - seralouk

2 Answers

0
votes

Each of those objects is already a list, each of which contains a single element, which is an array.

To access the 1st element and convert it to a list, try something like:

x = [array([2, 3, 3, 2, 3, 3, 2, 3, 2, 2, 3, 3, 3, 2, 3, 2, 3, 2, 3, 2, 3, 3,
   2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2, 3, 3, 2, 2, 2, 2, 2, 3, 2,
   2, 2, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 2, 2, 3, 2, 2, 2, 2, 2])]
x_list = list(x[0])

And x_list will contain the array element in list form.

0
votes

Way 1: Just index the lists e.g. pred[0]

Code:

pred = [array([0, 0, 0, 3, 0, 3, 2, 2, 1, 1, 2, 0, 2, 3, 0, 2, 2, 2, 2, 3, 3, 2,
            2, 2, 2, 2, 2, 1, 0, 3, 2, 2, 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 2, 2,
            2, 1, 0, 0, 0, 2, 2, 0, 2, 0, 2, 1, 0, 2, 2, 3, 0, 2, 0, 2])]

test = [array([0, 0, 0, 3, 0, 3, 2, 2, 1, 1, 2, 0, 2, 3, 0, 2, 2, 2, 2, 3, 3, 2,
            2, 2, 2, 2, 2, 1, 0, 3, 2, 2, 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 2, 2,
            2, 1, 0, 0, 0, 2, 2, 0, 2, 0, 2, 1, 0, 2, 2, 3, 0, 2, 0, 2])]

classification_report(pred[0], test[0])

Way 2:

Reform it to match sklearn requirements:

pred = [array([0, 0, 0, 3, 0, 3, 2, 2, 1, 1, 2, 0, 2, 3, 0, 2, 2, 2, 2, 3, 3, 2,
            2, 2, 2, 2, 2, 1, 0, 3, 2, 2, 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 2, 2,
            2, 1, 0, 0, 0, 2, 2, 0, 2, 0, 2, 1, 0, 2, 2, 3, 0, 2, 0, 2])]

test = [array([0, 0, 0, 3, 0, 3, 2, 2, 1, 1, 2, 0, 2, 3, 0, 2, 2, 2, 2, 3, 3, 2,
            2, 2, 2, 2, 2, 1, 0, 3, 2, 2, 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 2, 2,
            2, 1, 0, 0, 0, 2, 2, 0, 2, 0, 2, 1, 0, 2, 2, 3, 0, 2, 0, 2])]

flat_pred = [item for sublist in pred for item in sublist]
flat_test = [item for sublist in test for item in sublist]

print(classification_report(flat_pred,flat_test))