我一直在玩 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)
}
==================================================== =======================