Nov 20, 2008

Matlab Svmlight Interface

The most used implementations for SVM (support vector machines) are currently svmlight and libsvm. While libsvm comes with interfaces for many different programming languages, svmlight (svm-light perf) has the advantage that you can specify the loss function. I have very disproportionate classes in my training data, so using the area under the curve (AUC), the area under the receiver operating characteristics (ROC), brings a great improvement.

I couldn't find a good matlab interface, so I wrote one. Note that it is functional, but quite simple.
function Y=svmlight(training,test,params)
% (very) simple wrapper for svmlight
% Writes matrices in sparse format to data file that can be used by svmlight.
% Columns are variables, rows are observations.
% It is assumed that the first column of the matrix is the target. Targets are elements of {-1,1}.
% These steps are made:
% 1. output matlab matrix to text file
% 2. format text file for svm (awk)
% 3. create classification model (svm_learn)
% 4. apply classification model (svm_classify)
% All files are written in the /tmp/ directory
% Example:
% Y=svmlight(data(traininds,:),data(testinds,:),'-c 1 -w 3 -l 10 ');
% (if you set parameters for svmlight don't forget to include the learning options!)
% (c) Benjamin Auffarth, 2008
% licensed under CC-by-sa (creative commons attribution share-alike)
if nargin<3
   params='-c 1 ';
[s,w]=unix(['svmlight/svm_perf_learn ' params trainfile '.svm2 ' trainfile '.model']);
if s
'error in executing smv-light!');w;
   error('svm_perf_learn not found or returned error');
[s,w]=unix(['svmlight/svm_perf_classify -v 0 ' testfile '.svm2 ' trainfile '.model ' testfile '.dat']);
if s

   disp('error in executing smv-light!');w;
    error('svm_perf_classify not found or returned error');
Y=dlmread([testfile '.dat']);

function fname=sparse_write(M)
[a,fname]=unix('date +/tmp/_svm_%F_-%H:%M_%S%N');
fname=fname(1:end-1); % get rid of newline character
dlmwrite([fname '.svm1'],M,'delimiter',' ');
unix(['awk -F" " ''{printf $1" "; for (i=2;i<=NF;i++) {printf i-1":"$i " "}; print ""}'' ' fname '.svm1 > ' fname '.svm2']);


Some Explanations

You need awk installed (obviously). If you are working in a windows environment, you can install awk on cygwin, wubi, or install awk on windows).

Temporary files are generated and stored in the /tmp/ directory. If you are on windows you might want to change that to "."

You need svm-light perf installed. The function searches for it in the svmlight subdirectory. You might want to adapt that to point to your local installation of svmlight.

Enjoy. Please leave comments below.


  1. Please don't copy-paste this code if you are in a windows environment. Download it from snipplr.

  2. This is exactly what I need. Thank you very much!

  3. @Dylan: You are welcome. Glad it helped.

  4. thank so much this information could help me to do a fix in my computer , these kind of blog can help some people.

  5. Just for my own reference, you can do this without matlab using standard *nix commands.

    To convert from csv to svmlight format:
    awk -F"," '{printf $1" "; for(i=2;i<=NF;i++) {printf i-1":"$i " "}; print ""}' > hepatitis.svmlight

    Separate into training and test if needed:
    head -n 155 hepatitis.svmlight >
    tail -n 155 hepatitis.svmlight > hepatitis.svmlight.test

    Run training and classification:
    ./svm_learn -c 1 -# 1 -w 3 -l 10 .model
    ./svm_classify hepatitis.svmlight.test .model .outputfile

  6. hi, would you plz describe for me how can I use this code for KNN classifier?or do you have a code for ROC of KNN and Glass data set?I need it for a part of my project,plz help me,thank you

  7. This is for SVM not for KNN. For KNN you can use matlab's knn functions. For ROC/AUC statistics see my post on ROC.

  8. Can you get an example of use.
    like which format is the training parameter? 


  9. training is a matrix, See the commented text in the function:
    % Columns are variables, rows are observations.
    % It is assumed that the first column of the matrix is the target. Targets are elements of {-1,1}.

  10. What is the function sparse_write? Matlab doesn't recognize it.

  11. just a question for installation of SVMLIGHT it 'll be in with directory(may be in work folder of matlab)??and plz can you exmplain exatly what's role of awk??

  12. I tried the native mex interface for svmlight, but the performance is awful (many orders of magnitude slower than the libsvm interface) and I suspect it is doe to the mex interface itself. Not having the time to redevelop it myself, I came searching for another - this is something along the lines of what I would have tried to do by myself, but now I don't need to understand the svmlight doc format. I still feel dirty using this way though.

    Many thanks.

  13. Hallo,
    I am working on SVM, I want ask you a help, I want compiling and invoking the algorithm of SVMlight in ( )as MEX function
    from within the MATLAB environment.
    addpath ('C:\Users\hp\Documents\MATLAB\svm_mex601\bin');
    cmd=['-c 1 -w 3 -l 10 '];
    it gives me:

    compile failed
    Undefined function or method 'svm_learn'
    Thanks you for help me!!

  14. thank you of some of the articles I find this is one of the very interesting article to read, I like this alat bantu sex possible for people not important but very useful for us to read obat penghilang tato alami

  15. I just want to say : why do I not find this article sooner ! thankyou so much!
    happy wheels , super mario , pacman, solitaire , plants vs zombies, car games, agario!

  16. Thank you! I have to thank you for the efforts you’ve put in penning this site.I really hope to check out the same high-grade blog posts by you later on as well. In truth, your creative writing abilities has motivated me to get my own website now !
    I like play games. Click to play:
    open facebook
    facebook login
    happy wheels
    happy wheels game
    earn to die
    earn to die game

  17. In this game, you start at the cavern men's age, then evolve! There is a total of 5 ages, each with its units and turrets. Take control of 16 different units and 15 different turrets to defend your base and destroy your enemy.
    Summon creatures to fight enemy units and demolish the opposing castle. Your castle is equipped with a crossbow, which you can use to shoot enemies. Make sure you upgrade skills to increase your chances of winning battles.

    The war between humans, orcs and elves continues. Lead your race through a series of epic battles, using your crossbow to fend off foes and sending out units to destroy castles. Researching and upgrading wisely will be crucial to your success!

    Play Age of war 2! Battle your opponent through the ages, in the 2nd installment of the hit game Age Of War. Age of war 2 is totally free and requires no registration!
    This is an evolutionary strategy game that pits you against alien invaders. You begin the game in the Stone Age, with just a base and some money. Select your base to view the available building options and spend your money wisely. Buy buildings that will spawn offensive units and gatherers, as well as turrets to bolster your defenses. You can upgrade your buildings so that they produce more effective units and spawn them at a faster rate.

  18. Thanks for the best was very useful for me.keep sharing such ideas in the future as well.this was actually what i was looking for,and i am glad to came here!
    Tank trouble
    Hi! I’ve been reading your blog for a while
    now and finally got the courage to go ahead and give youu a shout out from Austin Texas! Just wanted to tell you keep up the fantastic work!my weblog:
    Earn to die

    Barron Games

    Subble shooter

  19. I love reading your articles and I shared it with my friends. People also like it and give good feedback. Thank you.
    - Mortal Kombat XL
    - Atari Breakout
    - Dragon Ball Z Games

  20. Excellent .. Amazing .. Thanks for giving me the useful information. I think I need it!
    See more at: happy wheels | happy wheels game | happywheels | happywheels game
    I would like more information about this, because it is very nice. Thanks for sharing!

  21. Let’s keep out sites for your child! click: | | happy wheels | abcya | fnaf 4 | games for kids | racing games | pacman to play for free!

  22. Hotmail is an email account of Microsoft Corporation. Like Google's Gmail, it is full of features usually xuyen.Neu of an email you want to register an account please follow these basic steps:
    Hotmail login

    Hotmail review

    Sign in to hotmail

    Log in to hotmail

    Recover hotmail password

    Tank Trouble is a very interesting flash game about tanks, about war and about destruction

    One Penguin Takes it personally when he is surfing the web and stumbles upon a web site telling him that he cant fly, after that he sets his mind to research and practice flying until he can prove the world that he can..
    Slitherio | LEARN TO FLY | LEARN TO FLY 2

    Strike Force Heroes is a new game action-packed shooter from the creators of Raze; with 3 game modes, 15 campaign missions and over 65 weapons.
    Strike Force Heroes | Strike Force Heroes 4


  23. تعد شركة الرحمة افضل شركات التنظيف ومكافحة الحشرات بالرياض والممكلة فهى توفر افضل عماله مدربة على جميع اعمال التنظيف للمنازل والفلل والشقق والخزانات بافضل المنظفات العالمية العالية الجودة وارسعارها في متناول الجميع
    افضل شركة تنظيف منازل بالرياض
    شركة نقل عفش بالرياض

    شركة جلى بلاط بالرياض

    كشف تسربات المياه بالرياض

    خدمات التنظيف تحتاج الى عماله ماهره لتقوم بها في اسرع وقت نوفر افضل العمالة الفلبينية والمصرية والهندية لتتم عمليات التنظيف وجلى البلاط على اتم صورة وباسرع وقت وبارخص الاسعار
    افضل شركة تنظيف بالرياض

    افضل شركة تنظيف شقق بالرياض
    افضل شركة تنظيف فلل بالرياض
    افضل شركة مكافحة حشرات بالرياض
    شركة تسليك مجارى بالرياض

    وان كنت تريد ان تتخلص من الاثاث القديم الذي يسبب لك الازعاج فشركة الرحمة تقوم بشراء الاثاث الستعمل فهى افضل شراء اثاث مستعمل بالرياض

    نوفر افضل مستودعات تخزين الاثاث وافضل سيارات نقل الاثاث بالاضافة الى خدمات تنظيف الخزانات وعزل الخزانات والاسطح باتصالك بالرحمة توفر على نفسك التعب والمجهود في التعامل مع غير المحترفين وصرف الكثير من الاموال فالرحمة افضل شركات التنظيف ومكافحة الحشرات بالرياض وبشهادة عملائنا
    افضل شركة تنظيف خزانات بالرياض
    افضل شركة عزل الاسطح بالرياض
    افضل شركة تنظيف مجالس بالرياض
    افضل شركة تنظيف سجاد بالرياض
    شركة تخزين اثاث بالرياض

  24. شركة تخزين اثاث بالرياض
    عند انتقالك عميلنا الكريم من مكان لمكان قد تحتاج الى شركة تخزين اثاث بالرياض فكثيرا عندما نتقل من مكان لاخر او من منزل لاخر نحتاج الى مكان تخزين اثاث بالرياض لنقوم بتخزين الاثاث الزائد عن الحد واحيانا نقوم بالانتقال الى خارج المملكة لفترة خصوصا الاخوة المغتربون فيحتاجون لخدمة تخزين عفش بالرياض فشركة الشمس توفر افضل مستودعات تخزين اثاث بالرياض وافضل مستودعات تخزين اثاث بالرياض فلا تردد في الاتصال بالشمس افضل شركة تخزين اثاث بالرياض وبالمملكة فنحن الافضل دائما

    وان كنت تريد الاستغناء وبيع بعض الاثاث القديم فشركة الشمس تقدم خدمة شراء اثاث مستعمل بالرياض بافضل الاسعار لانها افضل شركة شراء اثاث مستعمل بالرياض تتميز بتوفيرها عدة ارقام محلات شراء اثاث مستعمل بالرياض وخدماتها تغطى جميع احياء ومناطق الرياض وتوفر حقين شراء اثاث مستعمل بالرياض

    شركة شراء اثاث مستعمل شمال الرياض
    شركة شراء اثاث مستعمل شرق الرياض

  25. I just glad to be here see this article, i found here great knowledge.
    Chandigarh Call Girls

  26. Welcomes to google terminal keep sharing such ideas in the future as well.
    google snake this was actually EARN TO DIE
    what i was looking for,and i am glad to came here! AGE OF WAR 2
    Hi! I’ve been reading your blog for a while HAPPY WHEELS
    I want you to thank for your time of this wonderful read!!! Slitherio
    Amazing insight you have on this, Slither io This article always blew me BIG FARM
    hotmail sign in | hotmail login
    sign in to hotmail | log in to hotmail

  27. It enjoys a position Astarte a privileged and economic growth, a brilliant and Musharraf is the biggest sixteenth economy in the world and the sixth
    the largest economy in comparison with EU countries, therefore the areas of investment circle in Turkey has widened in terms of investment in medicine, health care, communications and information other areas and services the other has flourished investment in Turkey is the very big way and prefer a lot of
    apartments for sale in istanbul
    istanbul property
    real estate istanbul
    property in turkey
    turkey real estate
    invest in turkey
    investment investors in Turkey from the other country other and real Estate Investor and from other areas of investment and other and it is preferred because Turkey tourist country of destination for a lot in order to enjoy the atmosphere and Gamalhao stay out so easy Perhaps they Shark P owning real estate the process of buying a home and apartments in Turkey, it offers a range of best and biggest real estate, this area is completely successful and completely free of risk-taking venture.


  29. I was working and suddenly I visits your site frequently and recommended it to me to read also. Age Of War 2
    Big Farm | Slitherio
    Tank Trouble | Happy Wheels
    Goodgeme Empire |