kNN 持续过度使用一个标签

我使用 kNN 来对标记的图像进行分类。分类完成后,我输出了一个混淆矩阵。我注意到一个标签,bottle 被错误应用的频率更高。bottle_matrix

我移除了这个标签并再次测试,但随后发现另一个标签,shoe 被错误应用了,而上次它是正常的。enter image description here

没有进行归一化,所以我不确定是什么导致了这种行为。测试显示,无论我移除多少个标签,这种情况都会继续。我不太确定应该发布多少代码,所以我会放一些相关的内容,并将剩余的代码放到 pastebin 上。

def confusionMatrix(classifier, train_DS_X, train_DS_y, test_DS_X, test_DS_y):    # Will output a confusion matrix graph for the predicion    y_pred = classifier.fit(train_DS_X, train_DS_y).predict(test_DS_X)    labels = set(set(train_DS_y) | set(test_DS_y))    def plot_confusion_matrix(cm, title='Confusion matrix', cmap=plt.cm.Blues):        plt.imshow(cm, interpolation='nearest', cmap=cmap)        plt.title(title)        plt.colorbar()        tick_marks = np.arange(len(labels))        plt.xticks(tick_marks, labels, rotation=45)        plt.yticks(tick_marks, labels)        plt.tight_layout()        plt.ylabel('True label')        plt.xlabel('Predicted label')    # Compute confusion matrix    cm = confusion_matrix(test_DS_y , y_pred)    np.set_printoptions(precision=2)    print('Confusion matrix, without normalization')    #print(cm)    plt.figure()    plot_confusion_matrix(cm)    # Normalize the confusion matrix by row (i.e by the number of samples    # in each class)    cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]    print('Normalized confusion matrix')    #print(cm_normalized)    plt.figure()    plot_confusion_matrix(cm_normalized, title='Normalized confusion matrix')    plt.show()

主函数中的相关代码:

# Select training and test data    PCA = decomposition.PCA(n_components=.95)    zscorer = ZScoreMapper(param_est=('targets', ['rest']), auto_train=False)    DS = getVoxels (1, .5)    train_DS = DS[0]    test_DS = DS[1]    # Apply PCA and ZScoring    train_DS = processVoxels(train_DS, True, zscorer, PCA)    test_DS = processVoxels(test_DS, False, zscorer, PCA)    print 3*"\n"    # Select the desired features    # If selecting samples or PCA, that must be the only feature    featuresOfInterest = ['pca']    trainDSFeat = selectFeatures(train_DS, featuresOfInterest)    testDSFeat = selectFeatures(test_DS, featuresOfInterest)    train_DS_X = trainDSFeat[0]    train_DS_y = trainDSFeat[1]    test_DS_X = testDSFeat[0]    test_DS_y = testDSFeat[1]    # Optimization of neighbors    # Naively searches for local max starting at numNeighbors    lastScore = 0    lastNeightbors = 1    score = .0000001    numNeighbors = 5    while score > lastScore:        lastScore = score        lastNeighbors = numNeighbors        numNeighbors += 1        #Classification        neigh = neighbors.KNeighborsClassifier(n_neighbors=numNeighbors, weights='distance')        neigh.fit(train_DS_X, train_DS_y)        #Testing        score = neigh.score(test_DS_X,test_DS_y )    # Confusion Matrix Output    neigh = neighbors.KNeighborsClassifier(n_neighbors=lastNeighbors, weights='distance')    confusionMatrix(neigh, train_DS_X, train_DS_y, test_DS_X, test_DS_y)

Pastebin: http://pastebin.com/U7yTs3vs


回答:

问题部分是由于我的轴标签错误导致的,当我以为自己移除了错误的标签时,实际上只是移除了一个随机的标签,这意味着错误的数据仍然在被分析。修正轴标签并移除实际错误的标签rest后,得到的结果是:enter image description here

我更改的代码是:cm = confusion_matrix(test_DS_y , y_pred, labels)

基本上,我是根据我的有序标签列表手动设置了顺序。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注