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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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