R语言中使用支持向量机
来源:互联网 发布:非农数据网址 编辑:程序博客网 时间:2024/05/02 02:14
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-packagesThen you have to install and include it
> install.packages("e1071")> library("e1071")
The usage
You can use> ?svmto 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 = 96nu = 0.046487obj = -2.403411, rho = 0.041035nSV = 11, nBSV = 2*optimization finished, #iter = 36nu = 0.038898obj = -1.945146, rho = 0.167765nSV = 10, nBSV = 0*optimization finished, #iter = 63nu = 0.293571obj = -21.377493, rho = 0.143712nSV = 33, nBSV = 26Total nSV = 45svm> x <- subset(iris, select = -Species)svm> y <- Speciessvm> model <- svm(x, y)*optimization finished, #iter = 96nu = 0.046487obj = -2.403411, rho = 0.041035nSV = 11, nBSV = 2*optimization finished, #iter = 36nu = 0.038898obj = -1.945146, rho = 0.167765nSV = 10, nBSV = 0*optimization finished, #iter = 63nu = 0.293571obj = -21.377493, rho = 0.143712nSV = 33, nBSV = 26Total nSV = 45svm> 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.Width14 4.3 3.0 1.1 0.116 5.7 4.4 1.5 0.419 5.7 3.8 1.7 0.324 5.1 3.3 1.7 0.525 4.8 3.4 1.9 0.242 4.5 2.3 1.3 0.345 5.1 3.8 1.9 0.451 7.0 3.2 4.7 1.453 6.9 3.1 4.9 1.555 6.5 2.8 4.6 1.557 6.3 3.3 4.7 1.658 4.9 2.4 3.3 1.061 5.0 2.0 3.5 1.064 6.1 2.9 4.7 1.467 5.6 3.0 4.5 1.569 6.2 2.2 4.5 1.571 5.9 3.2 4.8 1.873 6.3 2.5 4.9 1.577 6.8 2.8 4.8 1.478 6.7 3.0 5.0 1.779 6.0 2.9 4.5 1.584 6.0 2.7 5.1 1.685 5.4 3.0 4.5 1.586 6.0 3.4 4.5 1.687 6.7 3.1 4.7 1.599 5.1 2.5 3.0 1.1101 6.3 3.3 6.0 2.5102 5.8 2.7 5.1 1.9107 4.9 2.5 4.5 1.7111 6.5 3.2 5.1 2.0119 7.7 2.6 6.9 2.3120 6.0 2.2 5.0 1.5122 5.6 2.8 4.9 2.0124 6.3 2.7 4.9 1.8127 6.2 2.8 4.8 1.8128 6.1 3.0 4.9 1.8130 7.2 3.0 5.8 1.6132 7.9 3.8 6.4 2.0134 6.3 2.8 5.1 1.5135 6.1 2.6 5.6 1.4139 6.0 3.0 4.8 1.8142 6.9 3.1 5.1 2.3147 6.3 2.5 5.0 1.9148 6.5 3.0 5.2 2.0150 5.9 3.0 5.1 1.8Coefficiants: [,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.00000000svm> pred <- predict(model, x)svm> table(pred, y) ypred setosa versicolor virginica setosa 50 0 0 versicolor 0 48 0 virginica 0 2 50svm> 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 = 12nu = 0.040404obj = -2.511836, rho = -0.359518nSV = 6, nBSV = 2svm> 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 = 266obj = 26836.184233, rho = 128.900207nSV = 503, nBSV = 498svm> m <- svm(~a + b)*optimization finished, #iter = 266obj = 26836.184233, rho = 128.900207nSV = 503, nBSV = 498svm> m <- svm(~., data = X)*optimization finished, #iter = 266obj = 26836.184233, rho = 128.900207nSV = 503, nBSV = 498svm> predict(m, t(c(0, 0)))[1] FALSEsvm> predict(m, t(c(4, 4)))[1] FALSEsvm> plot(X)svm> points(X[m$index, ], col = 2)>
0 0
- R语言中使用支持向量机
- R语言支持向量机SVM
- R语言调整支持向量机
- R语言实践之支持向量机
- R语言使用支持向量机完成数据分类(SVM)
- R语言svm支持向量机多元回归预测
- R语言svm支持向量机分类与参数优化
- 在R中使用支持向量机(SVM)进行数据挖掘(上)
- 在R中使用支持向量机(SVM)进行数据挖掘(下)
- R - SVM 支持向量机
- R--SVM支持向量机
- malab中支持向量机使用
- R语言中paste()中粘贴向量
- 支持向量机(中)
- 支持向量机及其R实现
- 支持向量机的R实现
- R语言与机器学习学习笔记(分类算法)(4)支持向量机
- R语言与机器学习学习笔记(分类算法)(4)支持向量机
- EL表达式个人总结
- 人品计算器
- 第十三周项目4——数组的排列(选择法)
- LeetCode[Array]: Search a 2D Matrix
- (实验五)《数据结构》第五章 树和二叉树实验
- R语言中使用支持向量机
- XSL 语言@等通配符,XPath等的说明
- 关于非对称加密算法中的公钥加密的用法的一些个人看法
- 黑马程序员--反射机制
- 第十四周项目3多科成绩单
- 用css让头像变圆且鼠标悬停在图片上时会发光
- 第十四周 项目三--多科成绩单(3.1&3.2)
- 第一篇汇编文章(基于8086)
- Android定时自动启动应用程序