開始使用免費開始

產生 2D 徑向可分的資料集

在這個練習中,你將建立一個包含 400 個均勻分布資料點的 2D 徑向可分資料集。

本練習屬於課程

R 的支援向量機

檢視課程

練習說明

  • 產生一個資料框 df,包含:
    • 變數 x1x2 的 400 個點。
    • x1x2 在 (-1, 1) 之間均勻分布。
  • 建立一個半徑為 0.8、以原點為中心的圓形邊界。
  • 建立 df$y,其值依點是否位於圓內或圓外而為 -1 或 1。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

#set number of variables and seed
n <- ___
set.seed(1)

#Generate data frame with two uniformly distributed predictors, x1 and x2
df <- data.frame(x1 = runif(n, min = ___, max = ___), 
                 x2 = runif(n, min = ___, max = ___))

#We want a circular boundary. Set boundary radius 
radius <- ___
radius_squared <- radius^2

#create dependent categorical variable, y, with value -1 or 1 depending on whether point lies
#within or outside the circle.
df$y <- factor(ifelse(df$___ + df$___ < radius_squared, -1, 1), levels = c(-1, 1))
編輯並執行程式碼