Python 3.x 人工神经网络的多类分类
我在Python中使用ANN进行多类分类(12个类)。然而,我得到了错误。以下是代码片段:Python 3.x 人工神经网络的多类分类,python-3.x,machine-learning,neural-network,Python 3.x,Machine Learning,Neural Network,我在Python中使用ANN进行多类分类(12个类)。然而,我得到了错误。以下是代码片段: import keras from keras.models import Sequential from keras.layers import Dense # Initialising the ANN # Initialising the ANN classifier = Sequential() # Adding the input layer and the first hidden laye
import keras
from keras.models import Sequential
from keras.layers import Dense
# Initialising the ANN
# Initialising the ANN
classifier = Sequential()
# Adding the input layer and the first hidden layer
classifier.add(Dense(units = 8, kernel_initializer = 'uniform', activation = 'relu', input_dim = 4))
# Adding the second hidden layer
classifier.add(Dense(units = 8, kernel_initializer = 'uniform', activation = 'relu'))
# Adding the output layer
classifier.add(Dense(units = 13, kernel_initializer = 'uniform', activation = 'softmax'))
# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])
# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size =200 , epochs = 100)
# Predicting the Test set results
y_pred = classifier.predict(X_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
程序一直运行,直到运行神经代码,还找到了y_pred。之后,我得到了这个错误,即混淆矩阵没有形成
错误:
ValueError:分类指标无法处理多类和连续多输出目标的混合
from sklearn.metrics import confusion_matrix
y_pred = classifier.predict(X_test)
predictions = np.argmax(y_pred, axis=-1)
cm = confusion_matrix(y_test, y_pred)
我希望它能解决你的问题
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import LabelEncoder
y_pred = classifier.predict(X_test)
predictions = np.argmax(y_pred, axis=-1)
label_encoder = LabelEncoder().fit(y_test)
label_y = label_encoder.transform(y_test)
cm = confusion_matrix(label_y, predictions)