关注微信公共号:小程在线
关注CSDN博客:程志伟的博客
R版本:3.6.1
e1701包:用于支持向量机模型
SVM函数:利用数据构建支持向量机模型
> library('e1071')
Warning message:
程辑包‘e1071’是用R版本3.6.2 来建造的
> setwd('G:\\R语言\\大三下半年\\数据挖掘:R语言实战\\')
> data("iris")
> summary(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width
Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
Median :5.800 Median :3.000 Median :4.350 Median :1.300
Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
Species
setosa :50
versicolor:50
virginica :50
> ######################### 应用案例 ######################
#第一种格式
> data("iris")
> model <- svm(Species ~ . ,data = iris)
> summary(model)
Call:
svm(formula = Species ~ ., data = iris)
Parameters:
SVM-Type: C-classification
SVM-Kernel: radial
cost: 1
Number of Support Vectors: 51
( 8 22 21 )
Number of Classes: 3
Levels:
setosa versicolor virginica
#第二种格式
> x=iris[,-5]
> y=iris[,5]
> model <- svm(x,y,kernel = "radial", gamma = if(is.vector(x)) 1 else 1/ncol(x))
> summary(model)
Call:
svm.default(x = x, y = y, kernel = "radial", gamma = if (is.vector(x)) 1 else 1/ncol(x))
Parameters:
SVM-Type: C-classification
SVM-Kernel: radial
cost: 1
Number of Support Vectors: 51
( 8 22 21 )
Number of Classes: 3
Levels:
setosa versicolor virginica
#预测判别
> x=iris[,1:4]#确认需要进行预测的样本特征矩阵
> prd=predict(model,x)#根据模型model对x数据进行yuce
> prd[sample(1:150,5)]#随机挑选8个预测结果进行展示
97 16 38 100 4
versicolor setosa setosa versicolor setosa
Levels: setosa versicolor virginica
#模型预测精度展示
> table(prd,y)
y
prd setosa versicolor virginica
setosa 50 0 0
versicolor 0 48 2
virginica 0 2 48
#综合建模
> attach(iris)#将数据集iris按列单独确认为向量
> x=subset(iris,select=-Species)#除去Species
> y=Species
> type=c("C-classification","nu-classification","one-classification")#确定要使用的分类方式
> kernel=c("linear","polynomial","radial","sigmoid")#去诶的那个要使用的核函数
> pred=array(0,dim=c(150,3,4))#初始化预测结果矩阵的三维长度为150,3,4
> accuracy=matrix(0,3,4)#初始化模型精准度矩阵的两位分别为3,4
> yy=as.integer(y)#将结果变量数量化为1,2,3
> for(i in 1:3)#确认i影响的维度代表分类方式
+ {
+ for(j in 1:4)#确认j影响的维度代表和函数
+ {
+ pred[,i,j]=predict(svm(x,y,type=type[i],kernel=kernel[j]),x)#对每一模型进行预测
+ if(i>2) accuracy[i,j]=sum(pred[,i,j]!=1)
+ else accuracy[i,j]=sum(pred[,i,j]!=yy)
+ }
+ }
Warning messages:
1: In Ops.factor(yorig, ret$fitted) : ‘-’ not meaningful for factors
2: In Ops.factor(yorig, ret$fitted) : ‘-’ not meaningful for factors
3: In Ops.factor(yorig, ret$fitted) : ‘-’ not meaningful for factors
4: In Ops.factor(yorig, ret$fitted) : ‘-’ not meaningful for factors
#确定模型精度变量的列名和行名
#看到C-classification 和radial最小的组合为4
> dimnames(accuracy)=list(type,kernel)
> accuracy
linear polynomial radial sigmoid
C-classification 5 7 4 17
nu-classification 5 14 5 12
one-classification 102 75 76 75
> table(pred[,1,3],y)
y
setosa versicolor virginica
1 50 0 0
2 0 48 2
3 0 2 48
> plot(cmdscale(dist(iris[,-5])),
+ col=c("lightgray","black","gray")[as.integer(iris[,5])],
+ pch=c("o","+")[1:150 %in% model$index+1])#绘制模型分类散点图
> legend(2,-0.4,c("setosa","versicolor","virginica"),
+ col=c("lightgray","black","gray"),lty=1)#标记图例
#优化建模
> data(iris)
> wts=c(1,1,1)#确定模型的各个类别比重为1:1:1
> names(wts)=c("setosa","versicolor","virginica")#确定各个比重对应的类别
> model1=svm(x,y,class.weights=wts)#建立模型
#确定模型的各个类别比重为1:100:100
> wts=c(1,100,100)
> names(wts)=c("setosa","versicolor","virginica")#确定各个比重对应的类别
> model2=svm(x,y,class.weights=wts)
> pred2=predict(model2,x)#根据模型进行预测
> table(pred2,y)#展示预测结果
y
pred2 setosa versicolor virginica
setosa 50 0 0
versicolor 0 49 1
virginica 0 1 49
#确定模型各个类别的比重1:500:500
> wts=c(1,500,500)
> names(wts)=c("setosa","versicolor","virginica")#确定各个比重对应的类别
> model3=svm(x,y,class.weights =wts)
> pred3=predict(model3,x)
> table(pred3,y)#在实际构建模型时可以改变各类样本的权重比例来提高模型预测精度
y
pred3 setosa versicolor virginica
setosa 50 0 0
versicolor 0 50 0
virginica 0 0 50
|
请发表评论