r - Plot margins for support vector machine -


I just set an SVM in R with e1071. Unfortunately the margin plot and the die-hyperplane do not look desired. I wanted to pass the margin to the support vector, should not this matter be? Can someone make my mistake?

This is my code:

  rm (list = ls (all = TRUE)) x1s & lt; - c (.5,1, 1,2,3,3,5, 1,3,5,4,5,5,5,6) x2s & lt; - c (3.51,2.5,2,1,1.2, 5.8,3,4,5,4,1) Ys & lt; - c (Representative (+1), Representative (-1,6) my.data & lt; - data.frame (x1 = x1s, x2 = x2s, type = as.factor (ys)) my .data library ('e1071') svm.model & lt; - svm (type ~., Data = my.data, type = 'c-classification', kernel = 'linear', scale = FALSE) plot (my.data [, -3], col = (ys + 3) / 2, pch = 19, xlim = c (-1,6), ylim = c (-1,6)) digits (my.data [svm.model $ index, c (1,2)], col = "blue ", Cex = 2) w  - T (svm.model $ coefs)% *% svm.model $ sv b & lt; - -svm.model $ rho p & lt; - svm.model $ sv abline (a = -b / w [1,2], b = -o [1,1] / w [1,2], col = "black", lty = 1) abline (A = - b / p [1,2], b = -w [1,1] / w [1,2], col = "orange", lty = 3) abline (a = - b / p [3,2] ], B = -W [1,1] / w [1,2], col = "orange", lty = 3)    

your last 2 commands

  should be abline (a = (-b-1) / w [1,2], B = -w [1,1] / w [1,2], col = "orange", lty = 3) abline (a = (- b + 1) / w [1,2], b = -h [1,1] / W [1,2], col = "orange", lty = 3)   

another way

  plot (my.data [, - 3], Col = (ys + 3) / 2, pch = 19, xlim = c (-1,6), ylim = c (-1,6)) digits (my.data [svm.model $ index, c (1,2)], col = "blue", cex = 2) x1min = min (x1s); X1max = max (x1s); X2min = x2s; X2max = Max (x2s); Coef1 = sum (svm.model $ coefs * x1s [svm.model $ index]); Coef2 = sum (svm.model $ coefs * x2s [svm.model $ index]); Line (C1 (x1min, x1max), (svm.model $ rho-coef1 * c (x1min, x1max)) / CoEff 2) line (c1 xmin, x1max), (svm.model $ rho + 1-coef1 * c (X1min, x1max) / coef2, lty = 2) rows (c1 (x1min, x1max), (svm.model $ rho-1-coef1 * c (x1min, x1max) / coef2, lty = 2)    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -