PegasosQSVC#
- class PegasosQSVC(quantum_kernel=None, C=1.0, num_steps=1000, precomputed=False, seed=None)[ソース]#
ベースクラス:
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.
- パラメータ:
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.
- 例外:
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)[ソース]#
Evaluate the decision function for the samples in X.
- パラメータ:
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.- 戻り値:
An array of the shape (n_samples), the decision function of the sample.
- 例外:
the method is called before the model has been fit.
Pre-computed kernel matrix has the wrong shape and/or dimension.
- 戻り値の型:
- fit(X, y, sample_weight=None)[ソース]#
Fit the model according to the given training data.
- パラメータ:
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.
- 戻り値:
self
, Fitted estimator.- 例外:
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.
- 戻り値の型:
- 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)[ソース]#
Perform classification on samples in X.
- パラメータ:
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.- 戻り値:
An array of the shape (n_samples), the predicted class labels for samples in X.
- 例外:
predict is called before the model has been fit.
Pre-computed kernel matrix has the wrong shape and/or dimension.
- 戻り値の型:
- 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.- パラメータ:
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.
- パラメータ:
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.
- 戻り値:
score – Mean accuracy of
self.predict(X)
w.r.t. y.- 戻り値の型: