LinearSupportVectorClassifier<T>

Models & Types · AiDotNet.Classification.SVM

Linear Support Vector Classifier optimized for linear classification.

Inheritance: ClassifierBase<T> → ProbabilisticClassifierBase<T> → SVMBase<T> → LinearSupportVectorClassifier<T>

For Beginners

Linear SVC is a simplified version of SVM that only draws straight lines to separate classes. It’s much faster to train than the regular SVC because it doesn’t need to compute kernel values between all pairs of training points.

Use Linear SVC when:

  • You have a large dataset (thousands of samples)
  • Your data is linearly separable or nearly so
  • You need fast training and prediction
  • You have high-dimensional data (many features)

Example use cases:

  • Text classification (spam detection, sentiment)
  • Document categorization
  • High-dimensional bioinformatics data

How It Works

This implementation uses a primal formulation with stochastic gradient descent (SGD) for efficient training on large datasets. Unlike the standard SVC which uses the kernel trick, this classifier works directly in the original feature space.

Example

// Create linear SVC for fast large-scale classification
var options = new SVMOptions<double>();
            
// Prepare training data: 6 samples with 2 features
var features = new Matrix<double>(6, 2);
features[0, 0] = 1.0; features[0, 1] = 1.1;
features[1, 0] = 1.2; features[1, 1] = 0.9;
features[2, 0] = 0.8; features[2, 1] = 1.0;
features[3, 0] = 5.0; features[3, 1] = 5.1;
features[4, 0] = 5.2; features[4, 1] = 4.9;
features[5, 0] = 4.8; features[5, 1] = 5.0;
var labels = new Vector<double>(new double[] { 0, 0, 0, 1, 1, 1 });
            
// Train using primal SGD in original feature space
var result = new AiModelBuilder<double, Matrix<double>, Vector<double>>()
    .ConfigureModel(new LinearSupportVectorClassifier<double>(options))
    .Build(features, labels);
            
// Predict class using learned linear hyperplane
var newSample = new Matrix<double>(1, 2);
newSample[0, 0] = 1.1; newSample[0, 1] = 1.0;
var prediction = result.Predict(newSample);
// Result is available in the returned value

Constructors

Constructor Summary
LinearSupportVectorClassifier(SVMOptions<T> options, IRegularization<T, Matrix<T>, Vector<T>> regularization) Initializes a new instance of the LinearSupportVectorClassifier class.

Methods

Method Summary
Matrix<T> DecisionFunction(Matrix<T> input)
Matrix<T> PredictProbabilities(Matrix<T> input)
ModelMetadata<T> GetModelMetadata()
Vector<T> Predict(Matrix<T> input)
void Train(Matrix<T> x, Vector<T> y) Returns the model type identifier for this classifier.