Matlab toolbox installation method LIBSVM

SVM (support vector machine, SVM) is a popular machine learning algorithms to learn, play an important role in the classification and regression analysis. There are a variety of SVM based algorithm development kit, here we want to install is very popular libsvm toolbox.

About libsvm

LIBSVM is a simple, easy to use and fast and efficient package Taiwan University Chih - Jen Lin (Lin Chih-Jen) associate professors and other development and design of SVM pattern recognition and regression, which not only provides a compiled executable file in the Windows family of systems , also provides the source code to facilitate improvements, modifications and other applications in the operating system; it also has a feature is to adjust the parameters of SVM involved is relatively small, a lot of default parameters, to use these default parameters you can solve a lot of problems; and provides cross-validation (Cross-SVM regression problems, including multi-class pattern recognition algorithm based on one.

libsvm difference that comes with MATLAB svm toolbox

In MATLAB, also comes with a svm toolbox, but compared to libsvm, there are some gaps in functionality and ease of use. Specific differences in:

  • MATLAB built-in function supports only achieved svm classification, regression is not supported; and libsvm not only supports classification, also supports regression
  • MATLAB comes with the realization of svm function supports only binary classification, multi-classification problem to be in accordance with the appropriate multi-classification algorithm for programming; and libsvm using one method to support multi-classification
  • Built-in MATLAB svm toolbox can not change the parameters of the Gaussian kernel function, and can libsvm
  • MATLAB svm toolbox also comes with some advantages, such as in solving quadratic programming problems, optional three methods (classical secondary method; SMO; least squares), and libsvm only SMO.

Common extension for the multi classification are:

  • One (one-versus-one) method , two types of training for any two samples are a trained classifier, the finally obtained k (k-1) / 2 binary classifiers, a classification is composed k. When the unknown sample classification, all the k (k-1) / 2 classifiers classify the category of the most frequent as the final classification result of the sample.

  • Many (one-versus-rest) Method : sequentially sample the k class category as a category training, the other remaining classified as another type, used two at a classification SVM training two classifiers Finally, the obtained k k binary classifiers composed classifier. Classification of unknown samples, were classified using the k two classifiers, most of that category will appear as a result of classification in the classification results.

Download the libsvm

In libsvm's official home on libsvm can download expansion packs, support for MATLAB, Java, Python and other languages, here we choose MATLAB version, download the zip file.

I share or download the package, click here to download

note

If you downloaded from the official home page, you need to download a data set [heart_scale.mat] , after testing convenience, click here to download .

If you downloaded my share package, which already contains this data set .mat file, you do not need to download.

libsvm installation

In the \libsvm-3.23\matlabdirectory, there is a README file detailing the installation method.

If windows 64-bit systems, pre-compiled binaries have been provided in \libsvm-3.23\windowsthe next file, you can see four documents, namely libsvmread.mexw64, libsvmwrite.mexw64, svmtrain.mexw64, svmpredict.mexw64.

This can skip a step following the first step of the compiler.

1. Compile

如果是win32位系统,需要自己重新编译c文件,生成MATLAB可识别的mexw32文件。编译方法在上述的README文件也有说明。

将MATLAB的工作文件夹调整到\libsvm-3.23\matlab目录下,在MATLAB的命令行窗口输入>> mex -setup,然后选择编译器如VS2010,最后输入指令>>make。编译完成后,当前路径下会生成对应的mexw32(32位系统)mexw64(64位系统)文件。

过程如下:

matlab>> mex -setup

Would you like mex to locate installed compilers [y]/n? y
Select a compiler:
[1] Microsoft Visual C/C++ version 7.1 in C:\Program Files\Microsoft Visual Studio
[0] None

Compiler: 1
Please verify your choices:
Compiler: Microsoft Visual C/C++ 7.1
Location: C:\Program Files\Microsoft Visual Studio

Are these correct?([y]/n): y

matlab>> make

注意

我的操作系统是win7 64位,原先安装的是MATLAB2014a和VS2015,发现并不支持libsvm工具包中提供好的mexw64文件,原因是MATLAB的版本过低。

随后我考虑重新编译生成适合自己版本的mexw64文件,结果发现MATLAB2014a不支持识别VS2015,最高支持到VS2013。

所以解决方法有两种,一种是给给VS降级,一种是给MATLAB升级。由于前者涉及很多.net和c++库文件,比较繁琐,所以最方便的做法是安装新版本的MATLAB,新老版本的MATLAB只要不安装在一个文件夹下,一般不会出现干扰情况。

安装MATLAB2016及以上版本就可以支持以上的mexw64文件,同时也能识别更新的c/c++编译器。

MATLAB 2016b的下载、安装、激活方法,可以参考这个链接

2.重命名函数

在得到libsvmread.mexw64、libsvmwrite.mexw64、svmtrain.mexw64、svmpredict.mexw64这4个文件后,为了避免和svm内置的函数冲突,最好将svmtrain.mexw64、svmpredict.mexw64这两个文件重命名为libsvmtrain.mexw64、libsvmpredict.mexw64。

3.添加到toolbox

将libsvm-3.23文件夹放置到\MATLAB R2016b\toolbox目录下;

主页> 设置路径> 添加文件夹> 选择libsvm-3.23文件夹;

添加并包含子文件夹> 选择libsvm-3.23文件夹;

主页> 预设> 常规> 更新工具箱缓存> 确定

测试

在MATLAB命令行窗口输入一下指令:

load heart_scale
model = libsvmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g     0.07');
[predict_label, accuracy, dec_values] = libsvmpredict(heart_scale_label, heart_scale_inst, model);

若出现以下结果,说明安装正确。

更详细的关于libsvm的使用方法可以参考libsvm文件夹下的README文件或者是官方主页的说明。

参考链接:https://www.cnblogs.com/Ran-Chen/p/9462825.html

Guess you like

Origin www.cnblogs.com/xiongdongdong/p/11386918.html