0

If I use the example code from the e1071 documentation on page 52, I obtain a pred variable of class "factor".

> 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" ...

This is fine; but when I use the same commands on my data, I obtain a pred variable of class "numeric":

> 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" ...

and this seems wrong; the prediction does not seem to work at all.

My code is:

# 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)

Where do I go wrong?

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 に答える