Since: 7.0

class SVM (View source)

Support Vector Machine Library

LibSVM is an efficient solver for SVM classification and regression problems. The svm extension wraps this in a PHP interface for easy use in PHP scripts.

Constants

C_SVC

The basic C_SVC SVM type. The default, and a good starting point

NU_SVC

NU_SVC type uses a different, more flexible, error weighting

ONE_CLASS

One class SVM type. Train just on a single class, using outliers as negative examples

EPSILON_SVR

A SVM type for regression (predicting a value rather than just a class)

NU_SVR

A NU style SVM regression type

KERNEL_LINEAR

A very simple kernel, can work well on large document classification problems

KERNEL_POLY

A polynomial kernel

KERNEL_RBF

The common Gaussian RBD kernel. Handles non-linear problems well and is a good default for classification

KERNEL_SIGMOID

A kernel based on the sigmoid function. Using this makes the SVM very similar to a two layer sigmoid based neural network

KERNEL_PRECOMPUTED

A precomputed kernel - currently unsupported.

OPT_TYPE

The options key for the SVM type

OPT_KERNEL_TYPE

The options key for the kernel type

OPT_DEGREE

OPT_SHRINKING

Training parameter, boolean, for whether to use the shrinking heuristics

OPT_PROPABILITY

Training parameter, boolean, for whether to collect and use probability estimates

OPT_GAMMA

Algorithm parameter for Poly, RBF and Sigmoid kernel types.

OPT_NU

The option key for the nu parameter, only used in the NU_ SVM types

OPT_EPS

The option key for the Epsilon parameter, used in epsilon regression

OPT_P

Training parameter used by Episilon SVR regression

OPT_COEF_ZERO

Algorithm parameter for poly and sigmoid kernels

OPT_C

The option for the cost parameter that controls tradeoff between errors and generality - effectively the penalty for misclassifying training examples.

OPT_CACHE_SIZE

Memory cache size, in MB

Methods

__construct()

Construct a new SVM object

float
crossvalidate(array $problem, int $number_of_folds)

Test training params on subsets of the training data

array
getOptions()

Return the current training parameters

bool
setOptions(array $params)

Set training parameters

train(array $problem, array $weights = null)

Create a SVMModel based on training data

Details

__construct()

Construct a new SVM object

Constructs a new SVM object ready to accept training data.

Exceptions

SVMException

float crossvalidate(array $problem, int $number_of_folds)

Test training params on subsets of the training data

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.

Parameters

array $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.

int $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.

Return Value

float

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.

array getOptions()

Return the current training parameters

Retrieve an array containing the training parameters. The parameters will be keyed on the predefined SVM constants.

Return Value

array

Returns an array of configuration settings.

bool setOptions(array $params)

Set training parameters

Set one or more training parameters.

Parameters

array $params

An array of training parameters, keyed on the SVM constants.

Return Value

bool

Return true on success, throws SVMException on error.

Exceptions

SVMException

SVMModel train(array $problem, array $weights = null)

Create a SVMModel based on training data

Train a support vector machine based on the supplied training data.

Parameters

array $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.

array $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.

Return Value

SVMModel

Returns an SVMModel that can be used to classify previously unseen data. Throws SVMException on error

Exceptions

SMVException