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

Friday, October 28, 2011

How to use gdb with NS2

I have always faced the problem of segmentation fault many time while performing simulation on NS2. Using gdb is one of the way to find in which function problem is being occurred. So i am just writing it down how to use gdb with NS2

Step1: Go to NS folder via change director command cd.
Step2: Start gdb with NS2 using command gdb ns
Step3: Start the simulation file (say multi.tcl in test folder) using command run test/multi.tcl

Result: Program stops and display the function where segmentation fault has occurred

-----------------
Usman Shahid

Tuesday, November 30, 2010

NS2 bugs

I have faced few bugs during installation of NS2 and its usage. I am trying to keep their record

Bug 1 :- Nam not working
-------------------------------------------------
[root@localhost Download]# nam
nam:
[code omitted because of length]
: no event type or button # or keysym
while executing
"bind Listbox {
%W yview scroll [expr {- (%D / 120) * 4}] units
}"
invoked from within
"if {[tk windowingsystem] eq "classic" || [tk windowingsystem] eq "aqua"} {
bind Listbox {
%W yview scroll [expr {- (%D)}] units
}
bind Li..."
[root@localhost Download]#

---------------------------------
Solution:-
Add
#ifdef GenericEvent
/* GenericEvent */ 0,
#endif
at line 589 in tk8.14/generic/tkbind.c
and then install again

-------------------------
Bug 2 :- Installation Stop at OTCL
**************
undefined reference to ‘__stack_chk_fail_local’
ld: libotcl.so:hidden symbol ‘_stack_chk_fail_local’ isn’t defined
ld: final link failed: Nonrepresentable section on output
make: *** [libotcl.so] Error 1
otcl-1.13 make failed! Exiting ...
See http://www.isi.edu/nsnam/ns/ns-problems.html for problems

*************

Solution
sudo apt-get install gcc-4.3
CC=gcc-4.3 CXX=g++-4.3 ./install
or
CC=gcc-4.3 ./install

---------------------------
Bug 3:- Ns pointing at some other executable
**************************
root@ubuntu:~$ ns
Usage: host [-v] [-a] [-t querytype] [options] name [server]
Listing: host [-v] [-a] [-t querytype] [options] -l zone [server]
Hostcount: host [-v] [options] -H [-D] [-E] [-G] zone
Check soa: host [-v] [options] -C zone
Addrcheck: host [-v] [options] -A host
Listing options: [-L level] [-S] [-A] [-p] [-P prefserver] [-N skipzone]
Common options: [-d] [-f|-F file] [-I chars] [-i|-n] [-q] [-Q] [-T] [-Z]
Other options: [-c class] [-e] [-m] [-o] [-r] [-R] [-s secs] [-u] [-w]
Special options: [-O srcaddr] [-j minport] [-J maxport]
Extended usage: [-x [name ...]] [-X server [name ...]]

****************************
Solution
This is due to the fact that machine has another ns command which is located in a higher priority directory (than NS2). To solve this, we can change part declaration order in ".bachrc" by
"PATH=$XGRAPH:$NS:$NAM:$PATH".
-------------------------
Bug 2 :- Installation Stop at TCL
**************
make: *** [tk3d.o] Error 1
tk8.4.14 make failed! Exiting ...
********************************************
Solution
sudo apt-get install build-essential autoconf automake libxmu-dev