# qiskit.aqua.algorithms.SklearnSVM¶

class SklearnSVM(training_dataset, test_dataset=None, datapoints=None, gamma=None, multiclass_extension=None)[source]

The Sklearn SVM algorithm (classical).

This scikit-learn based SVM algorithm uses a classical approach to experiment with feature map classification problems. See also the quantum classifier QSVM.

Internally, this algorithm will run the binary classification or multiclass classification based on how many classes the data has. If the data has more than 2 classes then a multiclass_extension is required to be supplied. Aqua provides several multiclass_extensions.

Parameters
• training_dataset (Dict[str, ndarray]) – Training dataset.

• test_dataset (Optional[Dict[str, ndarray]]) – Testing dataset.

• datapoints (Optional[ndarray]) – Prediction dataset.

• gamma (Optional[int]) – Used as input for sklearn rbf_kernel which is used internally. See sklearn.metrics.pairwise.rbf_kernel for more information about gamma.

• multiclass_extension (Optional[MulticlassExtension]) – If number of classes is greater than 2 then a multiclass scheme must be supplied, in the form of a multiclass extension.

Raises

AquaError – Multiclass extension not supplied when number of classes > 2

__init__(training_dataset, test_dataset=None, datapoints=None, gamma=None, multiclass_extension=None)[source]
Parameters
• training_dataset (Dict[str, ndarray]) – Training dataset.

• test_dataset (Optional[Dict[str, ndarray]]) – Testing dataset.

• datapoints (Optional[ndarray]) – Prediction dataset.

• gamma (Optional[int]) –

Used as input for sklearn rbf_kernel which is used internally. See sklearn.metrics.pairwise.rbf_kernel for more information about gamma.

• multiclass_extension (Optional[MulticlassExtension]) – If number of classes is greater than 2 then a multiclass scheme must be supplied, in the form of a multiclass extension.

Raises

AquaError – Multiclass extension not supplied when number of classes > 2

Methods

 __init__(training_dataset[, test_dataset, …]) type training_dataset Dict[str, ndarray] load_model(file_path) Load a model from a file path. predict(data) Predict using the SVM Execute the classical algorithm. save_model(file_path) Save the model to a file path. test(data, labels) Test the SVM train(data, labels) Train the SVM

Attributes

 class_to_label returns class to label label_to_class returns label to class random Return a numpy random. ret returns result
property class_to_label

returns class to label

property label_to_class

returns label to class

load_model(file_path)[source]

Load a model from a file path.

Parameters

file_path (str) – the path of the saved model.

predict(data)[source]

Predict using the SVM

Parameters

data (numpy.ndarray) – NxD array, where N is the number of data, D is the feature dimension.

Returns

predicted labels, Nx1 array

Return type

numpy.ndarray

property random

Return a numpy random.

property ret

returns result

run()

Execute the classical algorithm.

Returns

results of an algorithm.

Return type

dict

save_model(file_path)[source]

Save the model to a file path.

Parameters

file_path (str) – a path to save the model.

test(data, labels)[source]

Test the SVM

Parameters
• data (numpy.ndarray) – NxD array, where N is the number of data, D is the feature dimension.

• labels (numpy.ndarray) – Nx1 array, where N is the number of data

Returns

accuracy

Return type

float

train(data, labels)[source]

Train the SVM

Parameters
• data (numpy.ndarray) – NxD array, where N is the number of data, D is the feature dimension.

• labels (numpy.ndarray) – Nx1 array, where N is the number of data