R 中的支持向量机
Kailash Awati
Instructor
library(ggplot2)
# 按物种着色,绘制花瓣长度与宽度散点图
p <- ggplot(data = iris,
aes(x = Petal.Width,
y = Petal.Length,
color = Species)) +
geom_point()
# 显示图形
p

library(e1071)
# 训练模型
svm_model <- svm(Species ~ .,
data = trainset,
type = "C-classification",
kernel = "linear")
pred_train <- predict(svm_model, trainset)
mean(pred_train == trainset$Species)
0.9756098
pred_test <- predict(svm_model, testset)
mean(pred_test == testset$Species)
0.962963
R 中的支持向量机