0

如果我使用第 52 页上的e1071 文档pred中的示例代码,我将获得一个“因子”类的变量。

> str(pred)
 Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
 - attr(*, "names")= chr [1:150] "1" "2" "3" "4" ...

这可以; 但是当我对我的数据使用相同的命令时,我获得了一个“数字”类的 pred 变量:

> str(pred)
 Named num [1:1000] 0.95 0.0502 0.05 0.9902 -0.0448 ...
 - attr(*, "names")= chr [1:1000] "1" "2" "3" "4" ...

这似乎是错误的;预测似乎根本不起作用。

我的代码是:

# create variables to store the path to the files you downloaded:
data.dir   <- "c:/kaggle/scikit/"
train.file <- paste0(data.dir, 'train.csv')
trainLabels.file <- paste0(data.dir, 'trainLabels.csv')


# READ DATA - CAREFUL IF THERE IS A HEADER OR NOT
train <- read.csv(train.file, stringsAsFactors=F, header=FALSE)
trainLabels <- read.csv(trainLabels.file, stringsAsFactors=F, header=FALSE)


# LOADING LIBRARY e1071
install.packages('e1071') 
library('e1071')


## classification mode
model <- svm(train, trainLabels)

summary(model)

# test with train data
pred <- predict(model, train)

我哪里错了?

4

1 回答 1

2

好的,问题是我的类是作为 data.frame 而不是因子给出的。

由于另一个关于将 data.frame 转换为 factor 的问题,我修复了它。

所以我的工作代码是:

data.dir   <- "c:/xampp/htdocs/Big Data/kaggle/scikit/"
train.file <- paste0(data.dir, 'train.csv')
trainLabels.file <- paste0(data.dir, 'trainLabels.csv')


# READ DATA - CAREFUL IF THERE IS A HEADER OR NOT
train <- read.csv(train.file, stringsAsFactors=F, header=FALSE)
trainLabels <- read.csv(trainLabels.file, stringsAsFactors=F, header=FALSE)

# Make the trainLabels a factor
trainLabels <- as.factor(trainLabels$V1)


# APPLYING SVM TO KAGGLE DATA
install.packages('e1071') 
library('e1071')


## classification mode
model <- svm(train, trainLabels)

summary(model)

# test with train data
pred <- predict(model, train)

# Check accuracy:
table(pred, trainLabels)
于 2013-10-09T14:19:52.203 回答