我正在开发一个程序,我使用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&lt; - predict(Prediction_Weather_Tuned,weather.test)
UseMethod(“预测”)中的错误: 没有适用于“预测”的方法适用于“因子”类的对象
答案 0 :(得分:0)
Prediction_Weather_Tuned
不是经过培训的SVM。
错误的变量类型会重新考虑您的代码,并确保使用正确的变量。您希望使用SVM进行预测,而不是使用较早的预测进行预测。