1.ROC曲线
占坑。
------------------------------------------- 分割线 -------------------------------------------
2.混淆矩阵
混淆矩阵的作用:对机器学习的学习效果进行评估的一种指标。confusion matrix is to evaluate the accuracy of a classification.它的作用是评估分类的准确度。
混淆矩阵的定义:对于混淆矩阵C,C(i,j) 表示真实分类是第i类,但是预测值为第j类的观测总数。
对于二分类问题(假设1是正类):
C(0,0)表示真反类 TN(true negative) | C(0,1)表示假正类 FP(false negative) |
C(1,0)表示假反类 FP(false negative) | C(1,1)表示真正类 TP(true positive) |
混淆矩阵的实现:
通过使用scikit-learn模块计算混淆矩阵,
sklearn.metrics.confusion_matrix(y_true, y_pred, labels=None, sample_weight=None).
下面对参数进行说明:(注:表格中的数组类型在python中对应为列表list()类型)
Parameters: 参数: | y_true : 长度为n_samples的数组类型
y_pred : 长度为n_samples的数组类型
labels : [可选参数],长度为n_classes的数组类型 (n_classes表示类别/标签个数)
sample_weight : 类似长度为n_samples的数组形式,可选参数。
|
Returns: 返回值: | C : array, shape = [n_classes, n_classes] 返回混淆矩阵(Confusion matrix)C,shape=(n_classes, n_classes)。 |
矩阵的对角线表示正确分类的个数,非对角线的点是分类错误的情况。
混淆矩阵的实例:
1.计算混淆矩阵:
from sklearn.metrics import confusion_matrix
y_true = [2, 0, 2, 2, 0, 1]
y_pred = [0, 0, 2, 2, 0, 2]
confusion_matrix(y_true, y_pred)
2.使用matplotlib.pyplot画出混淆矩阵:
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names,
title='Confusion matrix, without normalization')
# Plot normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
title='Normalized confusion matrix')
plt.show()
-----end-----