#[non_exhaustive]pub struct LinearSVC { /* private fields */ }Expand description
Linear Support Vector Classifier.
Uses the Pegasos SGD algorithm to minimize hinge loss with L2 regularisation. Binary problems use a single weight vector; multiclass problems use one-vs-rest (one weight vector per class, prediction = argmax of decision function scores).
§Example
use scry_learn::dataset::Dataset;
use scry_learn::svm::LinearSVC;
let features = vec![
vec![0.0, 0.0, 10.0, 10.0],
vec![0.0, 0.0, 10.0, 10.0],
];
let target = vec![0.0, 0.0, 1.0, 1.0];
let data = Dataset::new(features, target, vec!["x".into(), "y".into()], "class");
let mut svc = LinearSVC::new().c(1.0).max_iter(500);
svc.fit(&data).unwrap();
let preds = svc.predict(&[vec![1.0, 1.0]]).unwrap();
assert_eq!(preds[0] as usize, 0);Implementations§
Source§impl LinearSVC
impl LinearSVC
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new LinearSVC with default parameters.
Defaults: C = 1.0, max_iter = 1000, tol = 1e-4.
Sourcepub fn c(self, c: f64) -> Self
pub fn c(self, c: f64) -> Self
Set the regularisation parameter C.
Larger values penalise misclassification more (tighter margin).
Sourcepub fn class_weight(self, cw: ClassWeight) -> Self
pub fn class_weight(self, cw: ClassWeight) -> Self
Set class weighting strategy for imbalanced datasets.
Sourcepub fn probability(self, enable: bool) -> Self
pub fn probability(self, enable: bool) -> Self
Enable Platt scaling for probability estimates.
When true, predict_proba returns
calibrated class probabilities after fitting.
Sourcepub fn fit(&mut self, data: &Dataset) -> Result<()>
pub fn fit(&mut self, data: &Dataset) -> Result<()>
Train the SVM on the given dataset.
Uses Pegasos-style SGD with one-vs-rest decomposition for multiclass problems (≥ 3 classes). Auto-dispatches to sparse kernels when the dataset uses sparse storage.
Sourcepub fn predict_sparse(&self, csr: &CsrMatrix) -> Result<Vec<f64>>
pub fn predict_sparse(&self, csr: &CsrMatrix) -> Result<Vec<f64>>
Predict class labels from sparse input (CSR format).
Sourcepub fn predict(&self, features: &[Vec<f64>]) -> Result<Vec<f64>>
pub fn predict(&self, features: &[Vec<f64>]) -> Result<Vec<f64>>
Predict class labels for the given row-major feature matrix.
Returns the class whose OVR decision function is largest.
Sourcepub fn decision_function(&self, features: &[Vec<f64>]) -> Result<Vec<Vec<f64>>>
pub fn decision_function(&self, features: &[Vec<f64>]) -> Result<Vec<Vec<f64>>>
Compute the raw decision function score for each class.
Returns scores[sample][class] = w · x + b for each OVR
sub-problem.
§Example
use scry_learn::dataset::Dataset;
use scry_learn::svm::LinearSVC;
let features = vec![
vec![0.0, 0.0, 10.0, 10.0],
vec![0.0, 0.0, 10.0, 10.0],
];
let target = vec![0.0, 0.0, 1.0, 1.0];
let data = Dataset::new(features, target, vec!["x".into(), "y".into()], "class");
let mut svc = LinearSVC::new();
svc.fit(&data).unwrap();
let scores = svc.decision_function(&[vec![1.0, 1.0]]).unwrap();
assert_eq!(scores[0].len(), 2); // two classesTrait Implementations§
Source§impl CalibrableClassifier for LinearSVC
impl CalibrableClassifier for LinearSVC
Source§impl PipelineModel for LinearSVC
impl PipelineModel for LinearSVC
Auto Trait Implementations§
impl Freeze for LinearSVC
impl RefUnwindSafe for LinearSVC
impl Send for LinearSVC
impl Sync for LinearSVC
impl Unpin for LinearSVC
impl UnsafeUnpin for LinearSVC
impl UnwindSafe for LinearSVC
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> 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