방사형으로 분리 가능한 데이터셋 생성

R에서의 Support Vector Machine

Kailash Awati

Instructor

2차원 균일 분포 포인트 집합 생성

  • 200개의 데이터 포인트 생성
    • 예측 변수 x1과 x2: -1과 1 사이의 균일 분포.
# Set required number of datapoints
n <- 200
# Set seed to ensure reproducibility
set.seed(42)

# Generate dataframe with 2 predictors x1 and x2 in (-1, 1)
df <- data.frame(x1 = runif(n, min = -1, max = 1),
                 x2 = runif(n, min = -1, max = 1))
R에서의 Support Vector Machine

원형 경계 생성

  • 반지름 0.7의 원형 결정 경계 생성.
  • 범주형 변수 y는 경계 외부는 +1, 내부는 -1.
radius <- 0.7
radius_squared <- radius ^ 2

#categorize data points depending on location wrt boundary
df$y <- factor(ifelse(df$x1 ^ 2 + df$x2 ^ 2 < radius_squared, -1, 1),
               levels = c(-1, 1))
R에서의 Support Vector Machine

데이터셋 시각화

  • ggplot으로 시각화.
library(ggplot2)
  • 예측 변수를 2개 축에 표시; 클래스는 색상으로 구분.
# Build plot
p <- ggplot(data = df, aes(x = x1, y = x2, color = y)) + 
     geom_point() + 
     scale_color_manual(values = c("-1" = "red", "1" = "blue")) 
# Display plot 
p
R에서의 Support Vector Machine

3장 1절 - 방사형 분리 가능 데이터셋

R에서의 Support Vector Machine

원형 경계 추가 - 1부

  • 원을 생성하는 함수 작성
# Function generates dataframe with points
# lying on a circle of radius r
circle <- 
  function(x1_center, x2_center, r, npoint = 100) {

  # Angular spacing of 2*pi/npoint between points
  theta <- seq(0, 2 * pi, length.out = npoint)
  x1_circ <- x1_center + r * cos(theta)
  x2_circ <- x2_center + r * sin(theta)

  data.frame(x1c = x1_circ, x2c = x2_circ)
}
R에서의 Support Vector Machine

원형 경계 추가 - 2부

  • 플롯에 경계 추가하기:
    • circle() 함수로 경계 생성.
    • geom_path()로 경계를 플롯에 추가.
# Generate boundary
boundary <- circle(x1_center = 0,
                   x2_center = 0,
                   r = radius)
# Add boundary to previous plot
p <- p + 
     geom_path(data = boundary,
               aes(x = x1c, y = x2c),
               inherit.aes = FALSE)
# Display plot
p
R에서의 Support Vector Machine

3장 1절 - 결정 경계가 있는 방사형 분리 가능 데이터셋

R에서의 Support Vector Machine

연습할 시간입니다!

R에서의 Support Vector Machine

Preparing Video For Download...