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
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 )
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 )
----------------------------------------------
No comments:
Post a Comment