Friday, December 21, 2012

R-Software SVM prediction example

Student x y Grade
1 4 85 B
2 6 80 B
3 8 92 A
4 4 70 C
5 2 65 C
6 1 60 C
7 5 89 B
8 7 82 B
9 4 81 B
10 6 95 A


-----------------------
Add library

library(e1071)
-----------------------
Read the data from csv file
a<-read.csv(file=file.choose(), header=TRUE)
-----------------------
name the columns
names(a)

------------------------
partition the data into testset and trainset

> index <-1:nrow(a)
> testindex <-sample(index,trunc(length(index)*30/100))
> testset <-a[testindex,]
> trainset<-a[-testindex,]

-----------------------------
label them

> x2<-trainset[,2]
> y2<-trainset[,3]
> z2<-trainset[,4]


> x1<-testset[,2]
> y1<-testset[,3]
> z1<-testset[,4]

-------------------------------
Create the dataframe
DF <-data.frame(x=x2,y=y2,z=z2)

---------------------------------
Train the model
 mod <-svm(z~x,data=DF,kernel="linear")
------------------------------
Predict
prediction <-predict(mod, newdata=data.frame(x=x1))
----------------------
Table out against the true values
 tab <-table(pred=prediction,true=z1)

----------------------------------
Result

  true
pred A B C
   A 0 0 0
   B 1 1 0
   C 0 0 1


--------------------------------
Library(kernlab)
Least Square SVM
 mod <-lssvm(z~x,data=DF,kernel="rbfdot")

--------------------------------
Least Square SVM kernal trick


> rbf=rbfdot(15)                                               ; 15 is the value of sigma, kernal scaling parameter
>  mod <-lssvm(z~x,data=DF,kernel=rbf )

----------------------------------------------

predict example


library(e1071)

x <- c(1:10)
y <- c(0,0,0,0,1,0,1,1,1,1)
test <- c(11:15)

> DF <- data.frame(x = x, y = y)
> mod <- svm(y ~ x, data = DF, kernel = "linear", gamma = 1, cost = 2,
+ type="C-classification")
> predict(mod, newdata = data.frame(x = test))
1 2 3 4 5 
1 1 1 1 1 
Levels: 0 1

Reading from CSV file

 a <-read.csv(file.choose(), header=TRUE)

One can give path of file instead of file.choose()

Extrating sql data to CSV file


[11/9/2012 3:35:43 PM] Osman: SELECT book_id , name , author  
INTO OUTFILE 'c:/abc.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
FROM books