【问题标题】:Confusion Matrix for 10-fold cross validation in scikit learnscikit learn中10倍交叉验证的混淆矩阵
【发布时间】:2017-03-03 07:44:43
【问题描述】:

我是机器学习和 scikit 的新手。我想知道如何使用 scikit 计算 10 倍 croos 验证中的 confusin 矩阵。如何找到 y_test 和 y_pred?

【问题讨论】:

标签: python machine-learning scikit-learn confusion-matrix


【解决方案1】:
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`.
    """
    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)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
                 horizontalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')



from sklearn import datasets
from sklearn.cross_validation import cross_val_score
from sklearn import svm
from sklearn.metrics import confusion_matrix
import itertools
import numpy as np
import matplotlib.pyplot as plt
from sklearn import cross_validation
iris = datasets.load_iris()
class_names = iris.target_names
# shape of data is 150
cv = cross_validation.KFold(150, n_folds=10,shuffle=False,random_state=None)
for train_index, test_index in cv:

    X_tr, X_tes = iris.data[train_index], iris.data[test_index]
    y_tr, y_tes = iris.target[train_index],iris.target[test_index]
    clf = svm.SVC(kernel='linear', C=1).fit(X_tr, y_tr) 

    y_pred=clf.predict(X_tes)
    cnf_matrix = confusion_matrix(y_tes, y_pred)
    np.set_printoptions(precision=2)

    # Plot non-normalized confusion matrix
    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()

【讨论】:

  • 请添加这一行:%matplotlib inline
  • 能告诉我问题吗
  • NameError Traceback (最近一次调用最后一次) in () 45 class_names = iris.target_names 46 # shape of data is 150 ---> 47 cv = cross_validation.KFold(150, n_folds=10,shuffle=False,random_state=None) 48 for train_index, test_index in cv: 49 NameError: name 'cross_validation' is not defined
  • 嗨 vsafa,它在我的系统中运行良好。因此,如果您收到此错误,请添加以下行: from sklearn import cross_validation
猜你喜欢
  • 2014-04-19
  • 2016-05-12
  • 2017-02-24
  • 2012-01-07
  • 2018-05-22
  • 2012-04-01
  • 1970-01-01
  • 2019-04-13
  • 2022-01-19
相关资源
最近更新 更多