⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readme

📁 一个计算线性支持向量机的matlab源代码
💻
字号:
Two things are provided in this directory: an automatic model selectiontool and a python binding for libsvm.		Part I: Model Selection ToolsIntroduction===============grid.py is a model selection tool for C-SVM classification using RBF(radial basis function) kernel. It uses cross validation (CV) techniqueto estimate the accuracy of each parameter combination in the specified range and helps you to decide the best parameters for your problem.grid.py directly executes libsvm binaries (so no python binding is needed)for cross validation and then draw contour of CV accuracy using gnuplot.You must have libsvm and gnuplot installed before using it. The package gnuplot is available at http://www.gnuplot.info/Usage: grid.py [-log2c begin,end,step] [-log2g begin,end,step] [-v fold]        [-svmtrain pathname] [-gnuplot pathname] [-out pathname] [-png pathname]         [additional parameters for svm-train] datasetThe program conducts v-fold cross validation using parameter C (and gamma) = 2^begin, 2^(begin+step), ..., 2^end. You can specify where the libsvm executable and gnuplot are using the-svmtrain and -gnuplot parameters.For windows users, please use pgnuplot.exe. If you are using gnuplot3.7.1, please upgrade to version 3.7.3. The version 3.7.1 has a bug.Example=======> python grid.py -log2c -5,5,1 -log2g -4,0,1 -v 5 -m 300 heart_scaleUsers (in particular MS Windows users) may need to specify the path ofexecutable files. You can either change paths in the beginning ofgrid.py or specify them in the command line. For example,> grid.py -log2c -5,5,1 -svmtrain c:\libsvm-2.7\windows\svmtrain.exe -gnuplot c:\tmp\gnuplot\bin\pgnuplot.exe -v 10 heart_scaleOutput: two filesdataset.png: the contour plot of the CV accuracy (generated by gnuplot)dataset.out: the log of accuracy at each (C,gamma)Parallel grid search (experimental)===================================You can conduct a parallel grid search by dispatching jobs to a cluster of computers which share the same file system. First, you add machine names in grid.py:telnet_workers = ["linux1", "linux5", "linux5"]The same machine (e.g., linux5 here) can be listed more than once ifit has multiple CPUs or has more RAM. If the local machine is thebest, you can also enlarge the nr_local_worker. For example:nr_local_worker = 2Example:> python grid.py heart_scalePassword: ********login ok linux1login ok linux5login ok linux5...The password is the one used for entering your system. If -log2c, -log2g, or-v is not specified, default values are used. If your system uses ssh instead of telnet, you should setup ssh first so thatthe authentication works without asking a password, and list the computer namesin ssh_workers.		Part II: Python-to-libsvm interfaceIntroduction============Python (http://www.python.org/) is a programming language suitable forrapid development. This python-to-libsvm interface is developed so users can easily experiment with libsvm using python. The interface is developed with SWIG, The original idea and the SWIG interface file was provided by Carl Staelin(staelin@hpl.hp.com) from HP Labs. The interface was integrated into thelibsvm package by Li-lun Wang (llwang@infor.org) from National TaiwanUniversity. Chih-Chung Chang (b4506055@csie.ntu.edu.tw) from NationalTaiwan University also contributed a lot of useful suggestions and help.Installation============The build process for the various Unix systems is as follows:Before you build the module, you need to find out the python includedirectory, which is typically located at /usr/local/include/python2.3 or/usr/include/python. You can set the variable PYTHON_INCLUDEDIR inMakefile manually or use something like the following:	make PYTHON_INCLUDEDIR=/usr/include/python allAlthough the interface is generated by SWIG, it is not necessary tohave SWIG installed because the generated svmc_wrap.c is included inthis package (It was generated using SWIG 1.3.21). If you prefergenerating the interface with SWIG on your own, you can simply removethe generated files with	make morecleanbefore building the module.When the build process completes, a shared object called svmc.so will becreated.For win32 systems, the shared library svmc.dll is ready in thedirectory windows/python. You need to copy it to this directory.  Thedll file depends on different versions of python, so you may have tore-make it by following the instruction of building windows binariesin libsvm README.Usage=====To use the module, the files svm.py and the shared library (namely svmc.soor svmc.dll) must be placed in the current directory, the python librarydirectory, or the directory where the environment variable PYTHONPATHpoints to. The user then imports everything in svm.py to use libsvm inpython:	from svm import *There are three classes in svm.py, namely svm_parameter, svm_problem, andsvm_model.svm_parameter is used to set the parameters of the trainingprocess. The attributes in svm_parameter include svm_type,kernel_type, degree, gamma, coef0, nu, cache_size, C, eps, p,shrinking, nr_weight, weight_label, and weight. Available svm typesinclude C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, and NU_SVR. Availablekernel types include LINEAR, POLY, RBF, and SIGMOID. The user cansetup the parameters with the constructor and keyword arguments:	param = svm_parameter(kernel_type = LINEAR, C = 10)The user can also modify the parameters later:	param.kernel_type = RBFsvm_problem is used to hold the training data for the problem. Theconstructor takes two arguments; the first of them is the list of labels,and the other is the list of samples. For example	prob = svm_problem([1,-1],[[1,0,1],[-1,0,-1]])or equivalently	prob = svm_problem([1,-1],[{1:1,3:1},{1:-1,3:-1}])Once the parameter and problem are ready, we can construct the model:	m = svm_model(prob, param)To conduct n-fold cross validation; predicted labels in the validationprocess are returned.	target = cross_validation(prob, param, n)To predict a new sample with the model:	r = m.predict([1, 1, 1])To obtain decision values of predicting a sample:	d = m.predict_values([1, 1, 1])	To predict a new sample and obtain probability estimates;return value is a dict that maps labels to probabilities.	prd, prb = m.predict_probability([1, 1, 1])	sample of prd : 1.0	sample of prb : {1:0.6, -1:0.4}To obtain sigma of the probability density function for regression;see ../README for the definition of the function.	sigma = m.get_svr_probability()To obtain the probability density function for regression; see../README for the definition of the function.	pdf = m.get_svr_pdf()	probability = pdf(z)To save the model to a file:	m.save('test.model')and to load the model from a file:	m = svm_model('test.model')Examples========There are two examples in this package. The one is svm_test.py, and theother is test_cross_validation.py.svm_test.py tests various kernels on a three-class problem withC-SVM. It also demonstrates how to obtain decision values andprobability estimates.test_cross_validation.py demonstrates loading data from a file anddoes a ten-fold cross validation on the heart_scale dataset. It makesuse of cross_validation.py which calls the C++ cross validationsubroutine.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -