Valueerror: Classification Metrics can’t Handle a mix of multilabel-Indicator and Binary targets

Asked

Viewed 64 times

-1

Hello. I’m creating a neural network to categorize some data. Basically the neural network will tell me which molecule is.

This is my code:

import pandas as pd
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Dropout
from sklearn.model_selection import cross_val_score
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.preprocessing import LabelEncoder, StandardScaler

#%%
base = pd.read_csv('fotoacustica-mol.csv')
atributos = base.iloc[:, :-1].to_numpy('float64')
classe = base.iloc[:, -1].to_numpy()

scaler = StandardScaler()
atributos = scaler.fit_transform(atributos)

encoder = LabelEncoder()
classe = encoder.fit_transform(classe)
classe = np_utils.to_categorical(classe)

#%%
def neuralNetwork():
    classificador = Sequential()
    classificador.add(Dense(units=4, activation='relu', input_dim=4))
    classificador.add(Dropout(0.2))
    classificador.add(Dense(units=4, activation='relu'))
    classificador.add(Dropout(0.2))
    classificador.add(Dense(units=3, activation='softmax'))
    classificador.compile(optimizer='adam', loss='categorical_crossentropy', 
                          metrics=['categorical_accuracy'])
    return classificador

#%%
classificador = KerasClassifier(build_fn=neuralNetwork, epochs=10,
                                batch_size=10)

resultados = cross_val_score(estimator=classificador,
                             X=atributos, y=classe,
                             cv=10, scoring='accuracy')

However this error occurs when you run the other portion of the database

ValueError: Classification metrics can't handle a mix of multilabel-indicator and binary targets

I don’t understand what it is. I thank you in advance.

1 answer

0

The error indicates that you are passing more than one label per sample (for example, a list or array with more than one element). Compare the values being provided in fit in the variable classe (of the first portion and second portion of the data). Make sure they have the same shape.

  • My attribute class has Shape (806, 4) while my class has (806, 3). I checked all this anyway the error persists.

  • I managed to run doing only categorical (0, 1, 2), but it is not yet as I want to implement.

  • The problem is not related to the attributes but to the class. You have two options, out works with Y.Shape=(806, 1) and assigning values [0] or [1] or [2] or with Y.Shape=(806, 3) and [1, 0, 0] or [0, 1, 0] or [0, 0, 1] (whereas you are not working with multi label)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.