为什么KNeighborsClassifier总是预测相同的数字?

为什么knn总是预测相同的数字?我该如何解决这个问题?数据集在这里链接

代码:

import numpy as npimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)import osimport scipy.io   from sklearn.neighbors import KNeighborsClassifierfrom sklearn import metricsfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import StandardScalerfrom torch.utils.data import Dataset, DataLoaderfrom sklearn import preprocessingimport torchimport numpy as npfrom sklearn.model_selection import KFoldfrom sklearn.neighbors import KNeighborsClassifierfrom sklearn import metricsdef load_mat_data(path):    mat = scipy.io.loadmat(DATA_PATH)    x,y = mat['data'], mat['class']    x = x.astype('float32')    # stadardize values    standardizer = preprocessing.StandardScaler()    x = standardizer.fit_transform(x)     return x, standardizer, ydef numpyToTensor(x):    x_train = torch.from_numpy(x)    return x_trainclass DataBuilder(Dataset):    def __init__(self, path):        self.x, self.standardizer, self.y = load_mat_data(DATA_PATH)        self.x = numpyToTensor(self.x)        self.len=self.x.shape[0]        self.y = numpyToTensor(self.y)    def __getitem__(self,index):              return (self.x[index], self.y[index])    def __len__(self):        return self.lendatasets = ['/home/katerina/Desktop/datasets/GSE75110.mat']for DATA_PATH in datasets:    print(DATA_PATH)    data_set=DataBuilder(DATA_PATH)    pred_rpknn = [0] * len(data_set.y)    kf = KFold(n_splits=10, shuffle = True, random_state=7)    for train_index, test_index in kf.split(data_set.x):        #Create KNN Classifier        knn = KNeighborsClassifier(n_neighbors=5)        #print("TRAIN:", train_index, "TEST:", test_index)        x_train, x_test = data_set.x[train_index], data_set.x[test_index]        y_train, y_test = data_set.y[train_index], data_set.y[test_index]        #Train the model using the training sets        y1_train = y_train.ravel()        knn.fit(x_train, y1_train)        #Predict the response for test dataset        y_pred = knn.predict(x_test)        #print(y_pred)        # Model Accuracy, how often is the classifier correct?        print("Accuracy:",metrics.accuracy_score(y_test, y_pred))        c = 0        for idx in test_index:            pred_rpknn[idx] = y_pred[c]            c +=1    print("Accuracy:",metrics.accuracy_score(data_set.y, pred_rpknn))    print(pred_rpknn, data_set.y.reshape(1,-1))

输出:

/home/katerina/Desktop/datasets/GSE75110.matAccuracy: 0.2857142857142857Accuracy: 0.38095238095238093Accuracy: 0.14285714285714285Accuracy: 0.4Accuracy: 0.3Accuracy: 0.25Accuracy: 0.3Accuracy: 0.6Accuracy: 0.25Accuracy: 0.45Accuracy: 0.33497536945812806[3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]

我尝试将knn与k折交叉验证结合,以使用10个折叠测试整个数据集。问题是knn在每个折叠中总是预测3的数组。我想要预测的类别是这些:

tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]]


回答:

TL;DR
这与StandardScaler有关,将其更改为简单的归一化处理。
例如:

from sklearn import preprocessing...x = preprocessing.normalize(x)

解释:

您使用的标准化处理会执行以下操作:

样本`x`的标准分数计算如下:    z = (x - u) / s其中`u`是训练样本的均值,如果`with_mean=False`则为零,`s`是训练样本的标准差,如果`with_std=False`则为一。

而您实际上希望这些特征帮助KNN决定哪个向量更接近。

在normalize中,归一化是分别对每个向量进行的,因此不会影响甚至有助于KNN区分向量

对于KNN,StandardScaler实际上可能损害您的预测。最好在其他形式的数据中使用它。

import scipy.iofrom torch.utils.data import Datasetfrom sklearn import preprocessingimport torchimport numpy as npfrom sklearn.model_selection import KFoldfrom sklearn.neighbors import KNeighborsClassifierfrom sklearn import metricsdef load_mat_data(path):    mat = scipy.io.loadmat(DATA_PATH)    x, y = mat['data'], mat['class']    x = x.astype('float32')    # stadardize values    x = preprocessing.normalize(x)    return x, ydef numpyToTensor(x):    x_train = torch.from_numpy(x)    return x_trainclass DataBuilder(Dataset):    def __init__(self, path):        self.x, self.y = load_mat_data(DATA_PATH)        self.x = numpyToTensor(self.x)        self.len=self.x.shape[0]        self.y = numpyToTensor(self.y)    def __getitem__(self,index):        return (self.x[index], self.y[index])    def __len__(self):        return self.lendatasets = ['/home/katerina/Desktop/datasets/GSE75110.mat']for DATA_PATH in datasets:    print(DATA_PATH)    data_set=DataBuilder(DATA_PATH)    pred_rpknn = [0] * len(data_set.y)    kf = KFold(n_splits=10, shuffle = True, random_state=7)    for train_index, test_index in kf.split(data_set.x):        #Create KNN Classifier        knn = KNeighborsClassifier(n_neighbors=5)        #print("TRAIN:", train_index, "TEST:", test_index)        x_train, x_test = data_set.x[train_index], data_set.x[test_index]        y_train, y_test = data_set.y[train_index], data_set.y[test_index]        #Train the model using the training sets        y1_train = y_train.view(-1)        knn.fit(x_train, y1_train)        #Predict the response for test dataset        y_pred = knn.predict(x_test)        #print(y_pred)        # Model Accuracy, how often is the classifier correct?        print("Accuracy in loop:", metrics.accuracy_score(y_test, y_pred))        c = 0        for idx in test_index:            pred_rpknn[idx] = y_pred[c]            c +=1    print("Accuracy:",metrics.accuracy_score(data_set.y, pred_rpknn))    print(pred_rpknn, data_set.y.reshape(1,-1))Accuracy in loop: 1.0Accuracy in loop: 0.8571428571428571Accuracy in loop: 0.8571428571428571Accuracy in loop: 1.0Accuracy in loop: 0.9Accuracy in loop: 0.9Accuracy in loop: 0.95Accuracy in loop: 1.0Accuracy in loop: 0.9Accuracy in loop: 1.0Accuracy: 0.9359605911330049

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中创建了一个多类分类项目。该项目可以对…

发表回复

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