ggplot으로 서포트 벡터 시각화하기
이 연습 문제에서는 선형 SVM을 학습할 때 사용한 훈련 데이터셋을 그린 뒤, 서포트 벡터를 표시해 볼 거예요. 훈련 데이터셋은 데이터 프레임 trainset에 미리 로드되어 있고, SVM 모델은 변수 svm_model에 저장되어 있어요.
이 연습은 강의의 일부입니다
R에서의 Support Vector Machine
연습 안내
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