支持向量机

目录

简介

类摘要

SVM

class SVM {

/* Constants */

const integer SVM::C_SVC = 0 ;

const integer SVM::NU_SVC = 1 ;

const integer SVM::ONE_CLASS = 2 ;

const integer SVM::EPSILON_SVR = 3 ;

const integer SVM::NU_SVR = 4 ;

const integer SVM::KERNEL_LINEAR = 0 ;

const integer SVM::KERNEL_POLY = 1 ;

const integer SVM::KERNEL_RBF = 2 ;

const integer SVM::KERNEL_SIGMOID = 3 ;

const integer SVM::KERNEL_PRECOMPUTED = 4 ;

const integer SVM::OPT_TYPE = 101 ;

const integer SVM::OPT_KERNEL_TYPE = 102 ;

const integer SVM::OPT_DEGREE = 103 ;

const integer SVM::OPT_SHRINKING = 104 ;

const integer SVM::OPT_PROPABILITY = 105 ;

const integer SVM::OPT_GAMMA = 201 ;

const integer SVM::OPT_NU = 202 ;

const integer SVM::OPT_EPS = 203 ;

const integer SVM::OPT_P = 204 ;

const integer SVM::OPT_COEF_ZERO = 205 ;

const integer SVM::OPT_C = 206 ;

const integer SVM::OPT_CACHE_SIZE = 207 ;

/* Methods */

public __construct ( void )

public float svm::crossvalidate ( array $problem , int $number_of_folds )

public array getOptions ( void )

public bool setOptions ( array $params )

public SVMModel svm::train ( 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 SVM::__construct ( 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 ( 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 ( 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 ( 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 ( 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 ( void )

public __construct ([ string $filename ] )

public array getLabels ( void )

public int getNrClass ( void )

public int getSvmType ( void )

public float getSvrProbability ( void )

public bool load ( string $filename )

public float predict_probability ( array $data )

public float predict ( array $data )

public bool save ( string $filename )

}

SVMModel::checkProbabilityModel

Returns true if the model has probability information

说明

public bool SVMModel::checkProbabilityModel ( void )

Returns true if the model contains probability information.

参数

此函数没有参数。

返回值

Return a boolean value

SVMModel::__construct

Construct a new SVMModel

说明

public SVMModel::__construct ([ 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 ( 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 SVMModel::getNrClass ( 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 SVMModel::getSvmType ( 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 ( 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 ( 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 ( 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 ( 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 ( 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