R में Support Vector Machines
Kailash Awati
Instructor
ggplot() का उपयोग करके training डेटा प्लॉट करें।# Visualize training data, distinguish classes using color
p <- ggplot(data = trainset, aes(x = x1, y = x2, color = y)) +
geom_point() +
scale_color_manual(values = c("red", "blue"))
# Render plot
p
svm_model के index से support vectors चिन्हित करें।# Identify support vectors
df_sv <- trainset[svm_model$index, ]
# Mark out support vectors in plot
p <- p + geom_point(data = df_sv,
aes(x = x1, y = x2),
color = "purple",
size = 4, alpha = 0.5)
# Display plot
p

बाउंडरी का slope और intercept निकालें:
svm_model के coefs और SV एलिमेंट से weight वेक्टर w बनाएँ।# Build weight vector
w <- t(svm_model$coefs) %*% svm_model$SV
-w[1] / w[2]# Calculate slope and save it to a variable
slope_1 <- -w[1] / w[2]
svm_model$rho / w[2]# Calculate intercept and save it to a variable
intercept_1 <- svm_model$rho / w[2]
geom_abline() का उपयोग करें।# Plot decision boundary based on calculated slope and intercept
p <- p + geom_abline(slope = slope_1,
intercept = intercept_1)
1 / w[2] से offset।# Add margins to plot
p <- p +
geom_abline(slope = slope_1,
intercept = intercept_1 - 1 / w[2],
linetype = "dashed") +
geom_abline(slope = slope_1,
intercept = intercept_1 + 1 / w[2],
linetype = "dashed")
# Display plot
p

e1071 में svm का plot() फंक्शन डिसीज़न बाउंडरी प्लॉट करना आसान बनाता है।# Visualize decision boundary using built in plot function
plot(x = svm_model,
data = trainset)

R में Support Vector Machines