Caret之Svm支持向量机
Posted OpeSource
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Caret之Svm支持向量机相关的知识,希望对你有一定的参考价值。
本节主要介绍利用R进行Svm支持向量机分类,案例数据采用“german_credit”信息。
[cut] Read more
library(caret)
library(pROC)
library(ROCR)
wd<-"G:/16支持向量/"
setwd(wd)
df<-read.csv("./german_credit_tra.csv",
header=T,
stringsAsFactors = FALSE)
german_credit<-as.data.frame(
apply(df,2,as.numeric))
str(german_credit)
attach(german_credit)
german_credit$Creditability<-as.factor(
german_credit$Creditability)
levels(german_credit$Creditability) <- list(
no="0", yes="1") inTrain<-createDataPartition(
german_credit$Creditability,
p=0.70,list=FALSE)
trainData<-german_credit[inTrain,]
testData<-german_credit[-inTrain,]
trainClass<-trainData$Creditability
testClass<-testData$Creditability
str(trainData)
set.seed(1)
control <- trainControl( method = 'repeatedcv', number = 10, repeats = 3, classProbs = TRUE, summaryFunction = twoClassSummary)
#####001svm radial model 高斯核
require(kernlab)
#宁可错杀不可放过,让本来信用不好的误判为信用好的例数减少
tuneRadial <- expand.grid(.sigma=c(0.1,0.2), .C=c(0.1,0.3))
svm.model = train(Creditability ~ AccountBalance + Duration +
PaymentStatusofPreviousCredit +Purpose +
ValueSavings,
data= trainData, method = "svmRadial", metric = "ROC", trControl = control,
tuneGrid = tuneRadial)
summary(svm.model$finalModel)
svm.model$bestTune
svm.model$finalModel
svmImp<-varImp(svm.model,scale=FALSE)
svmImpprediction <- predict(svm.model,newdata=testData)
confusionMatrix(prediction, testClass)
#####002svm linear model 线性核
tuneLinear <- expand.grid(.C=c(0.3,0.5,0.7))
svm.model2 = train(Creditability ~ AccountBalance + Duration +
PaymentStatusofPreviousCredit +Purpose +
ValueSavings,
data= trainData, method = "svmLinear", metric = "ROC", trControl = control,
tuneGrid = tuneLinear)
summary(svm.model2$finalModel)
svm.model2$bestTune
svm.model2$finalModel
svmImp2<-varImp(svm.model2,scale=FALSE)
svmImp2prediction2 <- predict(svm.model2,newdata=testData)
confusionMatrix(prediction2, testClass)
detach(german_credit)
#混淆矩阵输出结果 Reference
Prediction no yes no 38 25 yes 52 185
Accuracy : 0.7433
以上是关于Caret之Svm支持向量机的主要内容,如果未能解决你的问题,请参考以下文章
R语言使用caret包构建线性核的支持向量机SVM(Support Vector Machines with Linear Kernel )构建回归模型通过method参数指定算法名称