开始使用免费开始使用

使用 ggplot 可视化支持向量

本练习中,您将绘制用于构建线性 SVM 的训练数据集,并标注出支持向量。训练数据集已预加载在数据框 trainset 中,SVM 模型保存在变量 svm_model 中。

本练习是课程的一部分

R 中的支持向量机

查看课程

练习说明

  • 加载 ggplot2
  • 绘制训练数据集。
  • 使用 SVM 模型中的索引,在图中标注支持向量。

交互式实操练习

通过完成这段示例代码来试试这个练习。

#load ggplot
library(ggplot2)

#build scatter plot of training dataset
scatter_plot <- ggplot(data = ___, aes(x = x1, y = x2, color = y)) + 
    geom_point() + 
    scale_color_manual(values = c("red", "blue"))
 
#add plot layer marking out the support vectors 
layered_plot <- 
    scatter_plot + geom_point(data = trainset[svm_model$___, ], aes(x = x1, y = x2), color = "purple", size = 4, alpha = 0.5)

#display plot
layered_plot
编辑并运行代码