1.This problem involves hyperplanes in two dimensions.
(a) Sketch the hyperplane 1+3X1−X2=0. Indicate the set of points for which 1+3X1−X2>0, as well as the set of points for which 1+3X1−X2<0.
(b)On the same plot, sketch the hyperplane −2+X1+2X2=0. Indicate the set of points for which −2+X1+2X2>0, as well as the set of points for which −2+X1+2X2<0.
# Load required library
library(ggplot2)
# Create grid
x1 <- seq(-5, 5, length.out = 200)
x2 <- seq(-5, 5, length.out = 200)
grid <- expand.grid(X1 = x1, X2 = x2)
# Define two linear functions
grid$Z1 <- 1 + 3 * grid$X1 - grid$X2 # First hyperplane
grid$Z2 <- -2 + grid$X1 + 2 * grid$X2 # Second hyperplane
# Plot both hyperplanes with shaded regions
ggplot(grid, aes(x = X1, y = X2)) +
geom_contour(aes(z = Z1), breaks = 0, color = "red", linewidth = 1) +
geom_contour(aes(z = Z2), breaks = 0, color = "blue", linewidth = 1) +
geom_raster(aes(fill = factor(sign(Z1))), alpha = 0.25) +
geom_raster(aes(fill = factor(sign(Z2))), alpha = 0.25) +
labs(title = "Exercise 1: Hyperplanes and Their Regions", fill = "Sign") +
theme_minimal()
Explanation: This code generates a 2D plot showing two hyperplanes:
1 + 3X1 - X2 = 0 (red) and
-2 + X1 +2X2 = 0 (blue).
It also shades regions where the expressions are greater than or less than 0, helping visualize classification regions.
Exercise 2
# Create grid
x1 <- seq(-5, 5, length.out = 200)
x2 <- seq(-5, 5, length.out = 200)
grid <- expand.grid(X1 = x1, X2 = x2)
# Non-linear expression
grid$Z <- (1 + grid$X1)^2 + (2 - grid$X2)^2
# Plot the circle (decision boundary) and shading
ggplot(grid, aes(x = X1, y = X2)) +
geom_contour(aes(z = Z), breaks = 4, color = "black", size = 1) +
geom_raster(aes(fill = factor(Z > 4)), alpha = 0.4) +
labs(title = "Exercise 2: Non-Linear Decision Boundary", fill = "Class") +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Explanation:
For (0, 0), the expression evaluates to 13 > 4 → classified as
blue. For (-1, 1), the expression evaluates to 6 > 4
→ blue. For (2, 2), it evaluates to 9 > 4 →
blue. For (3, 8), it evaluates to 50 > 4 →
blue.
This code plots the circular decision boundary defined by:
(1+X1)^2 + (2-X2) ^2 = 4. It shades the interior region (≤ 4) and
exterior region (> 4), effectively simulating a nonlinear
classifier.