Using package e1071 to run svm
#install.packages("e1071", dep = TRUE)
library("e1071")
Method to get random data: Set directory, read file csv and take only the feature useful (X, Y, ISLandmark)
setwd("C:/Users/Admin/Documents/Intern/Intern")
data <- read.csv("1 .csv")
data1 <- data[,c("X","Y","IsLandmark")]
In case we want a sample of 90 coordinates randomly in position
i = 1
for (i in 1:50){
if (i <= 50){
randomdata <- data.frame(X= as.integer(runif(1,500,2500)) , Y = as.integer(runif(1,500,2500)), IsLandmark = 0)
data1 <- rbind(data1,randomdata)
# i = i + 1
}
}
rand.rowx <- data1[sample(nrow(data1)),]
write.csv(rand.rowx,file= "myfile.csv",quote=F,row.names=F,col.names=T)
## Warning in write.csv(rand.rowx, file = "myfile.csv", quote = F, row.names =
## F, : attempt to set 'col.names' ignored
We use sample with 90 coordinates, which contains 16 landmarks.
data2 <- read.csv("myfile.csv")
data2
## X Y IsLandmark
## 1 1749 1180 0
## 2 1424 615 0
## 3 1706 563 0
## 4 1575 771 0
## 5 1491 1135 1
## 6 1526 863 0
## 7 936 794 0
## 8 944 1562 0
## 9 2113 1776 0
## 10 831 906 1
## 11 514 1001 0
## 12 1807 1212 0
## 13 2199 524 0
## 14 881 944 1
## 15 755 2492 0
## 16 1388 1612 0
## 17 1218 1041 1
## 18 572 2440 0
## 19 1367 2105 0
## 20 1016 2379 0
## 21 2007 1111 0
## 22 1725 552 1
## 23 1768 691 0
## 24 2316 720 0
## 25 1126 1158 0
## 26 1978 1084 0
## 27 1969 2449 0
## 28 1656 926 0
## 29 2494 2216 0
## 30 985 1254 0
## 31 187 1043 0
## 32 2153 2145 0
## 33 1701 853 0
## 34 2476 1630 0
## 35 2220 647 0
## 36 549 2122 0
## 37 2290 1110 1
## 38 1947 626 1
## 39 2103 1019 0
## 40 2457 1866 0
## 41 585 881 0
## 42 1004 608 0
## 43 1123 1399 0
## 44 599 1585 0
## 45 1712 2316 0
## 46 591 514 0
## 47 1800 603 0
## 48 514 1021 0
## 49 1095 2467 0
## 50 228 977 0
## 51 2408 927 1
## 52 875 679 0
## 53 1594 1507 0
## 54 2041 764 0
## 55 1180 1459 0
## 56 617 771 0
## 57 877 1215 0
## 58 2104 2076 0
## 59 1918 834 0
## 60 553 1896 0
## 61 1405 1635 0
## 62 633 1421 0
## 63 2036 515 0
## 64 2062 1018 0
## 65 1206 2235 0
## 66 1380 1807 0
## 67 877 985 1
## 68 192 1005 0
## 69 1218 2363 0
## 70 966 2046 0
## 71 2289 606 0
## 72 542 840 0
## 73 569 918 0
## 74 266 943 0
## 75 1487 700 0
## 76 901 602 0
## 77 1698 1279 1
## 78 786 1092 1
## 79 579 836 0
## 80 563 1066 0
## 81 2364 1024 1
## 82 1242 847 1
## 83 2155 726 1
## 84 778 820 0
## 85 1199 617 0
## 86 2103 1223 1
## 87 2311 835 1
## 88 699 830 0
## 89 1640 1248 0
## 90 2013 1363 0
The implementation is based on svm method with iris data
x <- subset(data2, select=-IsLandmark)
y <- data2[[3]]
SVM model and show summary
svm_model1 <- svm(x,y)
summary(svm_model1)
##
## Call:
## svm.default(x = x, y = y)
##
##
## Parameters:
## SVM-Type: eps-regression
## SVM-Kernel: radial
## cost: 1
## gamma: 0.5
## epsilon: 0.1
##
##
## Number of Support Vectors: 39
Prediction + measure execution time in R
pred <- predict(svm_model1,x)
system.time(pred <- predict(svm_model1,x))
## user system elapsed
## 0 0 0
confusion matrix result of prediction, Using command table to compare the result of SVM prediction and the class data in y variable.
table(pred,y)
## y
## pred 0 1
## -0.0384500320198074 1 0
## -0.0384487120248271 1 0
## -0.0383044013746756 1 0
## -0.0382295659376446 1 0
## -0.0361004959462585 1 0
## -0.0283196905175232 1 0
## -0.0242531209952951 1 0
## -0.0226096057724657 1 0
## -0.0219677863608499 1 0
## -0.0206770355448382 1 0
## -0.0193489352663112 1 0
## -0.0187939622067206 1 0
## -0.0170244448390738 1 0
## -0.0169231263057651 1 0
## -0.0157935789496064 1 0
## -0.0133268137801264 1 0
## -0.0125872583608972 1 0
## -0.0112068697765265 1 0
## -0.0071237848825832 1 0
## -0.00702882186001524 1 0
## -0.00664897666920367 1 0
## -0.00559802255937325 1 0
## -0.00460750133992421 1 0
## 0.00125119399432361 1 0
## 0.00695020248054748 1 0
## 0.0101310179977522 1 0
## 0.0136434187451783 1 0
## 0.0142425912419 1 0
## 0.0142571454842559 1 0
## 0.0143134220888162 1 0
## 0.0146908044028896 1 0
## 0.0161077127921457 1 0
## 0.0185645516929446 1 0
## 0.0189104986669764 0 1
## 0.0191548480920185 0 1
## 0.0204219324561783 1 0
## 0.0210457572185303 1 0
## 0.0234378507161287 1 0
## 0.0240690698093879 1 0
## 0.0242287746196282 1 0
## 0.0261045025481876 1 0
## 0.0286203648311211 0 1
## 0.0288111988703123 1 0
## 0.0292097574998336 1 0
## 0.0317238554346276 1 0
## 0.0317579404939251 1 0
## 0.0335919520392115 1 0
## 0.0336011525236961 1 0
## 0.0337994905675384 1 0
## 0.0340587391427606 1 0
## 0.0345763227872327 1 0
## 0.034735586229229 1 0
## 0.0349177294068263 1 0
## 0.0351338204698413 1 0
## 0.0357602899270232 1 0
## 0.0370497086617673 1 0
## 0.0371175528263631 1 0
## 0.0382604944783368 1 0
## 0.0383113762904768 1 0
## 0.0383778747804265 1 0
## 0.0384145299923441 1 0
## 0.0384447214993768 1 0
## 0.0385807079745306 1 0
## 0.0386004559137983 1 0
## 0.0390910477670897 1 0
## 0.0399208215617779 1 0
## 0.0441056505575021 1 0
## 0.0443435893169393 1 0
## 0.0444867793600769 1 0
## 0.0455711692151401 0 1
## 0.0456511541135122 1 0
## 0.0461808504578682 0 1
## 0.0521578998296265 1 0
## 0.0522564556226635 1 0
## 0.0528270777114138 0 1
## 0.0558753316980536 0 1
## 0.056080890858341 1 0
## 0.0593087807404166 1 0
## 0.0609345699229172 0 1
## 0.0610315084716905 0 1
## 0.0616763695920771 0 1
## 0.0635144702398425 1 0
## 0.0653925998850003 0 1
## 0.0670179494926564 0 1
## 0.0686910143241712 1 0
## 0.0708371755515847 1 0
## 0.0922068534210563 0 1
## 0.113306377313982 0 1
## 0.128665122324105 0 1
## 0.129045449545462 0 1
TUning SVM to find the best cost and gamma ..
svm_tune <- tune(svm, train.x=x, train.y=y,
kernel="radial", ranges=list(cost=10^(-1:2), gamma=c(.5,1,2)))
print(svm_tune)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## cost gamma
## 10 2
##
## - best performance: 0.1050518
Use cost vs gamma above to create svm model again Try this (svm with 5-fold cross-validation)
svm_model_after_tune <- svm(IsLandmark ~ ., data=data1, kernel="radial", cost=100, gamma=0.5, cross=5)
summary(svm_model_after_tune)
##
## Call:
## svm(formula = IsLandmark ~ ., data = data1, kernel = "radial",
## cost = 100, gamma = 0.5, cross = 5)
##
##
## Parameters:
## SVM-Type: eps-regression
## SVM-Kernel: radial
## cost: 100
## gamma: 0.5
## epsilon: 0.1
##
##
## Number of Support Vectors: 60
##
##
##
## 5-fold cross-validation on training data:
##
## Total Mean Squared Error: 0.1465916
## Squared Correlation Coefficient: 0.1490407
## Mean Squared Errors:
## 0.130819 0.1113168 0.1804963 0.101058 0.2092679
Prediction new model
pred <- predict(svm_model_after_tune,x)
system.time(predict(svm_model_after_tune,x))
## user system elapsed
## 0 0 0
table(pred,y)
## y
## pred 0 1
## -0.129233445451639 1 0
## -0.0518964401751186 1 0
## -0.0475511693147122 1 0
## -0.0386215786019224 1 0
## -0.0386134451243432 1 0
## -0.0385925500420917 1 0
## -0.0385180444023849 1 0
## -0.0384879449911407 1 0
## -0.0384854285818151 1 0
## -0.0384796819283278 1 0
## -0.038387644162411 1 0
## -0.0383217140272445 1 0
## -0.0383177082515047 1 0
## -0.0341830345179834 1 0
## -0.0317008620627518 1 0
## -0.0293115783925876 1 0
## -0.0251632087560116 1 0
## -0.0200277711362385 1 0
## -0.015877082502413 1 0
## -0.0146790171357984 1 0
## -0.00889365769457012 1 0
## -0.00629819409103805 1 0
## -0.00405808291343723 1 0
## -0.00249480862129758 1 0
## -0.000523901631934748 1 0
## -0.000250322767015781 0 1
## 0.00173245054054805 0 1
## 0.00275771525548985 1 0
## 0.00378947803830831 1 0
## 0.00410532422838952 1 0
## 0.00771553766910513 1 0
## 0.00810188454485972 1 0
## 0.00936275063424519 1 0
## 0.0102886955227985 1 0
## 0.0103020462711589 1 0
## 0.010475880599922 1 0
## 0.0162888975783693 1 0
## 0.0221259614806988 1 0
## 0.0277078311504893 1 0
## 0.0280716952691987 1 0
## 0.0321986937468454 1 0
## 0.0335747904805273 1 0
## 0.0361066747566074 1 0
## 0.0379524290116965 1 0
## 0.0382448078442057 1 0
## 0.0382704551344974 1 0
## 0.0383358460829084 1 0
## 0.0383894230942887 1 0
## 0.0384065773855887 1 0
## 0.0384227073448817 1 0
## 0.0384294845942383 1 0
## 0.0384404586565304 1 0
## 0.0385104277478049 1 0
## 0.0385187586839231 1 0
## 0.0385353859973334 1 0
## 0.0385460953437441 1 0
## 0.0385735815062226 1 0
## 0.0385754123259577 1 0
## 0.0385871911787287 1 0
## 0.0385931221222157 1 0
## 0.0386010942610618 1 0
## 0.0386048605003614 1 0
## 0.048666144316881 0 1
## 0.0622586191595359 1 0
## 0.0627546053625379 1 0
## 0.0774572383997167 1 0
## 0.0874795561963724 1 0
## 0.118471248407242 1 0
## 0.121353218594963 1 0
## 0.131910920079385 0 1
## 0.158532843582648 1 0
## 0.158876921382093 1 0
## 0.159780723297842 1 0
## 0.170430491506608 1 0
## 0.192091809586378 0 1
## 0.192466953383206 0 1
## 0.216063619388621 0 1
## 0.216071586596134 1 0
## 0.230511043206016 0 1
## 0.241907940732692 1 0
## 0.292915673405811 0 1
## 0.297503336847947 0 1
## 0.311954674091814 1 0
## 0.333404750599932 0 1
## 0.3442247190623 1 0
## 0.421265644139548 0 1
## 0.558740598192256 0 1
## 0.741719488562597 0 1
## 0.961370597315667 0 1
## 0.977374694670405 0 1