Class SupportVectorMachine
- All Implemented Interfaces:
Serializable,Cloneable
- Direct Known Subclasses:
SVClassification,SVOneClass,SVRegression
Support vector machines are machine learning algorithms for classification,
regression, and the so-called one class problem. This package contains
SVClassification, SVRegression, and
SVOneClass for each of these problems, respectively. Each
formulation uses a kernel based optimization to find support vectors that
optimally separate clouds of high dimensional data for the purpose of making
predictions. SupportVectorMachine and all of the classes in this
package are based on the LIBSVM package of Chang, Chih-Chung;
Lin, Chih-Jen (2011).
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classClass that wraps exceptions thrown by reflective operations in core reflection.Nested classes/interfaces inherited from class com.imsl.datamining.PredictiveModel
PredictiveModel.CloneNotSupportedException, PredictiveModel.PredictiveModelException, PredictiveModel.StateChangeException, PredictiveModel.SumOfProbabilitiesNotOneException, PredictiveModel.VariableType -
Constructor Summary
ConstructorsConstructorDescriptionSupportVectorMachine(double[][] xy, int responseColumnIndex, PredictiveModel.VariableType[] varType) Constructs a support vector machine for a single response variable and multiple predictor variables.SupportVectorMachine(double[][] xy, int responseColumnIndex, PredictiveModel.VariableType[] varType, Kernel k) Constructs a support vector machine for a single response variable and multiple predictor variables. -
Method Summary
Modifier and TypeMethodDescriptionvoidfitModel()Fits the model to the training data, i.e, trains the support vector machine.doubleReturns the convergence tolerance.protected KernelReturns the kernel object being used in the optimization.double[]Returns the kernel parameters.protected SVModelgetModel()Returns the model object.doubleReturns the value of the \(\nu\) parameter.doubleReturns the value of the regularization parameter, C.doubleReturns the setting for the work array size.booleanReturns the boolean to perform the \(\nu\)-formulation of the optimization problem.booleanReturns the boolean to calculate probability estimates.booleanReturns the boolean to perform shrinking during optimization.protected abstract SVModelAbstract method to perform the support vector machine optimization.double[]predict()Returns the predicted values on the training data, i.e., returns the fitted values.double[]predict(double[][] testData) Returns the predicted values on the input test data.protected abstract double[]predictValues(SVModel model, double[][] attributeData) Abstract method for generating the predicted values using the fitted support vector machine model.protected voidSets the configuration ofPredictiveModelto that of the input model.voidsetConvergenceTolerance(double eps) Sets the convergence tolerance.voidSets the kernel to be used in the optimization.voidsetKernelParameters(double[] kParams) Sets the kernel parameters.voidsetNuFormulation(boolean nuFormulation) Sets the boolean to perform the \(\nu\)-formulation of the optimization problem.voidsetNuParameter(double nu) Sets the value of \(\nu\) in the \(\nu\)-formulation of the optimization problem.voidsetProbability(boolean probability) Sets the boolean to calculate probability estimates.voidsetRegularizationParameter(double C) Sets the regularization parameter, C.voidsetShrinking(boolean shrinking) Sets the boolean to perform shrinking during optimization.voidsetWorkArraySize(double workSize) Sets the work array size.Methods inherited from class com.imsl.datamining.PredictiveModel
clone, getClassCounts, getClassErrors, getClassErrors, getClassLabels, getClassProbabilities, getCostMatrix, getMaxNumberOfCategories, getMaxNumberOfIterations, getNumberOfClasses, getNumberOfColumns, getNumberOfMissing, getNumberOfPredictors, getNumberOfRows, getNumberOfUniquePredictorValues, getPredictorIndexes, getPredictorTypes, getPrintLevel, getPriorProbabilities, getRandomObject, getResponseColumnIndex, getResponseVariableAverage, getResponseVariableMostFrequentClass, getResponseVariableType, getTotalWeight, getVariableType, getWeights, getXY, isConstantSeries, isMustFitModel, isUserFixedNClasses, setClassCounts, setClassLabels, setClassProbabilities, setCostMatrix, setMaxNumberOfCategories, setMaxNumberOfIterations, setMustFitModel, setNumberOfClasses, setPredictorIndex, setPredictorTypes, setPrintLevel, setPriorProbabilities, setRandomObject, setResponseColumnIndex, setTrainingData, setVariableType, setWeights
-
Constructor Details
-
SupportVectorMachine
public SupportVectorMachine(double[][] xy, int responseColumnIndex, PredictiveModel.VariableType[] varType) Constructs a support vector machine for a single response variable and multiple predictor variables.- Parameters:
xy- adoublematrix containing the training data and associated response valuesresponseColumnIndex- anintspecifying the column index of the response variablevarType- aPredictiveModel.VariableTypearray of length equal toxy[0].lengthcontaining the type of each variable
-
SupportVectorMachine
public SupportVectorMachine(double[][] xy, int responseColumnIndex, PredictiveModel.VariableType[] varType, Kernel k) Constructs a support vector machine for a single response variable and multiple predictor variables.- Parameters:
xy- adoublematrix containing the training data and associated response valuesresponseColumnIndex- anintspecifying the column index of the response variablevarType- aPredictiveModel.VariableTypearray of length equal toxy[0].lengthcontaining the type of each variablek- aKernel, the kernel function
-
-
Method Details
-
fitModel
Fits the model to the training data, i.e, trains the support vector machine.- Overrides:
fitModelin classPredictiveModel- Throws:
PredictiveModel.PredictiveModelException- an exception has occurred in the com.imsl.datamining.PredictiveModel. Superclass exceptions should be considered such as com.imsl.datamining.PredictiveModel.StateChangeException, com.imsl.datamining.PredictiveModel.SumOfProbabilitiesNotOneException, and com.imsl.datamining.supportvectormachine.SupportVectorMachine.ReflectiveOperationException.
-
setConfiguration
Sets the configuration ofPredictiveModelto that of the input model.Note that the input
PredictiveModelobject must be the same subclass ofPredictiveModelas is this instance.- Specified by:
setConfigurationin classPredictiveModel- Parameters:
pm- aPredictiveModelobject which is to have its attributes duplicated in this instance- Throws:
PredictiveModel.CloneNotSupportedException- ajava.lang.CloneNotSupportedExceptionhas occurred. The original exception has been added to theSupportVectorMachine.CloneNotSupportedExceptionas a suppressed exception.Default: The class uses its default configuration as described in the different methods.
-
predict
Returns the predicted values on the training data, i.e., returns the fitted values.- Specified by:
predictin classPredictiveModel- Returns:
- a
doublearray containing the fitted values on the training data - Throws:
PredictiveModel.SumOfProbabilitiesNotOneException- the sum of probabilities is not approximately one
-
predict
public double[] predict(double[][] testData) throws PredictiveModel.SumOfProbabilitiesNotOneException Returns the predicted values on the input test data.- Specified by:
predictin classPredictiveModel- Parameters:
testData- adoublematrix containing test dataNote:
testDatamust have the same number of columns asxyand the columns must be in the same arrangement as inxy.- Returns:
- a
doublearray containing the predicted values - Throws:
PredictiveModel.SumOfProbabilitiesNotOneException- the sum of probabilities is not approximately one
-
setKernel
Sets the kernel to be used in the optimization.See the class
Kernelfor details.- Parameters:
kernel- an instance of classKernelDefault: kernel=
RadialBasisKernel
-
getKernel
Returns the kernel object being used in the optimization.- Returns:
- a
Kernelobject
-
getConvergenceTolerance
public double getConvergenceTolerance()Returns the convergence tolerance.- Returns:
- a
double, the tolerance value
-
setConvergenceTolerance
public void setConvergenceTolerance(double eps) Sets the convergence tolerance.- Parameters:
eps- adouble, the tolerance valueDefault:
eps=0.001
-
setNuFormulation
public void setNuFormulation(boolean nuFormulation) Sets the boolean to perform the \(\nu\)-formulation of the optimization problem. Whentrue, the \(\nu\)-formulation of the optimization problem is solved. Whenfalse, the standard formulation is solved.The \(\nu\)-support vector classification (\(\nu\)-SVC) algorithm presents a new parameter \(\nu \in (0,1] \) which acts as an upper bound on the fraction of training errors and a lower bound on the fraction of support vectors.
The primal optimization problem for the binary variable \(y \in \{1,-1\} \) is
$$\min_ {w, b, \xi, \rho} \frac{1}{2} w^Tw-\nu\rho+\frac{1}{l}\sum_{i=1}^{l} \xi _i$$
$$\text{subject to} \, y_i(w^T \phi (x_i)+b) \geq \rho - \xi _i,$$
$$ \xi _i \geq 0, i=1, \, \ldots \,,\, l\,,\,\rho \geq 0$$
Similar to \(\nu\)-SVC, in \(\nu\)-support vector regression (\(\nu\)-SVR) the parameter \(\nu \in (0,1] \) controls the number of support vectors. The \(\nu\)-SVR primal problem is
$$\min_ {w, b, \xi, \xi^*, \varepsilon} \frac{1}{2} w^Tw+C(\nu\varepsilon + \frac{1}{l}\sum_{i=1}^{l} (\xi _i + {\xi _i}^*))$$
$$\text{subject to} \, \, (w^T \phi (x_i)+b)-z_i \leq \varepsilon +\xi _i,$$
$$z_i - (w^T \phi (x_i)+b) \leq \varepsilon +{\xi _i}^*,$$
$$ \xi_i,\xi_i^*\ge 0,i=1,\;\ldots,\;l,\varepsilon\ge 0 $$
- Parameters:
nuFormulation- aboolean, giving the value for the flagDefault:
nuFormulation=false
-
isNuFormulation
public boolean isNuFormulation()Returns the boolean to perform the \(\nu\)-formulation of the optimization problem. Whentrue, the \(\nu\)-formulation of the optimization problem is solved. Whenfalse, the standard or default formulation is solved.- Returns:
- a
boolean, the current value of the flag
-
getNuParameter
public double getNuParameter()Returns the value of the \(\nu\) parameter.- Returns:
- a
double, the current value of the \(\nu\) parameter
-
setNuParameter
public void setNuParameter(double nu) Sets the value of \(\nu\) in the \(\nu\)-formulation of the optimization problem.- Parameters:
nu- adoublein (0,1], the \(\nu\) parameterDefault:
nu=0.5
-
getWorkingArraySize
public double getWorkingArraySize()Returns the setting for the work array size.- Returns:
- a
double, the number of megabytes allocated to the work array used in the decomposition method
-
setWorkArraySize
public void setWorkArraySize(double workSize) Sets the work array size.A larger work array size can reduce the computational time of the decomposition method.
- Parameters:
workSize- adouble, the number of megabytes allocated for the array used during the decomposition method.workSizemust be greater than 0.Default:
workSize= 100.0
-
getKernelParameters
public double[] getKernelParameters()Returns the kernel parameters. See documentation for eachKernelclass for specifications of the parameters.- Returns:
- a
doublearray containing the kernel parameter values
-
setKernelParameters
public void setKernelParameters(double[] kParams) Sets the kernel parameters. See documentation for eachKernelclass for specifications of the parameters.- Parameters:
kParams- adoublearray containing the kernel parameter values
-
isProbability
public boolean isProbability()Returns the boolean to calculate probability estimates.- Returns:
- a
boolean, indicating whether or not probability estimates for each class level should be computed
-
setProbability
public void setProbability(boolean probability) Sets the boolean to calculate probability estimates.In classification problems, when the flag is
true, class probabilities are estimated during the training procedure. For regression problems, when the flag istrue, distributional parameters will be estimated that allow probability inferences for the response variable. Whenfalse, probability information is not calculated.- Parameters:
probability- aboolean, indicating whether or not to calculate probability estimatesDefault:
probability=false
-
getRegularizationParameter
public double getRegularizationParameter()Returns the value of the regularization parameter, C.- Returns:
- a
double, the value of the regularization parameter
-
setRegularizationParameter
public void setRegularizationParameter(double C) Sets the regularization parameter, C.- Parameters:
C- adouble, greater than 0.0Default:
C=1.0
-
isShrinking
public boolean isShrinking()Returns the boolean to perform shrinking during optimization. Whentrue, the optimization performs shrinking.- Returns:
- a
boolean, the value of theshrinkingflag
-
setShrinking
public void setShrinking(boolean shrinking) Sets the boolean to perform shrinking during optimization.The shrinking technique tries to identify and remove some bounded elements during the application of the SMO (sequential minimal optimization) algorithm, so that a smaller optimization problem is solved.
- Parameters:
shrinking- aboolean, indicating whether or not shrinking should be performed during the optimization algorithmDefault:
shrinking=true
-
getModel
Returns the model object.- Returns:
- an
SVModelobject
-
optimize
protected abstract SVModel optimize(DataNode[][] x, double[] y, double[] w, int len, Kernel kernel) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException Abstract method to perform the support vector machine optimization.- Parameters:
x- aDataNodematrix containing the attribute datay- adoublearray containing the response variablelen- anint, the total possible number of support vectorsw- adoublearray containing the observation weightskernel- anKernelobject- Returns:
- an
SVModelstructure containing the fitted model - Throws:
NoSuchMethodException- thrown when a particular method cannot be foundInstantiationException- thrown when an application tries to create an instance of a class using thenewInstancemethod in classClass, but the specified class object cannot be instantiatedIllegalAccessException- thrown when an application tries to reflectively create an instance (other than an array), set or get a field, or invoke a method, but the currently executing method does not have access to the definition of the specified class, field, method or constructorInvocationTargetException- a checked exception that wraps an exception thrown by an invoked method or constructor
-
predictValues
protected abstract double[] predictValues(SVModel model, double[][] attributeData) throws PredictiveModel.SumOfProbabilitiesNotOneException Abstract method for generating the predicted values using the fitted support vector machine model.- Parameters:
model- a fittedSVModelobjectattributeData- adoublematrix containing the attribute (or predictor) data- Returns:
- a
doublearray containing the predictions for each row in the input attribute data - Throws:
PredictiveModel.SumOfProbabilitiesNotOneException- the sum of probabilities is not approximately one
-