Skip to main content

FittedSVC

Struct FittedSVC 

Source
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>

Source

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 for c_svc/nu_svc binary, _base.py:538-539), so a POSITIVE value predicts classes_[1]. Because this crate’s decision_value_binary already uses the higher-index class as +1, -raw_ovo equals decision_value_binary directly.
  • 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.

Source

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.)

Source

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).

Source

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

pub fn prob_a(&self) -> Array1<F>

The per-ovo-pair Platt sigmoid A parameters (probA_, sklearn/svm/_base.py), length n_class·(n_class-1)/2. Empty when fitted with probability=false.

Source

pub fn prob_b(&self) -> Array1<F>

The per-ovo-pair Platt sigmoid B parameters (probB_, sklearn/svm/_base.py), length n_class·(n_class-1)/2. Empty when fitted with probability=false.

Source§

impl<F: Float + ScalarOperand + 'static, K: Kernel<F>> FittedSVC<F, K>

Source

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).

Source

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).

Source

pub fn n_support(&self) -> Vec<usize>

Number of support vectors per class (n_support_, sklearn/svm/_base.py:668-682), parallel to classes_.

Source

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).

Source

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.

Source

pub fn coef(&self) -> Option<Array2<F>>

Primal weight vector coef_ = dual_coef_ @ support_vectors_, shape (n_class - 1, n_features) — available ONLY for the linear kernel (sklearn/svm/_base.py:650-666). Returns None for any other kernel (sklearn raises AttributeError).

Trait Implementations§

Source§

impl<F: Clone, K: Clone> Clone for FittedSVC<F, K>

Source§

fn clone(&self) -> FittedSVC<F, K>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F: Debug, K: Debug> Debug for FittedSVC<F, K>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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>

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]>>

The prediction output type (e.g., ndarray::Array1<F> or ndarray::Array1<usize>).
Source§

type Error = FerroError

The error type returned by predict.

Auto Trait Implementations§

§

impl<F, K> Freeze for FittedSVC<F, K>
where K: Freeze,

§

impl<F, K> RefUnwindSafe for FittedSVC<F, K>

§

impl<F, K> Send for FittedSVC<F, K>
where K: Send, F: Send,

§

impl<F, K> Sync for FittedSVC<F, K>
where K: Sync, F: Sync,

§

impl<F, K> Unpin for FittedSVC<F, K>
where K: Unpin, F: Unpin,

§

impl<F, K> UnsafeUnpin for FittedSVC<F, K>
where K: UnsafeUnpin,

§

impl<F, K> UnwindSafe for FittedSVC<F, K>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ByRef<T> for T

Source§

fn by_ref(&self) -> &T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DistributionExt for T
where T: ?Sized,

Source§

fn rand<T>(&self, rng: &mut (impl Rng + ?Sized)) -> T
where Self: Distribution<T>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V