MNIST数字识别数据集的性能不佳

时间:2014-06-12 04:24:45

标签: r machine-learning pca mnist

我一直在玩MNIST数字识别数据集,我有点卡住了。我读了一些研究论文,并实现了我所理解的一切。基本上我所做的是我首先创建了我的训练集和交叉验证集以评估我的分类器,然后我在我的测试和训练集上运行PCA,之后我使用KNN和SVM执行分类任务。我面临的主要问题是,我应该在所有集合上运行PCA,然后将我的训练集和交叉验证集分开或将它们分开,然后在交叉验证测试和训练集上单独运行PCA。我为问我已经尝试过的事情而道歉,因为我已经尝试了两种情况,在第一种情况下我的分类器执行得非常出色,因为我猜PCA使用测试数据集同时创建调整我的结果的主要组件并且很可能在我的模型中存在偏差的原因,在另一种情况下,性能是大约20%到30%,这是非常低的。所以我有点困惑,我应该如何改进我的模型,非常感谢任何帮助和指导,我已经粘贴了我的代码以供参考。

library(ggplot2)
library(e1071)
library(ElemStatLearn)
library(plyr)
library(class)

import.csv <- function(filename){
  return(read.csv(filename, sep = ",", header = TRUE, stringsAsFactors = FALSE))
}

train.data <- import.csv("train.csv")
test.data <- train.data[30001:32000,]
train.data <- train.data[1:6000,]

#Performing PCA on the dataset to reduce the dimensionality of the data

get_PCA <- function(dataset){
  dataset.features <- dataset[,!(colnames(dataset) %in% c("label"))]
  features.unit.variance <- names(dataset[, sapply(dataset, function(v) var(v, na.rm=TRUE)==0)])
  dataset.features <- dataset[,!(colnames(dataset) %in% features.unit.variance)]
  pr.comp <- prcomp(dataset.features, retx = T, center = T, scale = T)
  #finding the total variance contained in the principal components
  prin_comp <- summary(pr.comp)
  prin_comp.sdev <- data.frame(prin_comp$sdev)
  #print(paste0("%age of variance contained = ", sum(prin_comp.sdev[1:500,])/sum(prin_comp.sdev)))
  screeplot(pr.comp, type = "lines", main = "Principal Components")
  num.of.comp = 50
  red.dataset <- prin_comp$x
  red.dataset <- red.dataset[,1:num.of.comp]
  red.dataset <- data.frame(red.dataset)
  return(red.dataset)
}

#Perform k-fold cross validation 

do_cv_class <- function(df, k, classifier){
  num_of_nn = gsub("[^[:digit:]]","",classifier)
  classifier = gsub("[[:digit:]]","",classifier)
  if(num_of_nn == "")
  {
    classifier = c("get_pred_",classifier)
  }
  else
  {
    classifier = c("get_pred_k",classifier)
    num_of_nn = as.numeric(num_of_nn)
  }
  classifier = paste(classifier,collapse = "")
  func_name <- classifier
  output = vector()
  size_distr = c()
  n = nrow(df)
  for(i in 1:n)
  {
    a = 1 + (((i-1) * n)%/%k)
    b = ((i*n)%/%k)
    size_distr = append(size_distr, b - a + 1)
  }

  row_num = 1:n
  sampling = list()
  for(i in 1:k)
  {
    s = sample(row_num,size_distr)
    sampling[[i]] = s
    row_num = setdiff(row_num,s)
  }
  prediction.df = data.frame()
  outcome.list = list()

  for(i in 1:k)
  {
    testSample = sampling[[i]]
    train_set = df[-testSample,]
    test_set = df[testSample,]    

    if(num_of_nn == "")
    {
      classifier = match.fun(classifier)
      result = classifier(train_set,test_set)
      confusion.matrix <- table(pred = result, true = test_set$label)
      accuracy <- sum(diag(confusion.matrix)*100)/sum(confusion.matrix)
      print(confusion.matrix)
      outcome <- list(sample_ID = i, Accuracy = accuracy)
      outcome.list <- rbind(outcome.list, outcome)
    }
    else
    {

      classifier = match.fun(classifier)
      result = classifier(train_set,test_set)
      print(class(result))
      confusion.matrix <- table(pred = result, true = test_set$label)
      accuracy <- sum(diag(confusion.matrix)*100)/sum(confusion.matrix)
      print(confusion.matrix)
      outcome <- list(sample_ID = i, Accuracy = accuracy)
      outcome.list <- rbind(outcome.list, outcome)
    }
  }
  return(outcome.list)
}

#Support Vector Machines with linear kernel

get_pred_svm <- function(train, test){
  digit.class.train <- as.factor(train$label)
  train.features <- train[,-train$label]
  test.features <- test[,-test$label]
  svm.model <- svm(train.features, digit.class.train, cost = 10, gamma =  0.0001, kernel = "radial")
  svm.pred <- predict(svm.model, test.features)
  return(svm.pred)
}

#KNN model
get_pred_knn <- function(train,test){
  digit.class.train <- as.factor(train$label)
  train.features <- train[,!colnames(train) %in% "label"]
  test.features <- test[,!colnames(train) %in% "label"]
  knn.model <- knn(train.features, test.features, digit.class.train)
  return(knn.model)
}

=============================================== =========================

1 个答案:

答案 0 :(得分:4)

将PCA视为您应用于数据的转换。你想要做两件事:

  1. 由于测试集模仿了真实世界&#34;你得到之前没见过的样本的情况,你不能使用测试集来评估分类器。
  2. 您需要对所有样本应用相同的转换。
  3. 因此,您需要将PCA应用于训练集,保留转换数据,这是两条信息:

    1. 从样本中减去的平均值,以便集中它们。
    2. 变换矩阵,即协方差矩阵的特征向量
    3. 并将相同的转换应用于测试集。