“ ”有问题到淘宝找 大数据部落 就可以了。
> ## SVM in R
> ## classify cats into male / female by heart and body weight
> library(e1071)
> data(cats, package="MASS")
> inputData <- data.frame(cats[, c (2,3)],
+ response = as.factor(cats$Sex))
> ## linear kernel
> svmfit <- svm(response ~ ., data = inputData, kernel = "linear",
+ cost = 10, scale = FALSE)
> plot(svmfit, inputData)
> compareTable <- table (inputData$response, predict(svmfit))
> mean(inputData$response != predict(svmfit))
[1] 0.1944444
> compareTable
F M
F 33 14
M 14 83
> ## radial kernel
> svmfit <- svm(response ~ ., data = inputData, kernel = "radial",
+ cost = 10, scale = FALSE)
> plot(svmfit, inputData)
> compareTable <- table (inputData$response, predict(svmfit))
> mean(inputData$response != predict(svmfit))
[1] 0.1875
> ## radial kernel
> svmfit <- svm(response ~ ., data = inputData, kernel = "radial",
+ cost = 10, scale = FALSE)
> plot(svmfit, inputData)
> compareTable <- table (inputData$response, predict(svmfit))
> mean(inputData$response != predict(svmfit))
[1] 0.1875
>
> ## Prepare training and test data
> set.seed(100)
> rowIndices <- 1 : nrow(inputData)
> sampleSize <- 0.8 * length(rowIndices)
> trainingRows <- sample (rowIndices, sampleSize)
> trainingData <- inputData[trainingRows, ]
> testData <- inputData[-trainingRows, ]
> ## tune to select best gamma and cost
> tuned <- tune.svm(response ~., data = trainingData,
+ gamma = 10^(-6:1), cost = 10^(-1:2))
> summary(tuned)
Parameter tuning of 'svm':
- sampling method: 10-fold cross validation
- best parameters:
gamma cost
0.01 100
- best performance: 0.2439394
- Detailed performance results:
gamma cost error dispersion
1 0.000001 0.1 0.3325758 0.16269565
2 0.000010 0.1 0.3325758 0.16269565
3 0.000100 0.1 0.3325758 0.16269565
4 0.001000 0.1 0.3325758 0.16269565
5 0.010000 0.1 0.3325758 0.16269565