Click or drag to resize
Accord.NET (logo)

SupportVectorMachine Class

Linear Support Vector Machine (SVM).
Inheritance Hierarchy
SystemObject
  Accord.MachineLearningTransformBaseDouble, Boolean
    Accord.MachineLearningClassifierBaseDouble, Boolean
      Accord.MachineLearningBinaryClassifierBaseDouble
        Accord.MachineLearningBinaryScoreClassifierBaseDouble
          Accord.MachineLearningBinaryLikelihoodClassifierBaseDouble
            Accord.MachineLearning.VectorMachinesSupportVectorMachineLinear, Double
              Accord.MachineLearning.VectorMachinesSupportVectorMachineLinear
                Accord.MachineLearning.VectorMachinesSupportVectorMachine

Namespace:  Accord.MachineLearning.VectorMachines
Assembly:  Accord.MachineLearning (in Accord.MachineLearning.dll) Version: 3.8.0
Syntax
[SerializableAttribute]
public class SupportVectorMachine : SupportVectorMachine<Linear>, 
	IBinaryClassifier<double[]>, IMulticlassClassifier<double[]>, IMultilabelClassifier<double[]>, 
	IMultilabelClassifier<double[], int[]>, IClassifier<double[], int[]>, 
	IClassifier, ITransform<double[], int[]>, ICovariantTransform<double[], int[]>, 
	ITransform, IMultilabelClassifier<double[], bool[]>, IClassifier<double[], bool[]>, 
	ITransform<double[], bool[]>, ICovariantTransform<double[], bool[]>, 
	IMultilabelClassifier<double[], double[]>, IClassifier<double[], double[]>, 
	ITransform<double[], double[]>, ICovariantTransform<double[], double[]>, 
	IMulticlassClassifier<double[], int>, IClassifier<double[], int>, 
	ITransform<double[], int>, ICovariantTransform<double[], int>, 
	IMulticlassClassifier<double[], double>, IClassifier<double[], double>, 
	ITransform<double[], double>, ICovariantTransform<double[], double>, 
	IClassifier<double[], bool>, ITransform<double[], bool>, 
	ICovariantTransform<double[], bool>, ISupportVectorMachine<double[]>, 
	IBinaryLikelihoodClassifier<double[]>, IBinaryScoreClassifier<double[]>, IMulticlassOutScoreClassifier<double[], bool>, 
	IMulticlassScoreClassifierBase<double[], bool>, IMultilabelOutScoreClassifier<double[], bool>, 
	IMultilabelScoreClassifierBase<double[], bool>, IMulticlassScoreClassifier<double[]>, 
	IMulticlassScoreClassifier<double[], int>, IMulticlassOutScoreClassifier<double[], int>, 
	IMulticlassScoreClassifierBase<double[], int>, IMultilabelOutScoreClassifier<double[], int>, 
	IMultilabelScoreClassifierBase<double[], int>, IMulticlassRefScoreClassifier<double[], int[]>, 
	IMultilabelRefScoreClassifier<double[], int[]>, IMultilabelScoreClassifierBase<double[], int[]>, 
	IMulticlassScoreClassifier<double[], double>, IMulticlassOutScoreClassifier<double[], double>, 
	IMulticlassScoreClassifierBase<double[], double>, IMultilabelOutScoreClassifier<double[], double>, 
	IMultilabelScoreClassifierBase<double[], double>, IMulticlassRefScoreClassifier<double[], double[]>, 
	IMultilabelRefScoreClassifier<double[], double[]>, IMultilabelScoreClassifierBase<double[], double[]>, 
	IMulticlassRefScoreClassifier<double[], bool[]>, IMultilabelRefScoreClassifier<double[], bool[]>, 
	IMultilabelScoreClassifierBase<double[], bool[]>, IMultilabelScoreClassifier<double[]>, 
	IMultilabelScoreClassifier<double[], int>, IMultilabelScoreClassifier<double[], double>, 
	IMulticlassOutLikelihoodClassifier<double[], bool>, IMultilabelOutLikelihoodClassifier<double[], bool>, 
	IMultilabelLikelihoodClassifierBase<double[], bool>, IMulticlassLikelihoodClassifierBase<double[], bool>, 
	IMulticlassLikelihoodClassifier<double[]>, IMulticlassLikelihoodClassifier<double[], int>, 
	IMulticlassOutLikelihoodClassifier<double[], int>, IMultilabelOutLikelihoodClassifier<double[], int>, 
	IMultilabelLikelihoodClassifierBase<double[], int>, IMulticlassLikelihoodClassifierBase<double[], int>, 
	IMulticlassRefLikelihoodClassifier<double[], int[]>, IMultilabelRefLikelihoodClassifier<double[], int[]>, 
	IMultilabelLikelihoodClassifierBase<double[], int[]>, IMulticlassLikelihoodClassifier<double[], double>, 
	IMulticlassOutLikelihoodClassifier<double[], double>, IMultilabelOutLikelihoodClassifier<double[], double>, 
	IMultilabelLikelihoodClassifierBase<double[], double>, IMulticlassLikelihoodClassifierBase<double[], double>, 
	IMulticlassRefLikelihoodClassifier<double[], double[]>, IMultilabelRefLikelihoodClassifier<double[], double[]>, 
	IMultilabelLikelihoodClassifierBase<double[], double[]>, IMulticlassRefLikelihoodClassifier<double[], bool[]>, 
	IMultilabelRefLikelihoodClassifier<double[], bool[]>, IMultilabelLikelihoodClassifierBase<double[], bool[]>, 
	IMultilabelLikelihoodClassifier<double[]>, IMultilabelLikelihoodClassifier<double[], int>, 
	IMultilabelLikelihoodClassifier<double[], double>
Request Example View Source

The SupportVectorMachine type exposes the following members.

Constructors
  NameDescription
Public methodSupportVectorMachine
Initializes a new instance of the SupportVectorMachine class.
Top
Properties
  NameDescription
Public propertyInputs Obsolete.
Gets the number of inputs accepted by this machine.
(Inherited from SupportVectorMachineTKernel, TInput.)
Public propertyIsCompact Obsolete.
Obsolete.
(Inherited from SupportVectorMachineTKernel, TInput.)
Public propertyIsProbabilistic
Gets whether this machine has been calibrated to produce probabilistic outputs (through the Probability(TInput) method).
(Inherited from SupportVectorMachineTKernel, TInput.)
Public propertyKernel
Gets or sets the kernel used by this machine.
(Inherited from SupportVectorMachineTKernel, TInput.)
Public propertyNumberOfClasses
Gets the number of classes expected and recognized by the classifier.
(Inherited from ClassifierBaseTInput, TClasses.)
Public propertyNumberOfInputs
Gets the number of inputs accepted by the model.
(Inherited from TransformBaseTInput, TOutput.)
Public propertyNumberOfOutputs
Gets the number of outputs generated by the model.
(Inherited from TransformBaseTInput, TOutput.)
Public propertySupportVectors
Gets or sets the collection of support vectors used by this machine.
(Inherited from SupportVectorMachineTKernel, TInput.)
Public propertyThreshold
Gets or sets the threshold (bias) term for this machine.
(Inherited from SupportVectorMachineTKernel, TInput.)
Public propertyWeights
Gets or sets the collection of weights used by this machine.
(Inherited from SupportVectorMachineTKernel, TInput.)
Top
Methods
  NameDescription
Public methodClone
Creates a new object that is a copy of the current instance.
(Overrides SupportVectorMachineTKernelClone.)
Public methodCompress
If this machine has a linear kernel, compresses all support vectors into a single parameter vector.
(Inherited from SupportVectorMachineTKernel, TInput.)
Public methodCompute(TInput) Obsolete.
Computes the given input to produce the corresponding output.
(Inherited from SupportVectorMachineTKernel, TInput.)
Public methodCompute(TInput, Double) Obsolete.
Computes the given input to produce the corresponding output.
(Inherited from SupportVectorMachineTKernel, TInput.)
Public methodDecide(TInput)
Computes class-label decisions for a given set of input vectors.
(Inherited from ClassifierBaseTInput, TClasses.)
Public methodDecide(TInput)
Computes a class-label decision for a given input.
(Inherited from SupportVectorMachineTKernel, TInput.)
Public methodDecide(TInput, Boolean)
Computes class-label decisions for the given input.
(Inherited from BinaryClassifierBaseTInput.)
Public methodDecide(TInput, Boolean)
Computes a class-label decision for a given input.
(Inherited from BinaryScoreClassifierBaseTInput.)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodStatic memberFromLogisticRegression
Performs an explicit conversion from LogisticRegression to SupportVectorMachine.
Public methodStatic memberFromRegression
Performs an explicit conversion from MultipleLinearRegression to SupportVectorMachine.
Public methodStatic memberFromWeights
Creates a new linear SupportVectorMachine with the given set of linear weights.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodLogLikelihood(TInput)
Predicts a class label vector for the given input vector, returning the log-likelihood that the input vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodLogLikelihood(TInput)
Predicts a class label vector for the given input vector, returning the log-likelihood that the input vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodLogLikelihood(TInput, Boolean)
Predicts a class label vector for the given input vector, returning the log-likelihood that the input vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodLogLikelihood(TInput, Int32)
Predicts a class label for each input vector, returning the log-likelihood that each vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodLogLikelihood(TInput, Double)
Predicts a class label vector for the given input vectors, returning the log-likelihood that the input vector belongs to its predicted class.
(Inherited from SupportVectorMachineTKernel, TInput.)
Public methodLogLikelihood(TInput, Boolean, Double)
Predicts a class label for each input vector, returning the log-likelihood that each vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodLogLikelihoods(TInput)
Computes the log-likelihood that the given input vector belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodLogLikelihoods(TInput)
Computes the log-likelihoods that the given input vectors belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodLogLikelihoods(TInput, Boolean)
Predicts a class label vector for the given input vector, returning the log-likelihoods of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodLogLikelihoods(TInput, Double)
Computes the log-likelihood that the given input vector belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodLogLikelihoods(TInput, Double)
Computes the log-likelihoods that the given input vectors belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodLogLikelihoods(TInput, Int32)
Predicts a class label vector for each input vector, returning the log-likelihoods of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodLogLikelihoods(TInput, Boolean, Double)
Predicts a class label vector for the given input vector, returning the log-likelihoods of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodLogLikelihoods(TInput, Boolean, Double)
Predicts a class label vector for each input vector, returning the log-likelihoods of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodProbabilities(TInput)
Computes the probabilities that the given input vector belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodProbabilities(TInput)
Computes the probabilities that the given input vectors belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodProbabilities(TInput, Boolean)
Predicts a class label vector for the given input vector, returning the probabilities of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodProbabilities(TInput, Double)
Computes the probabilities that the given input vector belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodProbabilities(TInput, Double)
Computes the probabilities that the given input vectors belongs to each of the possible classes.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodProbabilities(TInput, Int32)
Predicts a class label vector for each input vector, returning the probabilities of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodProbabilities(TInput, Boolean, Double)
Predicts a class label vector for the given input vector, returning the probabilities of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodProbabilities(TInput, Boolean, Double)
Predicts a class label vector for each input vector, returning the probabilities of the input vector belonging to each possible class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodProbability(TInput)
Predicts a class label for the given input vector, returning the probability that the input vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodProbability(TInput)
Predicts a class label for the given input vector, returning the probability that the input vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodProbability(TInput, Boolean)
Predicts a class label for the given input vector, returning the probability that the input vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodProbability(TInput, Double)
Predicts a class label for the given input vector, returning the probability that the input vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodProbability(TInput, Int32)
Predicts a class label for each input vector, returning the probability that each vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodProbability(TInput, Boolean, Double)
Predicts a class label for each input vector, returning the probability that each vector belongs to its predicted class.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodScore(TInput)
Computes a numerical score measuring the association between the given input vector and its most strongly associated class (as predicted by the classifier).
(Inherited from BinaryScoreClassifierBaseTInput.)
Public methodScore(TInput)
Computes a numerical score measuring the association between the given input vector and its most strongly associated class (as predicted by the classifier).
(Inherited from BinaryScoreClassifierBaseTInput.)
Public methodScore(TInput, Boolean)
Predicts a class label for the input vector, returning a numerical score measuring the strength of association of the input vector to its most strongly related class.
(Inherited from BinaryScoreClassifierBaseTInput.)
Public methodScore(TInput, Boolean)
Predicts a class label for each input vector, returning a numerical score measuring the strength of association of the input vector to the most strongly related class.
(Inherited from BinaryScoreClassifierBaseTInput.)
Public methodScore(TInput, Double)
Computes a numerical score measuring the association between the given input vector and each class.
(Inherited from SupportVectorMachineTKernel, TInput.)
Public methodScore(TInput, Boolean, Double)
Predicts a class label for each input vector, returning a numerical score measuring the strength of association of the input vector to the most strongly related class.
(Inherited from BinaryScoreClassifierBaseTInput.)
Public methodScores(TInput)
Computes a numerical score measuring the association between the given input vector and each class.
(Inherited from BinaryScoreClassifierBaseTInput.)
Public methodScores(TInput)
Computes a numerical score measuring the association between the given input vector and each class.
(Inherited from BinaryScoreClassifierBaseTInput.)
Public methodScores(TInput, Boolean)
Predicts a class label vector for the given input vector, returning a numerical score measuring the strength of association of the input vector to each of the possible classes.
(Inherited from BinaryScoreClassifierBaseTInput.)
Public methodScores(TInput, Double)
Computes a numerical score measuring the association between the given input vector and each class.
(Inherited from BinaryScoreClassifierBaseTInput.)
Public methodScores(TInput, Boolean)
Predicts a class label vector for each input vector, returning a numerical score measuring the strength of association of the input vector to each of the possible classes.
(Inherited from BinaryScoreClassifierBaseTInput.)
Public methodScores(TInput, Double)
Computes a numerical score measuring the association between the given input vector and each class.
(Inherited from BinaryScoreClassifierBaseTInput.)
Public methodScores(TInput, Boolean, Double)
Predicts a class label vector for the given input vector, returning a numerical score measuring the strength of association of the input vector to each of the possible classes.
(Inherited from BinaryScoreClassifierBaseTInput.)
Public methodScores(TInput, Boolean, Double)
Predicts a class label vector for each input vector, returning a numerical score measuring the strength of association of the input vector to each of the possible classes.
(Inherited from BinaryScoreClassifierBaseTInput.)
Public methodToMulticlass
Views this instance as a multi-class generative classifier, giving access to more advanced methods, such as the prediction of integer labels.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodToMultilabel
Views this instance as a multi-label generative classifier, giving access to more advanced methods, such as the prediction of one-hot vectors.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodToWeights
Converts a Linear-kernel machine into an array of linear coefficients. The first position in the array is the Threshold value. If this machine is not linear, an exception will be thrown.
(Inherited from SupportVectorMachineTKernel.)
Public methodTransform(TInput)
Applies the transformation to an input, producing an associated output.
(Inherited from ClassifierBaseTInput, TClasses.)
Public methodTransform(TInput)
Applies the transformation to a set of input vectors, producing an associated set of output vectors.
(Inherited from TransformBaseTInput, TOutput.)
Public methodTransform(TInput, Boolean)
Applies the transformation to an input, producing an associated output.
(Inherited from BinaryClassifierBaseTInput.)
Public methodTransform(TInput, Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from BinaryClassifierBaseTInput.)
Public methodTransform(TInput, Boolean)
Applies the transformation to an input, producing an associated output.
(Inherited from BinaryClassifierBaseTInput.)
Public methodTransform(TInput, Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from BinaryClassifierBaseTInput.)
Public methodTransform(TInput, Int32)
Applies the transformation to an input, producing an associated output.
(Inherited from BinaryClassifierBaseTInput.)
Public methodTransform(TInput, Double)
Applies the transformation to a set of input vectors, producing an associated set of output vectors.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodTransform(TInput, Double)
Applies the transformation to a set of input vectors, producing an associated set of output vectors.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodTransform(TInput, Double)
Applies the transformation to a set of input vectors, producing an associated set of output vectors.
(Inherited from BinaryLikelihoodClassifierBaseTInput.)
Public methodTransform(TInput, TClasses)
Applies the transformation to an input, producing an associated output.
(Inherited from ClassifierBaseTInput, TClasses.)
Top
Operators
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Remarks
Examples

The first example shows how to learn a linear SVM. However, since the problem being learned is not linearly separable, the classifier will not be able to produce a perfect decision boundary.

// As an example, we will try to learn a linear machine  that can 
// replicate the "exclusive-or" logical function. However, since we
// will be using a linear SVM, we will not be able to solve this 
// problem perfectly as the XOR is a non-linear classification problem:
double[][] inputs =
{
    new double[] { 0, 0 }, // the XOR function takes two booleans
    new double[] { 0, 1 }, // and computes their exclusive or: the
    new double[] { 1, 0 }, // output is true only if the two booleans
    new double[] { 1, 1 }  // are different
};

int[] xor = // this is the output of the xor function
{
    0, // 0 xor 0 = 0 (inputs are equal)
    1, // 0 xor 1 = 1 (inputs are different)
    1, // 1 xor 0 = 1 (inputs are different)
    0, // 1 xor 1 = 0 (inputs are equal)
};

// Now, we can create the sequential minimal optimization teacher
var learn = new SequentialMinimalOptimization()
{
    UseComplexityHeuristic = true,
    UseKernelEstimation = false
};

// And then we can obtain a trained SVM by calling its Learn method
SupportVectorMachine svm = learn.Learn(inputs, xor);

// Finally, we can obtain the decisions predicted by the machine:
bool[] prediction = svm.Decide(inputs);

The second example shows how to learn an SVM using a standard kernel that operates on vectors of doubles. With kernels, it is possible to produce non-linear boundaries that perfectly separate the data.

// As an example, we will try to learn a decision machine 
// that can replicate the "exclusive-or" logical function:

double[][] inputs =
{
    new double[] { 0, 0 }, // the XOR function takes two booleans
    new double[] { 0, 1 }, // and computes their exclusive or: the
    new double[] { 1, 0 }, // output is true only if the two booleans
    new double[] { 1, 1 }  // are different
};

int[] xor = // this is the output of the xor function
{
    0, // 0 xor 0 = 0 (inputs are equal)
    1, // 0 xor 1 = 1 (inputs are different)
    1, // 1 xor 0 = 1 (inputs are different)
    0, // 1 xor 1 = 0 (inputs are equal)
};

// Now, we can create the sequential minimal optimization teacher
var learn = new SequentialMinimalOptimization<Gaussian>()
{
    UseComplexityHeuristic = true,
    UseKernelEstimation = true
};

// And then we can obtain a trained SVM by calling its Learn method
SupportVectorMachine<Gaussian> svm = learn.Learn(inputs, xor);

// Finally, we can obtain the decisions predicted by the machine:
bool[] prediction = svm.Decide(inputs);

The third example shows how to learn an SVM using a Sparse kernel that operates on sparse vectors.

// As an example, we will try to learn a decision machine 
// that can replicate the "exclusive-or" logical function:

Sparse<double>[] inputs =
{
    Sparse.FromDense(new double[] { 0, 0 }), // the XOR function takes two booleans
    Sparse.FromDense(new double[] { 0, 1 }), // and computes their exclusive or: the
    Sparse.FromDense(new double[] { 1, 0 }), // output is true only if the two booleans
    Sparse.FromDense(new double[] { 1, 1 })  // are different
};

int[] xor = // this is the output of the xor function
{
    0, // 0 xor 0 = 0 (inputs are equal)
    1, // 0 xor 1 = 1 (inputs are different)
    1, // 1 xor 0 = 1 (inputs are different)
    0, // 1 xor 1 = 0 (inputs are equal)
};

// Now, we can create the sequential minimal optimization teacher
var learn = new SequentialMinimalOptimization<Gaussian, Sparse<double>>()
{
    UseComplexityHeuristic = true,
    UseKernelEstimation = true
};

// And then we can obtain a trained SVM by calling its Learn method
var svm = learn.Learn(inputs, xor);

// Finally, we can obtain the decisions predicted by the machine:
bool[] prediction = svm.Decide(inputs);
See Also