【发布时间】:2018-09-28 05:26:29
【问题描述】:
我正在开发一个程序,我在其中使用 SVM 进行训练和测试,然后我必须绘制 ROC 曲线。它给我带来了一个我一直试图解决的错误。不幸的是,谷歌和 Stackoverflow 并没有太大帮助。 :( 任何帮助将不胜感激。
> head(weather.train)
我的数据集可以在上面的链接中找到。
library(e1071)
library(ROCR)
library(kernlab)
library(caret)
weather.train<-read.csv("weather.train.csv")
weather.test<-read.csv("weather.test.csv")
head(weather.train)
for(name in names(weather.test)){ # For every column,
if(is.factor(weather.test[[name]])) {# if it's a factor variable,
## change its set of *levels* (possible values)
## to that of the training set.
weather.test[[name]] <- factor(weather.test[[name]],
levels=levels(weather.train[[name]]))
}}
weather.train$Date <- NULL
weather.test$Date <- NULL
## train a support vector machine
svm_Train<-svm(RainTomorrow ~ .,data=weather.train,kernel = "radial",
cost=100,scale=F)
Prediction_Weather<- predict(svm_Train, weather.test)
Init<-table(truth = weather.test$RainTomorrow, prediction = Prediction_Weather)
confusionMatrix(Init)
#Tuning
best.tune(svm,RainTomorrow ~ ., data = weather.train,ranges = list(cost = 10^(-3:3), gamma = 10^(-3:3)))
svm_Train<-svm(RainTomorrow ~ .,data=weather.train,kernel = "radial",
cost=100,gamma=0.001,scale=F)
Prediction_Weather_Tuned<- predict(svm_Train, weather.test)
Accuracy<-table(truth = weather.test$RainTomorrow, prediction = Prediction_Weather)
confusionMatrix(Accuracy)
#Error is in the line. This part is for the ROC
svmPredict <- predict(Prediction_Weather_Tuned, weather.test)
pred <- prediction(attr(svmPredict,"probabilities")[,1], weather.test$RainTomorrow)
result <- performance(pred, "tpr", "fpr")
plot(result)
plot(svmPerf)
svmPredict
UseMethod("predict") 中的错误: 没有适用于“因素”类对象的“预测”方法
【问题讨论】:
-
你希望这条线能做什么?看起来
Prediction_Weather_Tuned是预测类的向量,您希望对这些预测类做什么? -
知道如何更改它吗?有什么建议会有所帮助吗?这就是我得到的:> class(Prediction_Weather_Tuned) [1] "factor"
-
我最好的猜测是你想做类似
predict(svm_Train, weather.test, probability = TRUE)的事情来获得预测的概率。否则,您需要尝试解释您要达到的目标。 -
我试过了,同样的错误仍然存在。这是我的作业问题:这项任务的目的是构建一个 SVM,以根据今天的天气状况预测明天的降雨(RainTomorrow)。绘制并比较测试集上默认分类器和调优分类器的 ROC 曲线。
标签: r machine-learning classification svm roc