image
svm(formula,data=NULL,…,subset,na.action=na.omit,sacle=TRUE)
或者
svm(x, y = NULL, scale = TRUE, type = NULL, kernel =
"radial", degree = 3, gamma = if(is.vector(x)) 1 else 1 / ncol(x),
coef0 = 0, cost = 1, nu = 0.5,
class.weights = NULL, cachesize = 40, tolerance =0.001, epsilon = 0.1,
shrinking = TRUE, cross = 0, probability = FALSE,fitted = TRUE,
..., subset, na.action = na.omit)
主要参数说明如下:
> if(!suppressWarnings(require(e1071)))
+ {
+ install.packages('e1071')
+ require(e1071)
+ }
> setwd("E:\\Rwork")
> data(iris)
>
> ir<-iris
>
> set.seed(123)
>
> index <- sample(nrow(ir),0.75*nrow(ir))
> data_train <- ir[index,]
> data_test <- ir[-index,]
> sv<-svm(Species~.,data=data_train,cross=5,type='C-classification',kernel='sigmoid')
>
> summary(sv) #查看支持向量机sv的具体信息,发现做5倍交叉验证的正确率为92%
Call:
svm(formula = Species ~ ., data = data_train, cross = 5, type = "C-classification", kernel = "sigmoid")
Parameters:
SVM-Type: C-classification
SVM-Kernel: sigmoid
cost: 1
gamma: 0.25
coef.0: 0
Number of Support Vectors: 47
( 6 19 22 )
Number of Classes: 3
Levels:
setosa versicolor virginica
5-fold cross-validation on training data:
Total Accuracy: 88.39286
Single Accuracies:
81.81818 95.45455 91.30435 86.36364 86.95652
>
>
> pre<-predict(sv,data_test)#对测试样本作预测。pre是一个类别向量。
>
> dim(data_test[data_test$Species!=pre,])[1]/dim(data_test)[1]#计算错误率
[1] 0.1315789
> table(pre,data_test$Species)
pre setosa versicolor virginica
setosa 11 0 0
versicolor 0 10 2
virginica 0 3 12
plot(sv, iris, Petal.Width ~
+
+ Petal.Length, slice = list(Sepal.Width = 3,
+
+ Sepal.Length = 4))