Book/svm-Phpdoc专题
支持向量机
目录
- 简介
- 安装/配置
- 范例
- SVM — The SVM class
- SVM::__construct — Construct a new SVM object
- SVM::crossvalidate — Test training params on subsets of the training data
- SVM::getOptions — Return the current training parameters
- SVM::setOptions — Set training parameters
- SVM::train — Create a SVMModel based on training data
- SVMModel — The SVMModel class
- SVMModel::checkProbabilityModel — Returns true if the model has probability information
- SVMModel::__construct — Construct a new SVMModel
- SVMModel::getLabels — Get the labels the model was trained on
- SVMModel::getNrClass — Returns the number of classes the model was trained with
- SVMModel::getSvmType — Get the SVM type the model was trained with
- SVMModel::getSvrProbability — Get the sigma value for regression types
- SVMModel::load — Load a saved SVM Model
- SVMModel::predict_probability — Return class probabilities for previous unseen data
- SVMModel::predict — Predict a value for previously unseen data
- SVMModel::save — Save a model to a file
简介
类摘要
SVM
class SVM {
/* Constants */
const int
SVM::C_SVC = 0 ;
const int
SVM::NU_SVC = 1 ;
const int
SVM::ONE_CLASS = 2 ;
const int
SVM::EPSILON_SVR = 3 ;
const int
SVM::NU_SVR = 4 ;
const int
SVM::KERNEL_LINEAR = 0 ;
const int
SVM::KERNEL_POLY = 1 ;
const int
SVM::KERNEL_RBF = 2 ;
const int
SVM::KERNEL_SIGMOID = 3 ;
const int
SVM::KERNEL_PRECOMPUTED = 4 ;
const int
SVM::OPT_TYPE = 101 ;
const int
SVM::OPT_KERNEL_TYPE = 102 ;
const int
SVM::OPT_DEGREE = 103 ;
const int
SVM::OPT_SHRINKING = 104 ;
const int
SVM::OPT_PROPABILITY = 105 ;
const int
SVM::OPT_GAMMA = 201 ;
const int
SVM::OPT_NU = 202 ;
const int
SVM::OPT_EPS = 203 ;
const int
SVM::OPT_P = 204 ;
const int
SVM::OPT_COEF_ZERO = 205 ;
const int
SVM::OPT_C = 206 ;
const int
SVM::OPT_CACHE_SIZE = 207 ;
/* Methods */
public <span class="methodname">__construct ( <span class="methodparam">void )
public float
svm::crossvalidate ( <span
class="methodparam">array $problem ,
int
$number_of_folds )
public array getOptions ( <span class="methodparam">void )
public bool
setOptions ( <span
class="methodparam">array $params )
public SVMModel
svm::train ( <span
class="methodparam">array $problem
[, array
$weights ] )
}
预定义常量
SVM Constants
SVM::C_SVC
The basic C_SVC SVM type. The default, and a good starting point
SVM::NU_SVC
The NU_SVC type uses a different, more flexible, error weighting
SVM::ONE_CLASS
One class SVM type. Train just on a single class, using outliers as
negative examples
SVM::EPSILON_SVR
A SVM type for regression (predicting a value rather than just a class)
SVM::NU_SVR
A NU style SVM regression type
SVM::KERNEL_LINEAR
A very simple kernel, can work well on large document classification
problems
SVM::KERNEL_POLY
A polynomial kernel
SVM::KERNEL_RBF
The common Gaussian RBD kernel. Handles non-linear problems well and is
a good default for classification
SVM::KERNEL_SIGMOID
A kernel based on the sigmoid function. Using this makes the SVM very
similar to a two layer sigmoid based neural network
SVM::KERNEL_PRECOMPUTED
A precomputed kernel - currently unsupported.
SVM::OPT_TYPE
The options key for the SVM type
SVM::OPT_KERNEL_TYPE
The options key for the kernel type
SVM::OPT_DEGREE
SVM::OPT_SHRINKING
Training parameter, boolean, for whether to use the shrinking heuristics
SVM::OPT_PROBABILITY
Training parameter, boolean, for whether to collect and use probability
estimates
SVM::OPT_GAMMA
Algorithm parameter for Poly, RBF and Sigmoid kernel types.
SVM::OPT_NU
The option key for the nu parameter, only used in the NU_ SVM types
SVM::OPT_EPS
The option key for the Epsilon parameter, used in epsilon regression
SVM::OPT_P
Training parameter used by Episilon SVR regression
SVM::OPT_COEF_ZERO
Algorithm parameter for poly and sigmoid kernels
SVM::OPT_C
The option for the cost parameter that controls tradeoff between errors
and generality - effectively the penalty for misclassifying training
examples.
SVM::OPT_CACHE_SIZE
Memory cache size, in MB
SVM::__construct
Construct a new SVM object
说明
public <span class="methodname">SVM::__construct ( <span class="methodparam">void )
Constructs a new SVM object ready to accept training data.
参数
此函数没有参数。
返回值
Throws SVMException if the libsvm library could not be loaded
SVM::crossvalidate
Test training params on subsets of the training data
说明
public float
svm::crossvalidate ( <span
class="methodparam">array $problem ,
int
$number_of_folds )
Crossvalidate can be used to test the effectiveness of the current parameter set on a subset of the training data. Given a problem set and a n "folds", it separates the problem set into n subsets, and the repeatedly trains on one subset and tests on another. While the accuracy will generally be lower than a SVM trained on the enter data set, the accuracy score returned should be relatively useful, so it can be used to test different training parameters.
参数
problem
The problem data. This can either be in the form of an array, the URL of
an SVMLight formatted file, or a stream to an opened SVMLight formatted
datasource.
number_of_folds
The number of sets the data should be divided into and cross tested. A
higher number means smaller training sets and less reliability. 5 is a
good number to start with.
返回值
The correct percentage, expressed as a floating point number from 0-1. In the case of NU_SVC or EPSILON_SVR kernels the mean squared error will returned instead.
参见
- SVM::train
SVM::getOptions
Return the current training parameters
说明
public array SVM::getOptions ( <span class="methodparam">void )
Retrieve an array containing the training parameters. The parameters will be keyed on the predefined SVM constants.
参数
此函数没有参数。
返回值
Returns an array of configuration settings.
SVM::setOptions
Set training parameters
说明
public bool
SVM::setOptions ( <span
class="methodparam">array $params )
Set one or more training parameters.
参数
params
An array of training parameters, keyed on the SVM constants.
返回值
Return true on success, throws SVMException on error.
SVM::train
Create a SVMModel based on training data
说明
public SVMModel
svm::train ( <span
class="methodparam">array $problem
[, array
$weights ] )
Train a support vector machine based on the supplied training data.
参数
problem
The problem can be provided in three different ways. An array, where the
data should start with the class label (usually 1 or -1) then followed
by a sparse data set of dimension => data pairs. A URL to a file
containing a SVM Light formatted problem, with the each line being a new
training example, the start of each line containing the class (1, -1)
then a series of tab separated data values shows as key:value. A opened
stream pointing to a data source formatted as in the file above.
weights
Weights are an optional set of weighting parameters for the different
classes, to help account for unbalanced training sets. For example, if
the classes were 1 and -1, and -1 had significantly more example than
one, the weight for -1 could be 0.5. Weights should be in the range 0-1.
返回值
Returns an SVMModel that can be used to classify previously unseen data. Throws SVMException on error
简介
The SVMModel is the end result of the training process. It can be used to classify previously unseen data.
类摘要
SVMModel
class SVMModel {
/* Methods */
public bool checkProbabilityModel ( <span class="methodparam">void )
public <span
class="methodname">__construct ([ <span
class="methodparam">string $filename
] )
public array getLabels ( <span class="methodparam">void )
public int <span class="methodname">getNrClass ( <span class="methodparam">void )
public int <span class="methodname">getSvmType ( <span class="methodparam">void )
public float getSvrProbability ( <span class="methodparam">void )
public bool
load ( <span
class="type">string $filename )
public float
predict_probability ( <span
class="methodparam">array $data )
public float
predict ( <span
class="methodparam">array $data )
public bool
save ( <span
class="type">string $filename )
}
SVMModel::checkProbabilityModel
Returns true if the model has probability information
说明
public bool SVMModel::checkProbabilityModel ( <span class="methodparam">void )
Returns true if the model contains probability information.
参数
此函数没有参数。
返回值
Return a boolean value
SVMModel::__construct
Construct a new SVMModel
说明
public <span
class="methodname">SVMModel::__construct ([ <span
class="methodparam">string $filename
] )
Build a new SVMModel. Models will usually be created from the SVM::train function, but then saved models may be restored directly.
参数
filename
The filename for the saved model file this model should load.
返回值
Throws SVMException on error
参见
- SVMModel::load
SVMModel::getLabels
Get the labels the model was trained on
说明
public array SVMModel::getLabels ( <span class="methodparam">void )
Return an array of labels that the model was trained on. For regression and one class models an empty array is returned.
参数
此函数没有参数。
返回值
Return an array of labels
参见
- SVMModel::getNrClass
SVMModel::getNrClass
Returns the number of classes the model was trained with
说明
public int <span class="methodname">SVMModel::getNrClass ( <span class="methodparam">void )
Returns the number of classes the model was trained with, will return 2 for one class and regression models.
参数
此函数没有参数。
返回值
Return an integer number of classes
SVMModel::getSvmType
Get the SVM type the model was trained with
说明
public int <span class="methodname">SVMModel::getSvmType ( <span class="methodparam">void )
Returns an integer value representing the type of the SVM model used, e.g SVM::C_SVC.
参数
此函数没有参数。
返回值
Return an integer SVM type
SVMModel::getSvrProbability
Get the sigma value for regression types
说明
public float SVMModel::getSvrProbability ( <span class="methodparam">void )
For regression models, returns a sigma value. If there is no probability information or the model is not SVR, 0 is returned.
参数
此函数没有参数。
返回值
Returns a sigma value
SVMModel::load
Load a saved SVM Model
说明
public bool
SVMModel::load ( <span
class="methodparam">string $filename
)
Load a model file ready for classification or regression.
参数
filename
The filename of the model.
返回值
Throws SVMException on error. Returns true on success.
参见
- SVMModel::save
SVMModel::predict_probability
Return class probabilities for previous unseen data
说明
public float
SVMModel::predict_probability ( <span
class="methodparam">array $data )
This function accepts an array of data and attempts to predict the class, as with the predict function. Additionally, however, this function returns an array of probabilities, one per class in the model, which represent the estimated chance of the data supplied being a member of that class. Requires that the model to be used has been trained with the probability parameter set to true.
参数
data
The array to be classified. This should be a series of key => value
pairs in increasing key order, but not necessarily continuous.
probabilities
The supplied value will be filled with the probabilities. This will be
either null, in the case of a model without probability information, or
an array where the index is the class name and the value the predicted
probability.
返回值
Float the predicted value. This will be a class label in the case of classification, a real value in the case of regression. Throws SVMException on error
参见
- SVM::predict
SVMModel::predict
Predict a value for previously unseen data
说明
public float
SVMModel::predict ( <span
class="methodparam">array $data )
This function accepts an array of data and attempts to predict the class or regression value based on the model extracted from previously trained data.
参数
data
The array to be classified. This should be a series of key => value
pairs in increasing key order, but not necessarily continuous.
返回值
Float the predicted value. This will be a class label in the case of classification, a real value in the case of regression. Throws SVMException on error
参见
- SVM::train
SVMModel::save
Save a model to a file
说明
public bool
SVMModel::save ( <span
class="methodparam">string $filename
)
Save the model data to a file, for later use.
参数
filename
The file to save the model to.
返回值
Throws SVMException on error. Returns true on success.
参见
- SVMModel::load