由于需要对数据进行分类,进一步进行判别,故对数据进行SVM和贝叶斯判别分析。
if(nnn==0) {
sink("svm.html")
library(ISLR)
library(e1071)
head(Auto)
summary(Auto)
mpg_median = median(Auto$mpg)
Auto$mpg_flag = as.factor(ifelse(Auto$mpg > mpg_median, 1, 0))
tune.out= tune(svm, mpg_flag~., data = Auto, kernel = "linear", ranges = list(cost = c(0.01,0.1,1,5,10)), scale = TRUE)
summary(tune.out)
best.svm = tune.out$best.model
Auto.pred = predict(best.svm,newdata = Auto)
matrix_pred = matrix(table(true = Auto[,"mpg_flag"],pred = Auto.pred),ncol = 2)
sum(diag(matrix_pred)) / nrow(Auto)
jpeg(file="svm-1.jpg")
plot(best.svm,Auto,mpg~weight)
tune.out= tune(svm, mpg_flag~., data = Auto, kernel = "radial", ranges = list(cost = c(0.1,1,10,100,1000),gamma = c(0.5,1,2,3,4)),scale = TRUE)
dev.off();jpeg(file="svm-2.jpg")
summary(tune.out)
best.svm = tune.out$best.model
summary(best.svm)
Auto.pred = predict(best.svm, newdata = Auto)
matrix_pred = matrix(table(true = Auto[,"mpg_flag"], pred = Auto.pred), ncol = 2)
sum(diag(matrix_pred)) / nrow(Auto)
plot(best.svm, Auto, mpg~weight)
dev.off()
sink()
} else {
sink("svm.html")
library(ISLR)
library(e1071)
head(Auto)
summary(Auto)
data=newdata
jpeg(file="svm-1.jpg")
tune.out= tune(svm, data[,aaa2]~., data = data, kernel = "linear", ranges = list(cost = c(0.01,0.1,1,5,10)), scale = TRUE)
summary(tune.out)
best.svm = tune.out$best.model
Auto.pred = predict(best.svm,newdata =newdata)
matrix_pred = matrix(table(true = newdata[,"y"],pred = Auto.pred ),ncol = 2)
sum(diag(matrix_pred)) / nrow(newdata)
plot(best.svm,newdata,data[,aaa2]~.)
dev.off()
sink()
}
if(nnn==0) {
sink("svm.html")
library(ISLR)
library(e1071)
head(Auto)
summary(Auto)
mpg_median = median(Auto$mpg)
Auto$mpg_flag = as.factor(ifelse(Auto$mpg > mpg_median, 1, 0))
tune.out= tune(svm, mpg_flag~., data = Auto, kernel = "linear", ranges = list(cost = c(0.01,0.1,1,5,10)), scale = TRUE)
summary(tune.out)
best.svm = tune.out$best.model
Auto.pred = predict(best.svm,newdata = Auto)
matrix_pred = matrix(table(true = Auto[,"mpg_flag"],pred = Auto.pred),ncol = 2)
sum(diag(matrix_pred)) / nrow(Auto)
jpeg(file="svm-1.jpg")
plot(best.svm,Auto,mpg~weight)
tune.out= tune(svm, mpg_flag~., data = Auto, kernel = "radial", ranges = list(cost = c(0.1,1,10,100,1000),gamma = c(0.5,1,2,3,4)),scale = TRUE)
dev.off();jpeg(file="svm-2.jpg")
summary(tune.out)
best.svm = tune.out$best.model
summary(best.svm)
Auto.pred = predict(best.svm, newdata = Auto)
matrix_pred = matrix(table(true = Auto[,"mpg_flag"], pred = Auto.pred), ncol = 2)
sum(diag(matrix_pred)) / nrow(Auto)
plot(best.svm, Auto, mpg~weight)
dev.off()
sink()
} else {
sink("svm.html")
library(ISLR)
library(e1071)
head(Auto)
summary(Auto)
data=newdata
jpeg(file="svm-1.jpg")
tune.out= tune(svm, data[,aaa2]~., data = data, kernel = "linear", ranges = list(cost = c(0.01,0.1,1,5,10)), scale = TRUE)
summary(tune.out)
best.svm = tune.out$best.model
Auto.pred = predict(best.svm,newdata =newdata)
matrix_pred = matrix(table(true = newdata[,"y"],pred = Auto.pred ),ncol = 2)
sum(diag(matrix_pred)) / nrow(newdata)
plot(best.svm,newdata,data[,aaa2]~.)
dev.off()
sink()
}