선형 분리 가능 데이터셋 생성

R에서의 Support Vector Machine

Kailash Awati

Instructor

학습 개요

  • SVM의 핵심 원리를 설명할 데이터셋을 생성합니다.
  • 두 개의 변수와 선형 결정 경계를 가진 데이터셋입니다.
R에서의 Support Vector Machine

runif()을 사용한 2차원 데이터셋 생성

  • 200개의 점으로 구성된 두 변수 데이터셋 생성
  • 변수 x1과 x2는 (0,1) 구간에서 균일 분포.
# Preliminaries...
# Set required number of data points
n <- 200
# Set seed to ensure reproducibility
set.seed(42)

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

두 클래스 생성

  • 직선 결정 경계 x1 = x2로 구분된 두 클래스 생성
  • 직선은 (0, 0)을 지나며 수평선과 45도 각도를 이룹니다
  • 경계 아래 점은 클래스 변수 y = -1, 위 점은 y = 1
# Classify points as -1 or +1
df$y <- factor(ifelse(df$x1 - df$x2 > 0, -1, 1),
               levels = c(-1, 1))
R에서의 Support Vector Machine

ggplot을 사용한 데이터셋 시각화

  • x축에 x1, y축에 x2를 배치한 2차원 산점도 생성
  • 클래스를 색으로 구분 (경계 아래 = 빨간색, 위 = 파란색)
  • 결정 경계는 x1 = x2: (0, 0)을 지나며 기울기 = 1
library(ggplot2)

# Build plot
p <- ggplot(data = df, aes(x = x1, y = x2, color = y)) + 
     geom_point() +
     scale_color_manual(values = c("-1" = "red", "1" = "blue")) +
     geom_abline(slope = 1, intercept = 0)

# Display it  
p
R에서의 Support Vector Machine

선형 분리 가능 데이터셋 챕터 1.2

R에서의 Support Vector Machine

마진 도입

  • 마진을 만들려면 경계 근처의 점을 제거해야 합니다
  • x1과 x2의 차이가 지정값 미만인 점을 제거합니다
# Create a margin of 0.05 in dataset
delta <- 0.05
# Retain only those points that lie outside the margin
df1 <- df[abs(df$x1 - df$x2) > delta, ]
# Check number of data points remaining
nrow(df1)

# Replot dataset with margin (code is exactly same as before) p <- ggplot(data = df1, aes(x = x1, y = x2, color = y)) + geom_point() + scale_color_manual(values = c("red", "blue")) + geom_abline(slope = 1, intercept = 0) # Display plot p
R에서의 Support Vector Machine

마진이 있는 선형 분리 가능 데이터셋 챕터 1.2

R에서의 Support Vector Machine

마진 경계 시각화

  • 마진 경계의 특성:
    • 결정 경계에 평행합니다 (기울기 = 1).
    • 결정 경계 양쪽으로 delta 단위 거리에 위치합니다 (delta = 0.05).
p <- p + 
     geom_abline(slope = 1, intercept = delta, linetype = "dashed") +
     geom_abline(slope = 1, intercept = -delta, linetype = "dashed")

p
R에서의 Support Vector Machine

결정 경계와 마진 경계가 있는 데이터셋 챕터 1.2

R에서의 Support Vector Machine

연습할 시간입니다!

R에서의 Support Vector Machine

Preparing Video For Download...