簡體   English   中英

如何在 R 中對 SVM 進行預測?

[英]How to do the prediction for SVM in R?

我有 2 個數據集train_valtest 我想建立 3 個模型並使用這些模型來預測結果。 這是我的 3 個模型:

#Model 1
rf.model <- randomForest(Survived ~ ., data = train_val, type = 'response')

#Model 2
svm.model.linear <- svm(Survived ~ ., data = train_val, kernel="linear", cost = 2, gamma = 0.1)

#Model 3
svm.model.radial <- svm(Survived ~ ., data = train_val, kernel="radial", cost = 10, gamma = 0.1)

使用上面的代碼訓練了 3 個模型后,我使用以下代碼進行預測:

prediction <- predict(rf.model, newdata = test)

prediction的輸出是:

在此處輸入圖像描述

然后我將結果放入 dataframe 中:

df <- data.frame(PassengerId = 892:1309, Survived = prediction)

df的 output 是

在此處輸入圖像描述

到目前為止,一切運行良好,但是,當我用rf.model替換prediction中的svm.model.linear時,output 發生了變化:

在此處輸入圖像描述

因此,dataframe 也有錯誤: 在此處輸入圖像描述

我可以知道這是什么原因嗎?我應該如何獲得與之前使用rf.model時相同的df的 output? 任何幫助將不勝感激!

這是帖子的完整代碼

library(dplyr)
library(tidyr)
library(ggplot2)
library(Amelia)
library(corrgram)
library(caret)
library(randomForest)
library(e1071)


#Import train and test dataset
train <- read.csv("C:/Users/User/Desktop/Titanic/train.csv")
test <- read.csv("C:/Users/User/Desktop/Titanic/test.csv")

#Ensure dataset loaded correctly
head(train)
head(test)


train$set <- "train"
test$set <- "test"
test$Survived <- NA
full <- rbind(train,test)

summary(full)
str(full)


# Creating new training and testing data set
full <- dplyr::select(full,-PassengerId,-Ticket,-Cabin)

#Creating a new variable called Dependents
full$Dependents <- full$SibSp + full$Parch
head(full)


missing_values <- full %>% summarise(across(everything(), ~sum(is.na(.))/length(.)))
missing_values <- missing_values %>% pivot_longer(cols = everything(),names_to = "feature",values_to="missing_pct")

ggplot(missing_values,aes(x=reorder(feature,-missing_pct),y=missing_pct,label=missing_pct)) + geom_text(hjust = 0,aes(label=scales::percent(missing_pct))) +
  geom_col(fill='red') + coord_flip() + scale_y_continuous(labels=scales::percent)


#Overview of scatterplots between all variables
full$Sex <- factor(full$Sex)
full$Sex.factor <- as.numeric(full$Sex)
str(full)
full %>% filter(set == "train") %>% select(-Name,-Embarked,-set,-SibSp,-Parch) %>% corrgram(lower.panel = panel.shade,upper.panel = panel.pie)


full$Survived <- as.factor(full$Survived)
full$Pclass <- as.factor(full$Pclass)
full %>% filter(set == "train") %>% ggplot(aes(Sex)) + geom_bar(aes(fill=Survived))
full %>% filter(set == "train") %>% ggplot(aes(Pclass)) + geom_bar(aes(fill=Survived))
full %>% filter(set == "train") %>% ggplot(aes(Age)) + geom_histogram(bins=10,aes(fill=Survived))
full %>% filter(set == "train") %>% ggplot(aes(Dependents)) + geom_histogram(bins=10,aes(fill=Survived))


full %>% ggplot(aes(x=Pclass,y=Age)) + geom_boxplot(aes(fill=Pclass))
full %>% ggplot(aes(x=Sex,y=Age)) + geom_boxplot(aes(fill=Sex))


#Getting the median age based on Pclass
p1MedAge <- full %>% filter(Pclass == 1 & Age != "NA") %>% summarise(median(Age)) %>% as.numeric()
p2MedAge <- full %>% filter(Pclass == 2 & Age != "NA") %>% summarise(median(Age)) %>% as.numeric()
p3MedAge <- full %>% filter(Pclass == 3 & Age != "NA") %>% summarise(median(Age)) %>% as.numeric()


#Imputing median age to missing data for train data
full$Age[is.na(full$Age) & full$Pclass == 1] <- p1MedAge
full$Age[is.na(full$Age) & full$Pclass == 2] <- p2MedAge
full$Age[is.na(full$Age) & full$Pclass == 3] <- p3MedAge

#Checking for missing age data
any(is.na(full$Age)
    
  
full[is.na(full$Fare),]
    
    
#Imputing the mean fare for Pclass 3
full$Fare[is.na(full$Fare)] <- round(mean(subset(full$Fare, full$Pclass == 3),na.rm = T),0)

#Final check of missing data
any(is.na(subset(full,select=-c(Survived))))


full <- full %>% select(-Name,-SibSp,-Parch,-Sex.factor)
str(full)


full$Dependents <- factor(full$Dependents)
full$Embarked <- factor(full$Embarked)
str(full)


train <- full %>% filter(set == "train") %>% select(-set)
test <- full %>% filter (set == "test") %>% select(-set)

#Creating data partition to cross validation
ind = createDataPartition(train$Survived,times = 1,p = 0.8,list = FALSE)
train_val <- train[ind,]
test_val <- train[-ind,]

#Checking distribution of data partition
round(prop.table(table(train$Survived)*100),digits=3)
round(prop.table(table(train_val$Survived)*100),digits=3)
round(prop.table(table(test_val$Survived)*100),digits=3)


log.model <- glm(formula = Survived ~ . - Fare - Embarked, data = train_val, family = binomial(link='logit'))
summary(log.model)


glm.prediction <- predict(log.model, newdata=test_val, type='response')
glm.prediction <- ifelse(glm.prediction >= 0.5, 1, 0)

table(test_val$Survived,glm.prediction)
sum(test_val$Survived==glm.prediction) / nrow(test_val)


rf.model <- randomForest(Survived ~ ., data = train_val)
print(rf.model$confusion)
importance(rf.model)


rf.prediction <- predict(rf.model, test_val)
table(test_val$Survived,rf.prediction)
sum(test_val$Survived==rf.prediction) / nrow(test_val)


tuned.svm.linear <- tune.svm(Survived ~., data = train, kernel = "linear", cost = c(0.01,0.1,0.2,0.5,0.8,1,2,3,5,10),gamma=c(0.1,0.5,1,2,5))
summary(tuned.svm.linear)
tuned.svm.radial <- tune.svm(Survived ~., data = train, kernel = "radial", cost = c(0.01,0.1,0.2,0.5,0.8,1,2,3,5,10),gamma=c(0.1,0.5,1,2,5))
summary(tuned.svm.radial)


svm.model.linear <- svm(Survived ~ ., data = train_val, kernel="linear", cost = 2, gamma = 0.1)
svm.model.radial <- svm(Survived ~ ., data = train_val, kernel="radial", cost = 10, gamma = 0.1)

svm.prediction.linear <- predict(svm.model.linear, test_val)
table(test_val$Survived,svm.prediction.linear)
sum(test_val$Survived==svm.prediction.linear) / nrow(test_val)

svm.prediction.radial <- predict(svm.model.radial, test_val)
table(test_val$Survived,svm.prediction.radial)
sum(test_val$Survived==svm.prediction.radial) / nrow(test_val)


#Predicting Survival on test data set using Random Forest model
rf.model <- randomForest(Survived ~ ., data = train, type = 'response')
prediction <- predict(svm.model.linear, newdata = test)

submission <- data.frame(PassengerId = 892:1309, Survived = prediction)
write.csv(submission, file = "submission.csv", row.names = FALSE)
paste("Your submission was successfully saved!")

這是我運行代碼時發生錯誤的地方在此處輸入圖像描述

如您所見,當我運行到行submission <- data.frame(PassengerId = 892:1309, Survived = prediction)時,出現了錯誤。

您使用錯誤的表作為newdata

您應該使用經過與train_val相同處理的test_val 相反,您正在使用train_val進行訓練,但使用test作為您的newdata

如果您對test_val表進行預測,那么 svm 和隨機森林模型都可以工作,並會為您提供 177 個預測。

您還需要將submission的 data.frame 更改為 177 行而不是 418 行。

編輯正如評論中所討論的(盡管它們現在已被刪除?),您想使用基於train數據的 model 來預測test數據。

嘗試這個:

svm.model.linear <- svm(Survived ~ ., data = train, kernel="linear", cost = 2, gamma = 0.1)
svm.prediction.linear <- predict(svm.model.linear, test[,-1])

對於 R 中的不同模型, predict function 的工作方式略有不同,這可能會導致混淆。 當您將它與 svm model 一起使用時,它實際上是在調用 predict.svm predict.svm() 這個特殊的 function 不喜歡您將newdata與空的Survived列一起傳遞。 如果您通過指定newdata=test[,-1]刪除該列,則預測將按預期工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM