-
Notifications
You must be signed in to change notification settings - Fork 0
/
three_dim.R
58 lines (49 loc) · 1.52 KB
/
three_dim.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
library(rgl)
library(e1071)
# Load data
data(iris)
data <- iris
# Only include two species for binary classification
# data <- data[data$Species != "versicolor", ]
# Fit SVM
model <- svm(
Species ~ Sepal.Length + Sepal.Width + Petal.Length,
data = data,
kernel = "linear",
scale = FALSE
)
# Create a sequence of values for each dimension
sl <- seq(min(data$Sepal.Length), max(data$Sepal.Length), length = 30)
sw <- seq(min(data$Sepal.Width), max(data$Sepal.Width), length = 30)
pl <- seq(min(data$Petal.Length), max(data$Petal.Length), length = 30)
# Expand grid to cover all combinations
grid <- expand.grid(Sepal.Length = sl, Sepal.Width = sw, Petal.Length = pl)
# Predict using the SVM model
classes <- predict(model, grid)
# Assign colors based on classes
colors <- ifelse(classes == "setosa", "red", "blue")
# colors <- ifelse(iris$Species == "setosa", "red",
# ifelse(iris$Species == "versicolor", "blue", "green")
# )
# Plot the original data points
plot3d(
x = data$Sepal.Length,
y = data$Sepal.Width,
z = data$Petal.Length,
col = ifelse(data$Species == "setosa", "red", "blue"),
# col = ifelse(iris$Species == "setosa", "red",
# ifelse(iris$Species == "versicolor", "blue", "green")
# ),
type = "s",
radius = 0.05,
xlab = "Sepal Length", ylab = "Sepal Width", zlab = "Petal Length"
)
# Add the SVM decision plane
points3d(
x = grid$Sepal.Length,
y = grid$Sepal.Width,
z = grid$Petal.Length,
col = colors,
alpha = 0.5, # Semi-transparent
size = 1
)