0

I am not able to get the confusion matrix. Can someone help with this?

#Reescalar datos
X = train_set.iloc[:,:20].values
y = train_set.iloc[:,20:21].values
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X = sc.fit_transform(X)
print('Normalized data:')
print(X[0])
#OneHotEncoderData
from sklearn.preprocessing import OneHotEncoder
ohe = OneHotEncoder()
y = ohe.fit_transform(y).toarray()
print('One hot encoded array:')
print(y[0:5])
#Separar la data en entrenamiento y test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
#Crear modelo de red neuronal
from keras.models import Sequential
from keras.layers import Dense

model = Sequential()
model.add(Dense(16, input_dim=20, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(4, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
#Dibujar modelo
history = model.fit(X_train, y_train, epochs=100, batch_size=64)

#Predecir clasificación en conjunto de test
y_pred = model.predict(X_test)
#Converting predictions to label
pred = list()
for i in range(len(y_pred)):
    pred.append(np.argmax(y_pred[i]))
#Converting one hot encoded test label to label
test = list()
for i in range(len(y_test)):
    test.append(np.argmax(y_test[i]))

#Crear matriz de confusión y hallar exactitud
from sklearn.metrics import accuracy_score
a = accuracy_score(pred,test)
print('Accuracy is:', a*100)

matriz = confusion_matrix(y_test, y_pred)
print('Matriz de Confusión:')
print(matriz)

I get the data from this page https://www.kaggle.com/iabhishekofficial/mobile-price-classification

And the error I get is:

ValueError: Classification metrics can't handle a mix of multilabel-indicator and continuous-multioutput targets
desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Please provide the expected [MRE - Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. Your posted code fails to run due to several undefined variables. [Include your minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of the example. Your code is not minimal, and you omitted most of the error message. – Prune Jan 31 '21 at 18:12
  • 1
    The error message tells you what is wrong: you specified a mixture of incompatible target types. Where are you confused with this information? – Prune Jan 31 '21 at 18:15

1 Answers1

0

Use this:

y_pred = model.predict(x) 
y_pred_classes = y_pred.argmax(axis=-1)

matriz = confusion_matrix(y_test, y_pred_classes)
print('Matriz de Confusión:')
print(matriz)

Keras predict method returns Probability values instead of class label

Chandan Malla
  • 481
  • 5
  • 14