Мне удалось получить результат в виде 55 строк, используя следующий код. Некоторые из изменений, которые я сделал, были для pt2
вместо as.character
, я сделал это в as.factor
и вместо pred <- predict(model_svm, test)
до pred <- predict(model_svm, as.matrix(test))
.
# load libraries
library(data.table)
library(e1071)
# create dataset with random values
featuredata_all <- matrix(rnorm(23*218), ncol=23)
# scale features
pt1 <- scale(featuredata_all[,1:22],center=T)
# make column as factor
pt2 <- as.factor(ifelse(featuredata_all[,23]>0, 0,1)) #since the label is a string I kept it separate
# join data (optional)
ft<-cbind.data.frame(pt1,pt2) #to preserve the label in text
colnames(ft)[23]<- "Cluster"
## 75% of the sample size
smp_size <- floor(0.75 * nrow(ft))
## set the seed to make your partition reproducible
set.seed(123)
train_ind <- sample(seq_len(nrow(ft)), size = smp_size)
# split data to train
train <- ft[train_ind,1:22] #163 reads
test <- ft[-train_ind,1:22] #55 reads
dim(train)
# [1] 163 22
dim(test)
# [1] 55 22
# split data to test
trainlabel<- ft[train_ind,23] #163 labels
testlabel <- ft[-train_ind,23] #55 labels
length(trainlabel)
[1] 163
length(testlabel)
[1] 55
#Support Vector Machine for classification
model_svm <- svm(x= as.matrix(train), y = trainlabel, probability = T)
summary(model_svm)
# Call:
# svm.default(x = as.matrix(train), y = trainlabel, probability = T)
#
#
# Parameters:
# SVM-Type: C-classification
# SVM-Kernel: radial
# cost: 1
#
# Number of Support Vectors: 159
#
# ( 78 81 )
#
#
# Number of Classes: 2
#
# Levels:
# 0 1
#Use the predictions on the data
# ---------------- This is where the question is ---------------- #
pred <- predict(model_svm, as.matrix(test))
length(pred)
# [1] 55
# ----------------------------------------------------------------#
print(table(pred[1:nrow(test)],testlabel))
# testlabel
# 0 1
# 0 14 14
# 1 11 16
Надеюсь, это поможет.