pub struct FittedSVC<F, K> { /* private fields */ }Expand description
Fitted Support Vector Classifier.
Stores one binary SVM per pair of classes (one-vs-one). Implements
Predict to produce class labels.
Implementations§
Source§impl<F: Float, K: Kernel<F>> FittedSVC<F, K>
impl<F: Float, K: Kernel<F>> FittedSVC<F, K>
Sourcepub fn decision_function(
&self,
x: &Array2<F>,
) -> Result<SvmScores<F>, FerroError>
pub fn decision_function( &self, x: &Array2<F>, ) -> Result<SvmScores<F>, FerroError>
The decision function for the samples in x
(sklearn/svm/_base.py:536-541, 778-781).
- Binary (
n_classes == 2):SvmScores::Binary, shape(n_samples,)=-raw_ovo.ravel()(sklearn flips the sign forc_svc/nu_svcbinary,_base.py:538-539), so a POSITIVE value predictsclasses_[1]. Because this crate’sdecision_value_binaryalready uses the higher-index class as+1,-raw_ovoequalsdecision_value_binarydirectly. - Multiclass
SvmDecisionShape::Ovr(default):SvmScores::Multiclass, shape(n_samples, n_classes)=_ovr_decision_function(raw_ovo)(_base.py:780). - Multiclass
SvmDecisionShape::Ovo:SvmScores::Multiclass, shape(n_samples, n·(n-1)/2)= the raw ovo values.
§Errors
Returns Ok for valid input.
Sourcepub fn predict_proba(&self, x: &Array2<F>) -> Result<Array2<F>, FerroError>
pub fn predict_proba(&self, x: &Array2<F>) -> Result<Array2<F>, FerroError>
Class probability estimates, shape (n_samples, n_classes); columns
correspond to classes_ in sorted order
(sklearn/svm/_base.py:829-864, libsvm.predict_probability,
svm.cpp:2918-2955).
Built from the per-pair Platt sigmoids fitted at fit time when
probability=true: the raw one-vs-one decision values (libsvm sign,
lower-index class +1) are mapped to pairwise probabilities via
[sigmoid_predict] (clamped to [1e-7, 1-1e-7], svm.cpp:2929-2938),
then coupled by [multiclass_probability] (Wu-Lin-Weng 2004,
svm.cpp:2941). For the binary case multiclass_probability reduces to
[sigmoid_predict(dec), 1 - sigmoid_predict(dec)] =
[P(classes_[0]), P(classes_[1])]. Each row sums to 1.
RNG-CV exact-value boundary (documented divergence). Because the
underlying (A, B) come from a cross-validation whose fold assignment
is RNG-dependent in libsvm/sklearn (sklearn’s probA_/probB_ and
predict_proba values change with random_state), ferrolearn uses a
DETERMINISTIC contiguous 5-fold split instead and therefore does NOT
bit-match sklearn’s predict_proba values. The reproduced contract is
structural: rows sum to 1, entries in [0, 1], and (binary) the
classes_[1] column is monotone non-decreasing in the
decision_function value. See SVC::probability.
§Errors
Returns FerroError::InvalidParameter when the model was fitted with
probability=false, with the message mirroring sklearn’s
NotFittedError text “predict_proba is not available when fitted with
probability=False” (_base.py:856-860). (This crate has no NotFitted
variant — predict-before-fit is a compile error via the typestate,
R-DEV-4 — so the “fitted-without-probability” runtime condition maps to
InvalidParameter; the binding maps it to the matching PyErr.)
Sourcepub fn predict_log_proba(&self, x: &Array2<F>) -> Result<Array2<F>, FerroError>
pub fn predict_log_proba(&self, x: &Array2<F>) -> Result<Array2<F>, FerroError>
Natural-log class probability estimates, shape (n_samples, n_classes)
= predict_proba(x).ln() elementwise (sklearn/svm/_base.py:866-894:
np.log(self.predict_proba(X))).
§Errors
Returns [FerroError::NotFitted] when the model was fitted with
probability=false (delegated from Self::predict_proba).
Sourcepub fn probability(&self) -> bool
pub fn probability(&self) -> bool
Whether Platt-scaling probability estimates were fitted
(probability, sklearn/svm/_classes.py); when false,
Self::predict_proba/Self::predict_log_proba raise.
Source§impl<F: Float + ScalarOperand + 'static, K: Kernel<F>> FittedSVC<F, K>
impl<F: Float + ScalarOperand + 'static, K: Kernel<F>> FittedSVC<F, K>
Sourcepub fn support(&self) -> Array1<usize>
pub fn support(&self) -> Array1<usize>
Indices of the support vectors into the training set, grouped by
class (all class-classes_[0] SVs first, then classes_[1], …),
ascending within each class.
Mirrors SVC.support_ (sklearn/svm/_base.py:318-410).
Sourcepub fn support_vectors(&self) -> Array2<F>
pub fn support_vectors(&self) -> Array2<F>
The support vectors X[support_], shape (n_SV, n_features).
Mirrors SVC.support_vectors_ (sklearn/svm/_base.py:318-410).
Sourcepub fn n_support(&self) -> Vec<usize>
pub fn n_support(&self) -> Vec<usize>
Number of support vectors per class (n_support_,
sklearn/svm/_base.py:668-682), parallel to classes_.
Sourcepub fn dual_coef(&self) -> Array2<F>
pub fn dual_coef(&self) -> Array2<F>
Dual coefficients in the libsvm public layout, shape
(n_class - 1, n_SV) (sklearn/svm/_base.py:318-410, the dual_coef_
attribute), columns in support_ (per-class-grouped) order.
For an SV belonging to class i, row m holds its coefficient in the
binary classifier between class i and the m-th OTHER class (the
other classes in increasing index order, skipping i). In the ovo pair
(a, b) with a < b, libsvm uses class a as the +1 side and b as
-1; the stored coefficient is alpha * y_libsvm.
This crate stores alpha * y per pair with the OPPOSITE sign
(class_neg = a mapped to -1), so the libsvm-internal coefficient is
the negation of the stored value. For n_class == 2 sklearn negates the
internal coefficient again to form the PUBLIC binary attribute
(sklearn/svm/_base.py:258-262: dual_coef_ = -dual_coef_), which
leaves the public binary value equal to this crate’s stored value; for
n_class > 2 the public value IS the libsvm-internal value (no flip).
Sourcepub fn intercept(&self) -> Array1<F>
pub fn intercept(&self) -> Array1<F>
The per-ovo-problem intercepts, length n_class * (n_class - 1) / 2,
in pair order (0,1),(0,2),(1,2),... (intercept_,
sklearn/svm/_base.py:318-410). For n_class == 2 sklearn negates the
internal bias to form the public attribute
(sklearn/svm/_base.py:258-262: intercept_ *= -1).
This crate’s per-pair bias is recovered for the decision function
sum coef*K + bias with class_pos (the higher index) as the +1
side. libsvm/sklearn use the lower-index class as +1, so the
libsvm-internal intercept is the negation of this crate’s bias. For
binary, the public attribute negates the internal again, leaving the
public value equal to this crate’s stored bias; for multiclass the
public value IS the internal -bias.
Trait Implementations§
Source§impl<F: Float + Send + Sync + ScalarOperand + 'static, K: Kernel<F> + 'static> Predict<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>> for FittedSVC<F, K>
impl<F: Float + Send + Sync + ScalarOperand + 'static, K: Kernel<F> + 'static> Predict<ArrayBase<OwnedRepr<F>, Dim<[usize; 2]>>> for FittedSVC<F, K>
Source§fn predict(&self, x: &Array2<F>) -> Result<Array1<usize>, FerroError>
fn predict(&self, x: &Array2<F>) -> Result<Array1<usize>, FerroError>
Predict class labels for the given feature matrix.
Uses one-vs-one voting (each binary model casts a vote, the most-voted
class wins, ties broken toward the lower class index), matching libsvm’s
super().predict (sklearn/svm/_base.py:813-814).
When break_ties == true AND SvmDecisionShape::Ovr AND
n_classes > 2, ties are instead broken by the one-vs-rest decision
confidence: predict = argmax(decision_function(X))
(sklearn/svm/_base.py:806-811).
§Errors
Returns FerroError::ShapeMismatch if the number of features
does not match the training data. Returns
FerroError::InvalidParameter when break_ties == true and the
decision-function shape is SvmDecisionShape::Ovo
(sklearn/svm/_base.py:801-804).
Source§type Output = ArrayBase<OwnedRepr<usize>, Dim<[usize; 1]>>
type Output = ArrayBase<OwnedRepr<usize>, Dim<[usize; 1]>>
ndarray::Array1<F> or ndarray::Array1<usize>).Source§type Error = FerroError
type Error = FerroError
predict.Auto Trait Implementations§
impl<F, K> Freeze for FittedSVC<F, K>where
K: Freeze,
impl<F, K> RefUnwindSafe for FittedSVC<F, K>where
K: RefUnwindSafe,
F: RefUnwindSafe,
impl<F, K> Send for FittedSVC<F, K>
impl<F, K> Sync for FittedSVC<F, K>
impl<F, K> Unpin for FittedSVC<F, K>
impl<F, K> UnsafeUnpin for FittedSVC<F, K>where
K: UnsafeUnpin,
impl<F, K> UnwindSafe for FittedSVC<F, K>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T, U> Imply<T> for U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more