Previous topic

util.stats

Next topic

util.zmq_cluster

This Page

util.svm

This module provides access to the LIBSVM solver.

glimpse.util.svm.PrepareLibSvmInput(features_per_class)

Format feature vectors for use by LIBSVM.

Parameters:features_per_class (list of ndarray) – Per-class feature vectors.
Returns:Labels and feature vectors.
Return type:2-tuple, where first element is a list and second element is a list of list
class glimpse.util.svm.RangeFeatureScaler(min=-1, max=1)

Scale features to lie in a fixed interval.

Example Usage:

>>> instances = np.arange(10).reshape(2, 5)
>>> scaler = RangeFeatureScaler()
>>> scaler.Learn(instances)
>>> scaled_instances = scaler.Apply(instances)

See also

sklearn.preprocessing.Scaler

Apply(features)

Scale the features in-place.

The range of output values will be (approximately) [-vmin, vmax], assuming the feature vectors passed here were drawn from the same distribution as those used to learn the scaling parameters.

Parameters:features (2D array-like) – Input data.
Returns:Scaled feature values.
Return type:2D ndarray
Learn(features)

Determine the parameters required to scale each feature (independently) to the range [-1, 1].

Parameters:features (2D array-like) – Input data.
class glimpse.util.svm.ScaledSvm(classifier=None, scaler=None)

A LIBSVM solver, which automatically scales feature values.

Test(features)

Test an existing classifier.

Parameters:features (3D array-like) – Test instances: indexed by class, instance, and then feature offset.
Returns:Results from svmutil.svm_predict.
Return type:dict
Train(features)

Train an SVM classifier.

Parameters:features (3D array-like) – Training instances, indexed by class, instance, and then feature offset.
class glimpse.util.svm.SpheringFeatureScaler(mean=0, std=1)

Scale features to have fixed mean and standard deviation.

Example usage:

>>> instances = np.arange(10).reshape(2, 5)
>>> scaler = SpheringFeatureScaler()
>>> scaler.Learn(instances)
>>> scaled_instances = scaler.Apply(instances)

See also

sklearn.preprocessing.Scaler

Apply(features)

Scale the features.

The range of output values will be (approximately) [-vmin, vmax], assuming the feature vectors passed here were drawn from the same distribution as those used to learn the scaling parameters.

Parameters:features (2D array-like) – Input data.
Returns:Scaled features.
Return type:2D ndarray
Learn(features)

Determine the parameters required to scale each feature (independently) to the range [-1, 1].

Parameters:features (2D array-like) – Input data.
class glimpse.util.svm.Svm(classifier=None)

The LIBSVM classifier.

See also

sklearn.svm.LinearSVC

Test(features)

Test an existing classifier.

Parameters:features (3D array-like) – Test instances: indexed by class, instance, and then feature offset.
Returns:Results from svmutil.svm_predict.
Return type:dict
Train(features)

Train an SVM classifier.

Parameters:features (3D array-like) – Training instances, indexed by class, instance, and then by feature offset.
glimpse.util.svm.SvmCrossValidate(features_per_class, num_repetitions=None, num_splits=None, scaler=None)

Perform NxM way cross-validation.

Parameters:
  • features_per_class (list of 2D ndarray) – Feature vectors indexed by class, instance, and then feature offset.
  • num_repetitions (int) – How many times to repeat the measurements (default is 10).
  • num_splits (int) – How many ways to split the instances (default is num_repetitions).
  • scaler – Feature scaling algorithm.
Returns:

Mean test accuracy across splits and repetitions.

Return type:

float

glimpse.util.svm.SvmForSplit(train_features, test_features, scaler=None)

Train and test an SVM classifier from a set of features.

Features should already have been partitioned into training and testing sets.

Parameters:
  • train_features (3D array-like) – Training instances: indexed by class, instance, and then feature offset.
  • test_features (list of 2D ndarray of float) – Testing instances: indexed by class, instance, and then feature offset.
  • scaler – Feature scaling algorithm.
Returns:

The trained classifier, the training accuracy, and the testing accuracy.

Return type:

3-tuple, where first element is an SVM classifier, and the last two elements are floats