시작하기무료로 시작하기

`ggplot2`로 결정 경계와 마진 시각화하기

이 연습에서는 이전 연습에서 만든 서포트 벡터 산점도에 결정 경계와 마진 경계를 추가해 보겠습니다. SVM 모델은 변수 svm_model에, 가중치 벡터는 미리 계산되어 변수 w에 들어 있습니다. ggplot2 라이브러리도 미리 로드되어 있습니다.

이 연습은 강의의 일부입니다

R에서의 Support Vector Machine

강의 보기

연습 안내

  • 결정 경계의 기울기와 절편을 계산하세요.
  • 결정 경계를 그래프에 추가하세요.
  • 마진 경계를 그래프에 추가하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

#calculate slope and intercept of decision boundary from weight vector and svm model
slope_1 <- -___/w[2]
intercept_1 <- ___$rho/w[2]

#build scatter plot of training dataset
scatter_plot <- ggplot(data = trainset, aes(x = x1, y = x2, color = y)) + 
    geom_point() + scale_color_manual(values = c("red", "blue"))
#add decision boundary
plot_decision <- scatter_plot + ___(slope = ___, intercept = ___) 
#add margin boundaries
plot_margins <- plot_decision + 
 ___(slope = ___, intercept = ___ - 1/w[2], linetype = "dashed")+
 ___(slope = ___, intercept = ___ + 1/w[2], linetype = "dashed")
#display plot
plot_margins
코드 편집 및 실행