SVM with a classical RBF kernel: multiclass classifier extension

A multiclass extension works in conjunction with an underlying binary (two class) classifier to provide multiclass classification.

Currently three different multiclass extensions are supported:

  • OneAgainstRest
  • AllPairs
  • ErrorCorrectingCode

These use different techniques to group the data with binary classification to achieve the final multiclass classification.


In [5]:
from datasets import *
from qiskit_aqua.utils import split_dataset_to_data_and_labels
from qiskit_aqua.input import get_input_instance
from qiskit_aqua import run_algorithm
import numpy as np

Here we choose the Wine dataset which has 3 classes.


In [6]:
n = 2  # dimension of each data point
sample_Total, training_input, test_input, class_labels = Wine(training_size=20,
                                                              test_size=10, n=n, PLOT_DATA=True)

temp = [test_input[k] for k in test_input]
total_array = np.concatenate(temp)


Now we setup an Aqua configuration dictionary to use the classical SVM algorithm and add a multiclass extension to classify the Wine data set, since it has 3 classes. We loop over the three extensions (modifying the params dictionary) to show the result with each.


In [7]:
aqua_dict = {
    'problem': {'name': 'svm_classification'},
    'algorithm': {
        'name': 'SVM'
    },
    'multiclass_extension': {'name': 'OneAgainstRest'}
}

algo_input = get_input_instance('SVMInput')
algo_input.training_dataset = training_input
algo_input.test_dataset = test_input
algo_input.datapoints = total_array

extensions = [
   {'name': 'OneAgainstRest'},
   {'name': 'AllPairs'}, 
   {'name': 'ErrorCorrectingCode', 'code_size': 5}
]

for extension in extensions:
    aqua_dict['multiclass_extension'] = extension
    result = run_algorithm(aqua_dict, algo_input)
    print("\n----- Using multiclass extension: '{}' -----\n".format(extension['name']))
    for k,v in result.items():
        print("'{}' : {}".format(k, v))


----- Using multiclass extension: 'OneAgainstRest' -----

'testing_accuracy' : 1.0
'test_success_ratio' : 1.0
'predicted_labels' : [0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2]
'predicted_classes' : ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C']

----- Using multiclass extension: 'AllPairs' -----

'testing_accuracy' : 1.0
'test_success_ratio' : 1.0
'predicted_labels' : [0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2]
'predicted_classes' : ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C']

----- Using multiclass extension: 'ErrorCorrectingCode' -----

'testing_accuracy' : 1.0
'test_success_ratio' : 1.0
'predicted_labels' : [0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2]
'predicted_classes' : ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C', 'C']