LibSVM, developed at National Taiwan University, is an excellent SVM (Support Vector Machine) library, written in C++ and Java. In order to use this library easily in .Net, I wrote this LibSVM .Net wrapper in C#. It is not a re-write of the library in C#, but simply a .Net Wrapper on top of the native C++ library, using PInvoke. The Wrapper currently supports LibSVM version 3.0.
LibSVM provides many excellent tools for offline SVM training. After the training is completed, and SVM model files are generated, this Wrapper may provide an easy solution to port LibSVM and SVM models into .Net applications.
The wrapper contains two DLL’s: JLib.LibSVM.dll and libsvm.dll. JLib.LibSVM.dll is the wrapper and libsvm.dll is the native C++ LibSVM library. The original LibSVM author also provides libsvm.dll, but it is safer to use my recompiled one.
This Wrapper is very easy to use. The Wrapper class name is called LibSVM and under JLib.SVM namespace, so:
Then, start with creating an SVM instance:
LibSVM svm = new LibSVM();
Then, load the model file. It needs to be in LibSVM model file format. Recommend using LibSVM tools to generate the model file.
After the SVM model is loaded, SVM type and classification labels are accessible through Type and Labels properties.
SVM class is designed to hold one SVM model only, so if a new SVM model is loaded, the previous one will be released.
If feature scaling is needed, load range file, and scale the sample features:
SVM.ReadRange(range_file, out target_min, out target_max, out features_min, out features_max);
samples_scaled = SVM.ScaleData(samples, target_min, target_max, features_min, features_max);
Then, call Predict function to do classification or regression:
double label = svm.Predict(samples_scaled);
If estimated posterior probabilities are required, call PredictProb function (this requires the SVM model be able to estimate posterior probability):
double label = svm.PredictProb(samples_scaled[i], out probs);
If decision values are required, call PredictValues function:
double label = svm.PredictValues(samples_scaled[i], out dec_values);
All Predict, PredictProb and PredictValues have several overloading functions, such that the user can use not only SVMNode, but several other more convenient data types, e.g., double, List<double>, double, List<List<double>>, etc.
SVM class implements IDisposable, so it would automatically release itself.
SVM class also provides a useful static function, SaveInLibSVMFormat, which helps on saving the original double or List<double> data into a file in LibSVM format. This is useful for generating offline LibSVM-formatted training data, such that the wonderful tools provided by the LibSVM can be used to train SVM models.
In the source codes, LibSVMTest is for testing the wrapper, and it also can serve as sample codes.
This LibSVM .Net wrapper provides an easy solution to use LibSVM library in .Net applications. Since it is not a re-write of the LibSVM, but simply a wrapper on top of the LibSVM, it is very easy to keep up with the original LibSVM updates. The wrapper adds only a very thin layer (PInvoke) on top of the original LibSVM functions, therefore has little overhead. In C# application, we usually deal with double or List<double> data, but LibSVM uses SVMNode data type. This Wrapper provides convenient data conversion.
Feel free to download and use this wrapper. Please kindly let me know if you use it for commercial use. I don’t provide any warranties, and I am not responsibly for any consequences of using this Wrapper. Please also respect the COPYRIGHT notice of the original LibSVM author, which I have included in both executables and source codes.
Please contact me at if you have any comments and suggestions.
Many thanks Rafał "R@V" Prasał email@example.com for explaining to me the difference of the default calling conventions between C++ and C# when using Visual Studio. He also kindly helped me to solve the issue of “Stack Imbalance Exception” which may occur when debugging the program using Visual Studio 2010.