我正在使用tf-learn进行情感分析任务,正在处理混淆矩阵代码,当我运行评估代码时出现此错误分类指标无法处理二进制和连续多输出的混合目标,我可以解决这个问题。它不能处理Y_test和Y_predict的形状。
编码目标值
# convert target value from string to integer
le=LabelEncoder()
Y_le=le.fit_transform(Y)
Y_le_oh=to_categorical(Y_le)
#np.argmax(to_categorical(Y_le))
培训,测试分组
X_train, X_test, Y_train, Y_test = train_test_split(X_pad,Y_le_oh, test_size = 0.33, random_state = 42)
X_train, X_Val, Y_train, Y_Val = train_test_split(X_train,Y_train, test_size = 0.1, random_state = 42)
print(X_train.shape,Y_train.shape)
print(X_test.shape,Y_test.shape)
print(X_Val.shape,Y_Val.shape)
type(Y_test)
混淆矩阵
def cnn_evaluate():
# predict class with test set
Y_predict = model.predict(X_test, batch_size=batch_size, verbose=0)
print('Accuracy:\t{:0.1f}%'.format(accuracy_score(np.argmax(Y_test,axis=1),Y_predict)*100))
#classification report
print('\n')
print(classification_report(np.argmax(Y_test,axis=1), Y_predict))
#confusion matrix
confmat = confusion_matrix(np.argmax(Y_test,axis=1), Y_predict)
fig, ax = plt.subplots(figsize=(4, 4))
ax.matshow(confmat, cmap=plt.cm.Blues, alpha=0.3)
for i in range(confmat.shape[0]):
for j in range(confmat.shape[1]):
ax.text(x=j, y=i, s=confmat[i, j], va='center', ha='center')
plt.xlabel('Predicted label')
plt.ylabel('True label')
plt.tight_layout()