PegasosQSVC¶
- class PegasosQSVC(quantum_kernel=None, C=1.0, num_steps=1000, precomputed=False, seed=None)[source]¶
Bases:
ClassifierMixin
,SerializableModelMixin
Implements Pegasos Quantum Support Vector Classifier algorithm. The algorithm has been developed in [1] and includes methods
fit
,predict
anddecision_function
following the signatures of sklearn.svm.SVC. This implementation is adapted to work with quantum kernels.Example
quantum_kernel = FidelityQuantumKernel() pegasos_qsvc = PegasosQSVC(quantum_kernel=quantum_kernel) pegasos_qsvc.fit(sample_train, label_train) pegasos_qsvc.predict(sample_test)
- References
- [1]: Shalev-Shwartz et al., Pegasos: Primal Estimated sub-GrAdient SOlver for SVM.
- Parameters:
quantum_kernel (BaseKernel | None) – A quantum kernel to be used for classification. Has to be
None
when a precomputed kernel is used. If None, andprecomputed
isFalse
, the quantum kernel will default toFidelityQuantumKernel
.C (float) – Positive regularization parameter. The strength of the regularization is inversely proportional to C. Smaller
C
induce smaller weights which generally helps preventing overfitting. However, due to the nature of this algorithm, some of the computation steps become trivial for largerC
. Thus, largerC
improve the performance of the algorithm drastically. If the data is linearly separable in feature space,C
should be chosen to be large. If the separation is not perfect,C
should be chosen smaller to prevent overfitting.num_steps (int) – The number of steps in the Pegasos algorithm. There is no early stopping criterion. The algorithm iterates over all steps.
precomputed (bool) – A boolean flag indicating whether a precomputed kernel is used. Set it to
True
in case of precomputed kernel.seed (int | None) – A seed for the random number generator.
- Raises:
if
quantum_kernel
is passed andprecomputed
is set toTrue
. To use a precomputed kernel,quantum_kernel
has to be of theNone
type. - if C is not a positive number.
Attributes
- FITTED = 0¶
- UNFITTED = 1¶
- num_steps¶
Returns number of steps in the Pegasos algorithm.
- precomputed¶
Returns a boolean flag indicating whether a precomputed kernel is used.
- quantum_kernel¶
Returns quantum kernel
Methods
- decision_function(X)[source]¶
Evaluate the decision function for the samples in
X
.- Parameters:
X (ndarray) – Features. For a callable kernel (an instance of
BaseKernel
) the shape should be(m_samples, n_features)
, for a precomputed kernel the shape should be(m_samples, n_samples)
. Wherem
denotes the set to be predicted andn
the size of the training set. In that case, the kernel values in X have to be calculated with respect to the elements of the set to be predicted and the training set.- Returns:
An array of the shape (n_samples), the decision function of the sample.
- Raises:
the method is called before the model has been fit.
Pre-computed kernel matrix has the wrong shape and/or dimension.
- Return type:
- fit(X, y, sample_weight=None)[source]¶
Fit the model according to the given training data.
- Parameters:
X (ndarray) – Train features. For a callable kernel (an instance of
BaseKernel
) the shape should be(n_samples, n_features)
, for a precomputed kernel the shape should be(n_samples, n_samples)
.y (ndarray) – shape (n_samples), train labels . Must not contain more than two unique labels.
sample_weight (ndarray | None) – this parameter is not supported, passing a value raises an error.
- Returns:
self
, Fitted estimator.- Raises:
X and/or y have the wrong shape. - X and y have incompatible dimensions. - y includes more than two unique labels. - Pre-computed kernel matrix has the wrong shape and/or dimension.
when a sample_weight which is not None is passed.
- Return type:
- classmethod load(file_name)¶
Loads a model from the file. If the loaded model is not an instance of the class whose method was called, then a warning is raised. Nevertheless, the loaded model may be a valid model.
- predict(X)[source]¶
Perform classification on samples in X.
- Parameters:
X (np.ndarray) – Input features. For a callable kernel (an instance of
BaseKernel
), the shape should be(m_samples, n_features)
. For a pre-computed kernel, the shape should be(m_samples, n_samples)
. Here,m_*
denotes the set to be predicted, andn_*
denotes the size of the training set. In the case of a pre-computed kernel, the kernel values inX
must be calculated with respect to the elements of the set to be predicted and the training set.- Returns:
- An array of shape
(n_samples,)
, representing the predicted class labels for each sample in
X
.
- An array of shape
- Return type:
np.ndarray
- Raises:
If the
predict()
method is called before the model has been fit.
If the pre-computed kernel matrix has the wrong shape and/or dimension.
- predict_proba(X)[source]¶
Extract class prediction probabilities. The decision function values are not bounded in the range \([0, 1]\). Therefore, these values are converted into probabilities using the sigmoid activation function, which maps the real-valued outputs to the \([0, 1]\) range.
- Parameters:
X (np.ndarray) – Input features. For a callable kernel (an instance of
BaseKernel
), the shape should be(m_samples, n_features)
. For a pre-computed kernel, the shape should be(m_samples, n_samples)
. Here,m_*
denotes the set to be predicted, andn_*
denotes the size of the training set. In the case of a pre-computed kernel, the kernel values inX
must be calculated with respect to the elements of the set to be predicted and the training set.- Returns:
- An array of shape
(n_samples, 2)
, representing the predicted class probabilities (in the range \([0, 1]\)) for each sample in
X
.
- An array of shape
- Return type:
np.ndarray
- save(file_name)¶
Saves this model to the specified file. Internally, the model is serialized via
dill
. All parameters are saved, including a primitive instance that is referenced by internal objects. That means if a model is loaded from a file and is used, for instance, for inference, the same primitive will be used even if a cloud primitive was used.- Parameters:
file_name (str) – a file name or path where to save the model.
- score(X, y, sample_weight=None)¶
Return the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Test samples.
y (array-like of shape (n_samples,) or (n_samples, n_outputs)) – True labels for X.
sample_weight (array-like of shape (n_samples,), default=None) – Sample weights.
- Returns:
score – Mean accuracy of
self.predict(X)
w.r.t. y.- Return type: