R语言中使用支持向量机

R语言中使用支持向量机


Including the SVM package

The SVM package is in a package called "e1071." Firt you need to set the path to include the directory where the e1071 package is. For example, if e1071 is in the subdirectory R-packages of your home directory:
> export R_LIB=~/R-packages
Then you have to install and include it
> install.packages("e1071")
> library("e1071")

The usage

You can use
> ?svm
to see the help information of the interface.

A simple example: the XOR problem

Assign four points to a four by two matrix called x
> x <- array(data = c(0,0,1,1,0,1,0,1),dim=c(4,2))
Assign labels of four data
> y <- factor(c(1,-1,-1,1))
Training the problem using support vector classification
> model <- svm(x,y,type="C-classification")
Test the original data again using the output model
> predict(model,x)

Three useful examples

In the following there are three more complicated examples which are from the  document of this interface :
  • Support vector classification (with the famous iris dataset)
  • Support vector regression (approximate the log function)
  • Density estimation
> example("svm")

svm> data(iris)

svm> attach(iris)

svm> model <- svm(Species ~ ., data = iris)
*
optimization finished, #iter = 96
nu = 0.046487
obj = -2.403411, rho = 0.041035
nSV = 11, nBSV = 2
*
optimization finished, #iter = 36
nu = 0.038898
obj = -1.945146, rho = 0.167765
nSV = 10, nBSV = 0
*
optimization finished, #iter = 63
nu = 0.293571
obj = -21.377493, rho = 0.143712
nSV = 33, nBSV = 26
Total nSV = 45

svm> x <- subset(iris, select = -Species)

svm> y <- Species

svm> model <- svm(x, y)
*
optimization finished, #iter = 96
nu = 0.046487
obj = -2.403411, rho = 0.041035
nSV = 11, nBSV = 2
*
optimization finished, #iter = 36
nu = 0.038898
obj = -1.945146, rho = 0.167765
nSV = 10, nBSV = 0
*
optimization finished, #iter = 63
nu = 0.293571
obj = -21.377493, rho = 0.143712
nSV = 33, nBSV = 26
Total nSV = 45

svm> print(model)

Call:
 svm.default(x = x, y = y) 

Parameters:
   SVM-Type:  C-classification 
 SVM-Kernel:  radial 
       cost:  1 
     degree:  3 
      gamma:  0.25 
     coef.0:  0 
         nu:  0.5 
    epsilon:  0.5 
       cost:  1 


Number of Support Vectors:  45  ( 7 19 19 )


Number of Classes:  3 

Levels:
 setosa versicolor virginica 

Rho:
 0.04103499 0.1677649 0.1437119 


svm> summary(model)

Call:
 svm.default(x = x, y = y) 

Parameters:
   SVM-Type:  C-classification 
 SVM-Kernel:  radial 
       cost:  1 
     degree:  3 
      gamma:  0.25 
     coef.0:  0 
         nu:  0.5 
    epsilon:  0.5 
       cost:  1 


Number of Support Vectors:  45  ( 7 19 19 )


Number of Classes:  3 

Levels:
 setosa versicolor virginica 

Rho:
 0.04103499 0.1677649 0.1437119 

Support Vectors:
    Sepal.Length Sepal.Width Petal.Length Petal.Width
14           4.3         3.0          1.1         0.1
16           5.7         4.4          1.5         0.4
19           5.7         3.8          1.7         0.3
24           5.1         3.3          1.7         0.5
25           4.8         3.4          1.9         0.2
42           4.5         2.3          1.3         0.3
45           5.1         3.8          1.9         0.4
51           7.0         3.2          4.7         1.4
53           6.9         3.1          4.9         1.5
55           6.5         2.8          4.6         1.5
57           6.3         3.3          4.7         1.6
58           4.9         2.4          3.3         1.0
61           5.0         2.0          3.5         1.0
64           6.1         2.9          4.7         1.4
67           5.6         3.0          4.5         1.5
69           6.2         2.2          4.5         1.5
71           5.9         3.2          4.8         1.8
73           6.3         2.5          4.9         1.5
77           6.8         2.8          4.8         1.4
78           6.7         3.0          5.0         1.7
79           6.0         2.9          4.5         1.5
84           6.0         2.7          5.1         1.6
85           5.4         3.0          4.5         1.5
86           6.0         3.4          4.5         1.6
87           6.7         3.1          4.7         1.5
99           5.1         2.5          3.0         1.1
101          6.3         3.3          6.0         2.5
102          5.8         2.7          5.1         1.9
107          4.9         2.5          4.5         1.7
111          6.5         3.2          5.1         2.0
119          7.7         2.6          6.9         2.3
120          6.0         2.2          5.0         1.5
122          5.6         2.8          4.9         2.0
124          6.3         2.7          4.9         1.8
127          6.2         2.8          4.8         1.8
128          6.1         3.0          4.9         1.8
130          7.2         3.0          5.8         1.6
132          7.9         3.8          6.4         2.0
134          6.3         2.8          5.1         1.5
135          6.1         2.6          5.6         1.4
139          6.0         3.0          4.8         1.8
142          6.9         3.1          5.1         2.3
147          6.3         2.5          5.0         1.9
148          6.5         3.0          5.2         2.0
150          5.9         3.0          5.1         1.8


Coefficiants:
              [,1]        [,2]
 [1,]  0.000000000  0.01017126
 [2,]  0.434080723  0.95574579
 [3,]  0.063057430  0.00000000
 [4,]  0.005863633  0.00000000
 [5,]  0.341475825  0.01149015
 [6,]  1.000000000  0.96747404
 [7,]  0.479896333  0.00000000
 [8,] -0.567881290  0.00000000
 [9,]  0.000000000  1.00000000
[10,]  0.000000000  1.00000000
[11,]  0.000000000  1.00000000
[12,] -0.541206839  0.00000000
[13,]  0.000000000  0.40490500
[14,]  0.000000000  1.00000000
[15,]  0.000000000  1.00000000
[16,]  0.000000000  1.00000000
[17,]  0.000000000  1.00000000
[18,]  0.000000000  1.00000000
[19,]  0.000000000  1.00000000
[20,] -0.141083347  1.00000000
[21,]  0.000000000  1.00000000
[22,] -0.074202468  1.00000000
[23,]  0.000000000  1.00000000
[24,]  0.000000000  0.27363722
[25,]  0.000000000  1.00000000
[26,] -1.000000000  0.00000000
[27,] -0.118961508  0.00000000
[28,]  0.000000000 -0.31890447
[29,] -0.731975036 -1.00000000
[30,]  0.000000000 -1.00000000
[31,] -0.427405462  0.00000000
[32,] -0.101852870 -1.00000000
[33,]  0.000000000 -1.00000000
[34,]  0.000000000 -1.00000000
[35,]  0.000000000 -1.00000000
[36,]  0.000000000 -1.00000000
[37,]  0.000000000 -0.82788343
[38,] -0.405831306 -0.13176137
[39,]  0.000000000 -1.00000000
[40,]  0.000000000 -0.62555212
[41,]  0.000000000 -1.00000000
[42,] -0.158855069 -0.77444084
[43,]  0.000000000 -1.00000000
[44,]  0.000000000 -1.00000000
[45,]  0.000000000 -1.00000000



svm> pred <- predict(model, x)

svm> table(pred, y)
            y
pred         setosa versicolor virginica
  setosa         50          0         0
  versicolor      0         48         0
  virginica       0          2        50

svm> x <- seq(0.1, 5, by = 0.05)

svm> y <- log(x) + rnorm(x, sd = 0.2)

svm> m <- svm(x, y)
*
optimization finished, #iter = 12
nu = 0.040404
obj = -2.511836, rho = -0.359518
nSV = 6, nBSV = 2

svm> new <- predict(m, x)

svm> plot(x, y)

svm> points(x, log(x), col = 2)

svm> points(x, new, col = 4)

svm> X <- data.frame(a = rnorm(1000), b = rnorm(1000))

svm> attach(X)

svm> m <- svm(X)
*
optimization finished, #iter = 266
obj = 26836.184233, rho = 128.900207
nSV = 503, nBSV = 498

svm> m <- svm(~a + b)
*
optimization finished, #iter = 266
obj = 26836.184233, rho = 128.900207
nSV = 503, nBSV = 498

svm> m <- svm(~., data = X)
*
optimization finished, #iter = 266
obj = 26836.184233, rho = 128.900207
nSV = 503, nBSV = 498

svm> predict(m, t(c(0, 0)))
[1] FALSE

svm> predict(m, t(c(4, 4)))
[1] FALSE

svm> plot(X)

svm> points(X[m$index, ], col = 2)
> 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值