如何从以下模型中获取混淆矩阵?
How to get confusion matrix from the below model?
X 代表特征,Y 代表图像分类标签。我正在使用 CNN 进行二进制图像分类,例如猫和狗。
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
import pickle
import numpy as np
from sklearn.metrics import confusion_matrix
X = np.array(pickle.load(open("X.pickle","rb")))
Y = np.array(pickle.load(open("Y.pickle","rb")))
x_test = np.array(pickle.load(open("x_test.pickle","rb")))
y_test = np.array(pickle.load(open("y_test.pickle","rb")))
# X = np.array(pickle.load(open("x_train.pickle","rb")))
# Y = np.array(pickle.load(open("y_train.pickle","rb")))
#scaling our image data
X = X/255.0
model = Sequential()
#model.add(Conv2D(64 ,(3,3), input_shape = X.shape[1:]))
model.add(Conv2D(64 ,(3,3), input_shape = X.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Conv2D(128 ,(3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Conv2D(256 ,(3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Conv2D(512 ,(3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Flatten())
model.add(Dense(2048))
model.add(Activation("relu"))
model.add(Dropout(0.5))
np.argmax(model.add(Dense(2)))
model.add(Activation('softmax'))
model.compile(loss="binary_crossentropy",
optimizer = "adam",
metrics = ['accuracy'])
predicted = model.predict(x_test)
print(predicted.shape)
print(y_test.shape)
print(confusion_matrix(y_test,predicted))
预测和 y_test 形状的输出是 (90, 2) 和
(90,) 当我使用混淆矩阵时它会刷新:-
ValueError:分类指标无法处理二进制和连续多输出目标的混合。
您可以使用 scikit-learn:
from sklearn.metrics import confusion_matrix
predicted = model.predict(x_test)
print(confusion_matrix(y_test,predicted.round()))
这是混淆矩阵的 scikit 学习文档:
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html
编辑:
建议:
- 更喜欢在输出层使用 Softmax 激活,无论是二分类还是多标签分类。在输出层中使用 softmax 节点数 = 类.
这是我们如何使用 PyCM 库获取混淆矩阵的一个示例:
您需要在 anaconda 中安装 pycm 库或使用 pip3
conda install -c sepandhaghighi pycm
from pycm import *
def plot_confusion_matrix(cm, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
"""
This function modified to plots the ConfusionMatrix object.
Normalization can be applied by setting 'normalize=True'.
Code Reference :
http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
"""
plt_cm = []
for i in cm.classes :
row=[]
for j in cm.classes:
row.append(cm.table[i][j])
plt_cm.append(row)
plt_cm = np.array(plt_cm)
if normalize:
plt_cm = plt_cm.astype('float') / plt_cm.sum(axis=1)[:, np.newaxis]
plt.imshow(plt_cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(cm.classes))
plt.xticks(tick_marks, cm.classes, rotation=45)
plt.yticks(tick_marks, cm.classes)
fmt = '.2f' if normalize else 'd'
thresh = plt_cm.max() / 2.
for i, j in itertools.product(range(plt_cm.shape[0]), range(plt_cm.shape[1])):
plt.text(j, i, format(plt_cm[i, j], fmt),
horizontalalignment="center",
color="white" if plt_cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('Actual')
plt.xlabel('Prediction')
.....
svm.fit(x_train, y_train)
y_predicted = svm.predict(x_test)
#Get the confusion Matrix:
cm = ConfusionMatrix(actual_vector=y_test, predict_vector=y_predicted)
#Print classes
print("[INFO] Clases")
print(cm.classes)
#Print the table of cmatrix
print(cm.table)
#indicators of the confusion matrix
print(cm)
您好!
X 代表特征,Y 代表图像分类标签。我正在使用 CNN 进行二进制图像分类,例如猫和狗。
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D
import pickle
import numpy as np
from sklearn.metrics import confusion_matrix
X = np.array(pickle.load(open("X.pickle","rb")))
Y = np.array(pickle.load(open("Y.pickle","rb")))
x_test = np.array(pickle.load(open("x_test.pickle","rb")))
y_test = np.array(pickle.load(open("y_test.pickle","rb")))
# X = np.array(pickle.load(open("x_train.pickle","rb")))
# Y = np.array(pickle.load(open("y_train.pickle","rb")))
#scaling our image data
X = X/255.0
model = Sequential()
#model.add(Conv2D(64 ,(3,3), input_shape = X.shape[1:]))
model.add(Conv2D(64 ,(3,3), input_shape = X.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Conv2D(128 ,(3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Conv2D(256 ,(3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Conv2D(512 ,(3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Flatten())
model.add(Dense(2048))
model.add(Activation("relu"))
model.add(Dropout(0.5))
np.argmax(model.add(Dense(2)))
model.add(Activation('softmax'))
model.compile(loss="binary_crossentropy",
optimizer = "adam",
metrics = ['accuracy'])
predicted = model.predict(x_test)
print(predicted.shape)
print(y_test.shape)
print(confusion_matrix(y_test,predicted))
预测和 y_test 形状的输出是 (90, 2) 和 (90,) 当我使用混淆矩阵时它会刷新:- ValueError:分类指标无法处理二进制和连续多输出目标的混合。
您可以使用 scikit-learn:
from sklearn.metrics import confusion_matrix
predicted = model.predict(x_test)
print(confusion_matrix(y_test,predicted.round()))
这是混淆矩阵的 scikit 学习文档:
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html
编辑:
建议:
- 更喜欢在输出层使用 Softmax 激活,无论是二分类还是多标签分类。在输出层中使用 softmax 节点数 = 类.
这是我们如何使用 PyCM 库获取混淆矩阵的一个示例: 您需要在 anaconda 中安装 pycm 库或使用 pip3
conda install -c sepandhaghighi pycm
from pycm import *
def plot_confusion_matrix(cm, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
"""
This function modified to plots the ConfusionMatrix object.
Normalization can be applied by setting 'normalize=True'.
Code Reference :
http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
"""
plt_cm = []
for i in cm.classes :
row=[]
for j in cm.classes:
row.append(cm.table[i][j])
plt_cm.append(row)
plt_cm = np.array(plt_cm)
if normalize:
plt_cm = plt_cm.astype('float') / plt_cm.sum(axis=1)[:, np.newaxis]
plt.imshow(plt_cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(cm.classes))
plt.xticks(tick_marks, cm.classes, rotation=45)
plt.yticks(tick_marks, cm.classes)
fmt = '.2f' if normalize else 'd'
thresh = plt_cm.max() / 2.
for i, j in itertools.product(range(plt_cm.shape[0]), range(plt_cm.shape[1])):
plt.text(j, i, format(plt_cm[i, j], fmt),
horizontalalignment="center",
color="white" if plt_cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('Actual')
plt.xlabel('Prediction')
.....
svm.fit(x_train, y_train)
y_predicted = svm.predict(x_test)
#Get the confusion Matrix:
cm = ConfusionMatrix(actual_vector=y_test, predict_vector=y_predicted)
#Print classes
print("[INFO] Clases")
print(cm.classes)
#Print the table of cmatrix
print(cm.table)
#indicators of the confusion matrix
print(cm)
您好!