支持向量机

支持向量机(Support Vector Machine, SVM)是统计机器学习和数据挖掘中常用的一种分类模型。

  • 从逻辑回归到线性分类与非线性分类
> install.packages("e1071", dep = TRUE, type = "source")  # 正确的安装软件包
> iris[1:5,]  # datasets软件包中的iris数据集
 # 正确的安装软件包
> iris[1:5,]  # datasets软件包中的iris数据集
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa

> library(lattice)
> xyplot(Petal.Length ~ Petal.Width, data = iris, groups = Species, auto.key = list(corner = c(1, 0)))  # 简单地线性图初步判定数据的分布情况。


 # 简单地线性图初步判定数据的分布情况。




> data(iris)
> attach(iris)
> subdata <- iris[iris$Species != 'virginica',]
> subdata$Species <- factor(subdata$Species)
> model1 <- svm(Species ~ Petal.Length + Petal.Width, data = subdata)
> plot(model1, subdata, Petal.Length ~ Petal.Width)

> model2 <- svm(Species ~ ., data = iris)  # .代替全部特征变量 # .代替全部特征变量
> summary(model2)  # 显示模型构建的情况

Call:
svm(formula = Species ~ ., data = iris)


Parameters:
   SVM-Type:  C-classification 
 SVM-Kernel:  radial 
       cost:  1 
      gamma:  0.25 

Number of Support Vectors:  51

 ( 8 22 21 )


Number of Classes:  3 

Levels: 
 setosa versicolor virginica # 显示模型构建的情况

Call:
svm(formula = Species ~ ., data = iris)


Parameters:
   SVM-Type:  C-classification 
 SVM-Kernel:  radial 
       cost:  1 
      gamma:  0.25 

Number of Support Vectors:  51

 ( 8 22 21 )


Number of Classes:  3 

Levels: 
 setosa versicolor virginica
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)

上述为svm函数的主要格式,x可以是一个数据矩阵或稀疏矩阵或数据向量,y是对于x数据的结果标签,可以是字符变量或数值变量。type指定建立模型的类别,通常有分类模型,回归模型或者异常检测模型。kernel是指在模型建立过程中使用的核函数,有线性核函数,多项式核函数,高斯核函数和神经网络函数等。

> x = iris[,-5]
> y = iris[,5]
> model3 = svm(x,y, kenel = "radial",gamma = if (is.vector(x)) 1 else 1 / ncol(x))  # 如果特征向量是向量则ganma=1,否则gamma值为特征向量个数的倒数。
> pred <- predict(model3, x)
> table(pred, y)
            y
pred         setosa versicolor virginica
  setosa         50          0         0
  versicolor      0         48         2
  virginica       0          2        48 # 如果特征向量是向量则ganma=1,否则gamma值为特征向量个数的倒数。
> pred <- predict(model3, x)
> table(pred, y)
            y
pred         setosa versicolor virginica
  setosa         50          0         0
  versicolor      0         48         2
  virginica       0          2        48
> pred <- predict(model3, x, decision.values = T)  # 预测分类
> plot(cmdscale(dist(iris[,-5])), col = c("Orange", "blue", "green")[as.integer(iris[,5])], pch = c("o","+")[1:150 %in% model3$index + 1])
> legend(1.8, -0.8, c("Setosa", "Versicolor", "virgincia"), col = c("Orange", "blue", "green"), lty = 1 ) # 预测分类
> plot(cmdscale(dist(iris[,-5])), col = c("Orange", "blue", "green")[as.integer(iris[,5])], pch = c("o","+")[1:150 %in% model3$index + 1])
> legend(1.8, -0.8, c("Setosa", "Versicolor", "virgincia"), col = c("Orange", "blue", "green"), lty = 1 )


图中“+”表示的是支持向量,“0”表示的是普通的样本点。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值