rustyml 0.12.0

A high-performance machine learning & deep learning library in pure Rust, offering ML algorithms and neural network support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//! Integration tests for LinearSVC: constructor validation, fit/predict error
//! paths, label domain, sign consistency, separable-data correctness, getters,
//! penalties, save/load round-trip, and random_state reproducibility

use ndarray::{Array1, Array2, array};
use rustyml::error::Error;
use rustyml::machine_learning::{LinearSVC, RegularizationType};

// helper

/// Return (x, y) for a perfectly linearly-separable 2-feature problem split by
/// the boundary x1=0 (class 0 at negative x1, class 1 at positive x1)
fn make_separable() -> (Array2<f64>, Array1<f64>) {
    let x = Array2::from_shape_vec(
        (8, 2),
        vec![
            -5.0, 0.0, -6.0, 0.0, -7.0, 0.0, -4.0, 0.0, 5.0, 0.0, 6.0, 0.0, 7.0, 0.0, 4.0, 0.0,
        ],
    )
    .unwrap();
    let y = array![0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0];
    (x, y)
}

/// Fit a default (L2) LinearSVC on the separable dataset with enough iterations
/// that convergence is expected, returning the trained model
fn fit_separable_model() -> LinearSVC {
    let (x, y) = make_separable();
    let mut model = LinearSVC::new(5000, 0.01, RegularizationType::L2(0.1), true, 1e-5).unwrap();
    model.fit(&x, &y).unwrap();
    model
}

// constructor validation

#[test]
fn new_rejects_max_iter_zero() {
    let result = LinearSVC::new(0, 0.01, RegularizationType::L2(1.0), true, 1e-4);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "max_iter=0 must return InvalidParameter"
    );
}

#[test]
fn new_rejects_learning_rate_zero() {
    let result = LinearSVC::new(100, 0.0, RegularizationType::L2(1.0), true, 1e-4);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "learning_rate=0.0 must return InvalidParameter"
    );
}

#[test]
fn new_rejects_learning_rate_negative() {
    let result = LinearSVC::new(100, -0.001, RegularizationType::L2(1.0), true, 1e-4);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "learning_rate=-0.001 must return InvalidParameter"
    );
}

#[test]
fn new_rejects_learning_rate_nan() {
    let result = LinearSVC::new(100, f64::NAN, RegularizationType::L2(1.0), true, 1e-4);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "learning_rate=NaN must return InvalidParameter"
    );
}

#[test]
fn new_rejects_learning_rate_infinity() {
    let result = LinearSVC::new(100, f64::INFINITY, RegularizationType::L2(1.0), true, 1e-4);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "learning_rate=+Inf must return InvalidParameter"
    );
}

#[test]
fn new_rejects_tol_zero() {
    let result = LinearSVC::new(100, 0.01, RegularizationType::L2(1.0), true, 0.0);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "tol=0.0 must return InvalidParameter"
    );
}

#[test]
fn new_rejects_tol_negative() {
    let result = LinearSVC::new(100, 0.01, RegularizationType::L2(1.0), true, -1e-4);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "tol=-1e-4 must return InvalidParameter"
    );
}

#[test]
fn new_rejects_tol_nan() {
    let result = LinearSVC::new(100, 0.01, RegularizationType::L2(1.0), true, f64::NAN);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "tol=NaN must return InvalidParameter"
    );
}

#[test]
fn new_rejects_penalty_lambda_negative_l2() {
    let result = LinearSVC::new(100, 0.01, RegularizationType::L2(-0.5), true, 1e-4);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "L2 with negative lambda must return InvalidParameter"
    );
}

#[test]
fn new_rejects_penalty_lambda_negative_l1() {
    let result = LinearSVC::new(100, 0.01, RegularizationType::L1(-1.0), true, 1e-4);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "L1 with negative lambda must return InvalidParameter"
    );
}

#[test]
fn new_rejects_penalty_lambda_nan() {
    let result = LinearSVC::new(100, 0.01, RegularizationType::L2(f64::NAN), true, 1e-4);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "L2(NaN) must return InvalidParameter"
    );
}

#[test]
fn new_rejects_penalty_lambda_infinity() {
    let result = LinearSVC::new(100, 0.01, RegularizationType::L1(f64::INFINITY), true, 1e-4);
    assert!(
        matches!(result, Err(Error::InvalidParameter { .. })),
        "L1(+Inf) must return InvalidParameter"
    );
}

/// Lambda = 0.0 is explicitly allowed (no regularization)
#[test]
fn new_accepts_zero_penalty_lambda() {
    let result = LinearSVC::new(100, 0.01, RegularizationType::L2(0.0), true, 1e-4);
    assert!(result.is_ok(), "L2(0.0) must be accepted");
}

/// All defaults match documented values
#[test]
fn default_constructor_has_documented_defaults() {
    let model = LinearSVC::default();
    assert_eq!(model.get_max_iterations(), 1000);
    assert_eq!(model.get_learning_rate(), 0.001);
    assert_eq!(model.get_tolerance(), 1e-4);
    assert!(model.get_fit_intercept());
    assert_eq!(model.get_penalty(), RegularizationType::L2(1.0));
    assert!(
        model.get_weights().is_none(),
        "weights should be None before fit"
    );
    assert!(model.get_bias().is_none(), "bias should be None before fit");
    assert!(
        model.get_actual_iterations().is_none(),
        "n_iter should be None before fit"
    );
}

/// Constructor stores the passed parameters exactly
#[test]
fn new_stores_parameters() {
    let model = LinearSVC::new(500, 0.005, RegularizationType::L1(0.1), false, 1e-6).unwrap();
    assert_eq!(model.get_max_iterations(), 500);
    assert_eq!(model.get_learning_rate(), 0.005);
    assert_eq!(model.get_penalty(), RegularizationType::L1(0.1));
    assert!(!model.get_fit_intercept());
    assert_eq!(model.get_tolerance(), 1e-6);
}

// fit error paths

#[test]
fn fit_rejects_empty_x() {
    let x: Array2<f64> = Array2::zeros((0, 2));
    let y: Array1<f64> = Array1::zeros(0);
    let mut model = LinearSVC::default();
    let result = model.fit(&x, &y);
    assert!(
        matches!(result, Err(Error::EmptyInput(_))),
        "0-row input must return EmptyInput"
    );
}

#[test]
fn fit_rejects_empty_y() {
    let x: Array2<f64> = Array2::zeros((3, 2));
    let y: Array1<f64> = Array1::zeros(0);
    let mut model = LinearSVC::default();
    let result = model.fit(&x, &y);
    // Either DimensionMismatch (y.len()=0 != x.nrows()=3) or EmptyInput is valid,
    // depending on which guard fires first
    assert!(result.is_err(), "mismatched empty y must return an error");
}

#[test]
fn fit_rejects_dimension_mismatch_xy() {
    // x has 4 rows but y has 3 elements
    let x = Array2::zeros((4, 2));
    let y = Array1::zeros(3);
    let mut model = LinearSVC::default();
    let result = model.fit(&x, &y);
    assert!(
        matches!(result, Err(Error::DimensionMismatch { .. })),
        "x/y row count mismatch must return DimensionMismatch"
    );
}

#[test]
fn fit_rejects_nan_in_x() {
    let x = Array2::from_shape_vec((2, 2), vec![1.0, 2.0, f64::NAN, 4.0]).unwrap();
    let y = array![0.0, 1.0];
    let mut model = LinearSVC::default();
    let result = model.fit(&x, &y);
    assert!(
        matches!(result, Err(Error::NonFinite(_))),
        "NaN in x must return NonFinite"
    );
}

#[test]
fn fit_rejects_infinite_in_x() {
    let x = Array2::from_shape_vec((2, 2), vec![1.0, 2.0, f64::INFINITY, 4.0]).unwrap();
    let y = array![0.0, 1.0];
    let mut model = LinearSVC::default();
    let result = model.fit(&x, &y);
    assert!(
        matches!(result, Err(Error::NonFinite(_))),
        "Inf in x must return NonFinite"
    );
}

// predict / decision_function error paths

#[test]
fn predict_not_fitted_returns_not_fitted_error() {
    let model = LinearSVC::default();
    let x = Array2::zeros((2, 2));
    let result = model.predict(&x);
    assert!(
        matches!(result, Err(Error::NotFitted("LinearSVC"))),
        "predict before fit must return NotFitted(\"LinearSVC\")"
    );
}

#[test]
fn decision_function_not_fitted_returns_not_fitted_error() {
    let model = LinearSVC::default();
    let x = Array2::zeros((2, 2));
    let result = model.decision_function(&x);
    assert!(
        matches!(result, Err(Error::NotFitted("LinearSVC"))),
        "decision_function before fit must return NotFitted(\"LinearSVC\")"
    );
}

#[test]
fn predict_wrong_feature_count_returns_dimension_mismatch() {
    // Fit on 2-feature data, then predict with 3 features
    let model = fit_separable_model();
    let x_wrong = Array2::zeros((2, 3));
    let result = model.predict(&x_wrong);
    assert!(
        matches!(
            result,
            Err(Error::DimensionMismatch {
                expected: 2,
                found: 3
            })
        ),
        "wrong feature count must return DimensionMismatch(expected=2, found=3)"
    );
}

#[test]
fn decision_function_wrong_feature_count_returns_dimension_mismatch() {
    let model = fit_separable_model();
    let x_wrong = Array2::zeros((2, 1));
    let result = model.decision_function(&x_wrong);
    assert!(
        matches!(
            result,
            Err(Error::DimensionMismatch {
                expected: 2,
                found: 1
            })
        ),
        "wrong feature count must return DimensionMismatch(expected=2, found=1)"
    );
}

#[test]
fn predict_empty_input_after_fit_returns_error() {
    let model = fit_separable_model();
    // 0-row input with correct ncols
    let x_empty: Array2<f64> = Array2::zeros((0, 2));
    let result = model.predict(&x_empty);
    assert!(
        result.is_err(),
        "empty input to predict after fit must return an error"
    );
}

// output label domain: must be exactly 0.0 or 1.0

#[test]
fn predict_labels_are_in_zero_one_domain() {
    let model = fit_separable_model();
    let (x, _y) = make_separable();
    let predictions = model.predict(&x).unwrap();
    for (i, &pred) in predictions.iter().enumerate() {
        assert!(
            pred == 0.0 || pred == 1.0,
            "prediction[{i}] = {pred} is not in {{0.0, 1.0}}"
        );
    }
}

// sign-consistency invariant
// Contract: predict(x)==1.0 iff decision_function(x)>0.0, else predict(x)==0.0

#[test]
fn sign_consistency_on_training_data() {
    let model = fit_separable_model();
    let (x, _y) = make_separable();
    let preds = model.predict(&x).unwrap();
    let scores = model.decision_function(&x).unwrap();
    for i in 0..preds.len() {
        let expected_pred = if scores[i] > 0.0 { 1.0 } else { 0.0 };
        assert_eq!(
            preds[i], expected_pred,
            "sign inconsistency at sample {i}: score={:.6} but predict={}",
            scores[i], preds[i]
        );
    }
}

#[test]
fn sign_consistency_on_new_points() {
    // [+10, 0] should score > 0 (class 1), [-10, 0] should score < 0 (class 0);
    // assert the invariant rather than a hardcoded class, since the sign axis may flip
    let model = fit_separable_model();
    let x_new = Array2::from_shape_vec((2, 2), vec![10.0, 0.0, -10.0, 0.0]).unwrap();
    let preds = model.predict(&x_new).unwrap();
    let scores = model.decision_function(&x_new).unwrap();
    for i in 0..2 {
        let expected_pred = if scores[i] > 0.0 { 1.0 } else { 0.0 };
        assert_eq!(
            preds[i], expected_pred,
            "sign consistency violated at sample {i}: score={:.6}, predict={}",
            scores[i], preds[i]
        );
    }
}

// correctness on separable data

/// On perfectly linearly-separable data the classifier predicts the true class
/// for every training sample
#[test]
fn predicts_all_training_samples_correctly_on_separable_data() {
    let (x, y) = make_separable();
    let mut model = LinearSVC::new(10_000, 0.01, RegularizationType::L2(0.01), true, 1e-6).unwrap();
    model.fit(&x, &y).unwrap();
    let preds = model.predict(&x).unwrap();
    // Ground truth: samples 0-3 are class 0, samples 4-7 are class 1
    let ground_truth = [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0];
    for (i, (&pred, &truth)) in preds.iter().zip(ground_truth.iter()).enumerate() {
        assert_eq!(
            pred, truth,
            "sample {i}: expected class {truth} but got {pred}"
        );
    }
}

/// fit_predict agrees with fit then predict on the same data
#[test]
fn fit_predict_agrees_with_fit_then_predict() {
    let (x, y) = make_separable();
    let mut model_a = LinearSVC::new(5000, 0.01, RegularizationType::L2(0.1), true, 1e-5).unwrap();
    let preds_a = model_a.fit_predict(&x, &y).unwrap();
    let preds_b = model_a.predict(&x).unwrap();
    assert_eq!(preds_a, preds_b, "fit_predict and fit→predict must agree");
}

// getters after fit

#[test]
fn getters_are_some_after_fit() {
    let model = fit_separable_model();
    assert!(
        model.get_weights().is_some(),
        "weights must be Some after fit"
    );
    assert!(model.get_bias().is_some(), "bias must be Some after fit");
    assert!(
        model.get_actual_iterations().is_some(),
        "n_iter must be Some after fit"
    );
}

#[test]
fn n_iter_is_in_valid_range() {
    let max_iter = 5000usize;
    let (x, y) = make_separable();
    let mut model =
        LinearSVC::new(max_iter, 0.01, RegularizationType::L2(0.1), true, 1e-5).unwrap();
    model.fit(&x, &y).unwrap();
    let n = model.get_actual_iterations().unwrap();
    assert!(n >= 1, "n_iter must be at least 1");
    assert!(
        n <= max_iter,
        "n_iter {n} must not exceed max_iter {max_iter}"
    );
}

/// Early stopping fires before max_iter on well-separated data with tight tolerance
#[test]
fn convergence_stops_before_max_iter_on_separable_data() {
    let max_iter = 50_000usize;
    let (x, y) = make_separable();
    let mut model =
        LinearSVC::new(max_iter, 0.01, RegularizationType::L2(0.01), true, 1e-3).unwrap();
    model.fit(&x, &y).unwrap();
    let n = model.get_actual_iterations().unwrap();
    assert!(
        n < max_iter,
        "expected early stopping (n_iter={n} < max_iter={max_iter})"
    );
}

#[test]
fn weights_have_correct_dimensionality() {
    let model = fit_separable_model();
    let weights = model.get_weights().unwrap();
    assert_eq!(
        weights.len(),
        2,
        "weight vector must match number of training features"
    );
}

// fit_intercept = false

/// When fit_intercept=false the bias is never updated and stays 0.0
#[test]
fn fit_intercept_false_bias_stays_zero() {
    // Data centred at origin lets the zero-bias model solve the problem
    let (x, y) = make_separable();
    let mut model = LinearSVC::new(5000, 0.01, RegularizationType::L2(0.1), false, 1e-5).unwrap();
    model.fit(&x, &y).unwrap();
    let bias = model.get_bias().unwrap();
    assert_eq!(
        bias, 0.0,
        "bias must stay exactly 0.0 when fit_intercept=false"
    );
}

/// When fit_intercept=false, decision_function(x) = x . weights exactly (bias is 0.0)
#[test]
fn fit_intercept_false_decision_function_equals_dot_product() {
    let (x, y) = make_separable();
    let mut model = LinearSVC::new(5000, 0.01, RegularizationType::L2(0.1), false, 1e-5).unwrap();
    model.fit(&x, &y).unwrap();

    let weights = model.get_weights().unwrap().clone();
    let bias = model.get_bias().unwrap();
    assert_eq!(bias, 0.0, "bias must be 0 for this test to be meaningful");

    let scores = model.decision_function(&x).unwrap();
    for i in 0..x.nrows() {
        let manual = x.row(i).dot(&weights) + bias;
        assert!(
            (scores[i] - manual).abs() < 1e-12,
            "decision_function[{i}]={:.10} != dot-product={:.10}",
            scores[i],
            manual
        );
    }
}

// L1 vs L2 penalty produce different weight vectors

/// L1 and L2 penalties with the same lambda on the same data yield different
/// weight vectors
#[test]
fn l1_and_l2_penalties_produce_different_weights() {
    let (x, y) = make_separable();

    let mut model_l2 = LinearSVC::new(3000, 0.01, RegularizationType::L2(1.0), true, 1e-6).unwrap();
    model_l2.fit(&x, &y).unwrap();
    let w_l2 = model_l2.get_weights().unwrap().clone();

    let mut model_l1 = LinearSVC::new(3000, 0.01, RegularizationType::L1(1.0), true, 1e-6).unwrap();
    model_l1.fit(&x, &y).unwrap();
    let w_l1 = model_l1.get_weights().unwrap().clone();

    assert_ne!(
        w_l1, w_l2,
        "L1 and L2 regularization must yield different weight vectors"
    );
}

/// Strong L1 keeps the irrelevant feature (column 1, all zeros) smaller in
/// magnitude than the relevant feature: |w[1]| < |w[0]|
#[test]
fn l1_sparsity_irrelevant_feature_closer_to_zero() {
    let (x, y) = make_separable();
    // x column 1 is all zeros, so a strong L1 penalty shrinks w[1] toward zero
    let mut model =
        LinearSVC::new(10_000, 0.001, RegularizationType::L1(10.0), true, 1e-7).unwrap();
    model.fit(&x, &y).unwrap();
    let w = model.get_weights().unwrap();
    assert!(
        w[1].abs() < w[0].abs(),
        "under strong L1 the irrelevant feature (w[1]={:.6}) should have smaller \
         magnitude than the relevant feature (w[0]={:.6})",
        w[1],
        w[0]
    );
}

// save / load round-trip

#[test]
fn save_load_round_trip_yields_identical_predictions() {
    let path = "/tmp/rustyml_linear_svc_roundtrip_test.json";

    let model = fit_separable_model();
    model.save_to_path(path).unwrap();

    let loaded = LinearSVC::load_from_path(path).unwrap();

    let (x, _) = make_separable();
    let preds_orig = model.predict(&x).unwrap();
    let preds_loaded = loaded.predict(&x).unwrap();

    assert_eq!(
        preds_orig, preds_loaded,
        "loaded model must produce identical predictions"
    );

    // Decision scores are also bit-identical after round-trip
    let scores_orig = model.decision_function(&x).unwrap();
    let scores_loaded = loaded.decision_function(&x).unwrap();
    for i in 0..scores_orig.len() {
        assert!(
            (scores_orig[i] - scores_loaded[i]).abs() < 1e-12,
            "decision score[{i}] changed after save/load: {:.10} vs {:.10}",
            scores_orig[i],
            scores_loaded[i]
        );
    }

    let _ = std::fs::remove_file(path);
}

#[test]
fn load_from_nonexistent_path_returns_io_error() {
    let result = LinearSVC::load_from_path("/tmp/this_file_does_not_exist_rustyml_svc.json");
    assert!(
        matches!(result, Err(Error::Io(_))),
        "loading from non-existent path must return Err(Io(...))"
    );
}

#[test]
fn save_load_preserves_hyperparameters() {
    let path = "/tmp/rustyml_linear_svc_hparams_test.json";

    let model = fit_separable_model();
    model.save_to_path(path).unwrap();
    let loaded = LinearSVC::load_from_path(path).unwrap();

    assert_eq!(loaded.get_max_iterations(), model.get_max_iterations());
    assert_eq!(loaded.get_learning_rate(), model.get_learning_rate());
    assert_eq!(loaded.get_tolerance(), model.get_tolerance());
    assert_eq!(loaded.get_fit_intercept(), model.get_fit_intercept());
    assert_eq!(loaded.get_penalty(), model.get_penalty());

    let _ = std::fs::remove_file(path);
}

// correctness invariant: class-1 must be on positive side

/// On separable data, all class-1 samples score positive and all class-0 samples
/// score negative
#[test]
fn class_one_samples_score_positive_class_zero_samples_score_negative() {
    let (x, y) = make_separable();
    let mut model = LinearSVC::new(10_000, 0.01, RegularizationType::L2(0.01), true, 1e-6).unwrap();
    model.fit(&x, &y).unwrap();

    let scores = model.decision_function(&x).unwrap();

    // First 4 samples are class 0, last 4 are class 1; the large margin means all
    // land on their respective sides of the hyperplane
    let mut all_correct = true;
    for i in 0..4 {
        if scores[i] >= 0.0 {
            all_correct = false;
        }
    }
    for i in 4..8 {
        if scores[i] <= 0.0 {
            all_correct = false;
        }
    }
    assert!(
        all_correct,
        "class-0 samples must score <0 and class-1 samples must score >0 on separable data; \
         scores = {:?}",
        scores
    );
}

// decision_function returns finite values

#[test]
fn decision_function_returns_finite_values() {
    let model = fit_separable_model();
    let (x, _) = make_separable();
    let scores = model.decision_function(&x).unwrap();
    for (i, &s) in scores.iter().enumerate() {
        assert!(s.is_finite(), "decision_function[{i}] = {s} is not finite");
    }
}

// reproducibility via random_state

// Training shuffles sample indices each epoch to form minibatches (batch size 32
// here), so the shuffle order affects the trained weights and the seed matters

/// 200 deterministic, roughly-separable samples
fn make_many() -> (Array2<f64>, Array1<f64>) {
    let n = 200;
    let mut x = Array2::zeros((n, 2));
    let mut y = Array1::zeros(n);
    for i in 0..n {
        let class = (i % 2) as f64;
        x[[i, 0]] = if class == 1.0 { 5.0 } else { -5.0 } + (i as f64 * 0.017).sin();
        x[[i, 1]] = (i as f64 * 0.013).cos();
        y[i] = class;
    }
    (x, y)
}

/// Two models trained with the same seed produce bit-identical weights and bias
#[test]
fn same_random_state_is_reproducible() {
    let (x, y) = make_many();
    let train = |seed| {
        let mut m = LinearSVC::new(300, 0.01, RegularizationType::L2(0.1), true, 1e-9)
            .unwrap()
            .with_random_state(seed);
        m.fit(&x, &y).unwrap();
        m
    };
    let a = train(42);
    let b = train(42);
    assert_eq!(
        a.get_weights().unwrap(),
        b.get_weights().unwrap(),
        "identical seed must yield identical weights"
    );
    assert_eq!(
        a.get_bias(),
        b.get_bias(),
        "identical seed must yield identical bias"
    );
}

/// Different seeds drive different minibatch orders, so the trained weights differ
#[test]
fn different_random_state_changes_result() {
    let (x, y) = make_many();
    let train = |seed| {
        let mut m = LinearSVC::new(300, 0.01, RegularizationType::L2(0.1), true, 1e-9)
            .unwrap()
            .with_random_state(seed);
        m.fit(&x, &y).unwrap();
        m.get_weights().unwrap().clone()
    };
    assert_ne!(
        train(1),
        train(2),
        "different seeds should produce different weights on this dataset"
    );
}
// in-training weight-explosion guard (check_weights_validity)

/// A huge but finite learning rate on large-magnitude finite features trips the
/// in-training weight-explosion guard and surfaces Error::NonFinite
#[test]
fn fit_huge_learning_rate_on_large_finite_data_returns_non_finite() {
    // Balanced, separable, large-magnitude finite data
    let x = Array2::from_shape_vec(
        (8, 2),
        vec![
            -1.0e8, 0.0, -1.0e8, 1.0, -1.0e8, -1.0, -1.0e8, 2.0, 1.0e8, 0.0, 1.0e8, 1.0, 1.0e8,
            -1.0, 1.0e8, 2.0,
        ],
    )
    .unwrap();
    let y = array![0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0];

    let mut model = LinearSVC::new(100, f64::MAX, RegularizationType::L2(1.0), true, 1e-6)
        .unwrap()
        .with_random_state(0);

    let result = model.fit(&x, &y);
    assert!(
        matches!(result, Err(Error::NonFinite(_))),
        "huge learning_rate must trip check_weights_validity, got {:?}",
        result
    );
}

// decision_function applies a non-zero fitted bias (fit_intercept=true)

/// Origin-shifted separable data forces a non-zero trained bias, so
/// decision_function(x)[i] must equal x.row(i).weights + bias from the getters
#[test]
fn decision_function_applies_nonzero_fitted_bias() {
    let x = Array2::from_shape_vec(
        (8, 2),
        vec![
            8.0, 0.0, 9.0, 0.0, 10.0, 0.0, 11.0, 0.0, // class 0
            14.0, 0.0, 15.0, 0.0, 16.0, 0.0, 17.0, 0.0, // class 1
        ],
    )
    .unwrap();
    let y = array![0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0];

    let mut model =
        LinearSVC::new(10_000, 0.001, RegularizationType::L2(0.01), true, 1e-7).unwrap();
    model.fit(&x, &y).unwrap();

    let weights = model.get_weights().unwrap().clone();
    let bias = model.get_bias().unwrap();

    // The shifted boundary forces a clearly non-zero intercept, which makes the
    // identity below a real test that the bias is applied
    assert!(
        bias.abs() > 1e-6,
        "origin-shifted separable data must yield a non-zero bias, got {bias}"
    );

    let scores = model.decision_function(&x).unwrap();
    for i in 0..x.nrows() {
        let manual = x.row(i).dot(&weights) + bias;
        assert!(
            (scores[i] - manual).abs() < 1e-9,
            "decision_function[{i}]={:.10} != x·w+b={:.10} (bias={:.10})",
            scores[i],
            manual,
            bias
        );
    }
}