rustyml 0.14.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
//! Estimator traits shared by every model and stateful transformer in the crate
//!
//! These traits give the otherwise independent estimators a single uniform contract
//! so that generic code can train, transform, and run predictions over any of them:
//!
//! - [`Fit`](crate::traits::Fit) - train a model from input data (`(&X, &Y)` for supervised
//!   models, `&X` for unsupervised ones)
//! - [`Predict`](crate::traits::Predict) - run inference on a fitted model, with the output
//!   type chosen by each model (labels, probabilities, cluster ids, anomaly scores, ...)
//! - [`Transform`](crate::traits::Transform) - project new data through a fitted transformer
//!   (`PCA`, `KernelPCA`, `StandardScaler`)
//! - [`FitTransform`](crate::traits::FitTransform) - fit and transform the training data in
//!   one call (`PCA`, `KernelPCA`, `TSNE`, `StandardScaler`)
//!
//! The traits live at the crate root because implementors span features: the
//! estimators in [`machine_learning`](crate::machine_learning) and the preprocessing
//! transformers in [`utils`](crate::utils). Each module also re-exports them
//! (`machine_learning::Fit`, `utils::Fit`), and both preludes bring them into scope
//!
//! Every implementor also exposes the same operations as inherent methods
//! (`model.fit(..)`, `model.predict(..)`). The trait implementations forward to them.
//! Rust's method resolution tries the inherent methods first, so existing call
//! sites are unaffected. Bring the traits into scope (directly or via a prelude) to
//! write code that is generic over the concrete estimator
//!
//! # Examples
//!
//! This example writes one routine that trains and scores any supervised estimator, without
//! naming a concrete model type:
//!
//! ```rust
//! use rustyml::traits::{Fit, Predict};
//! use ndarray::{Array1, Array2};
//!
//! fn train_and_predict<M>(model: &mut M, x: &Array2<f64>, y: &Array1<f64>) -> Array1<f64>
//! where
//!     M: for<'a> Fit<(&'a Array2<f64>, &'a Array1<f64>)>
//!         + for<'a> Predict<&'a Array2<f64>, Output = Array1<f64>>,
//! {
//!     model.fit((x, y)).unwrap();
//!     model.predict(x).unwrap()
//! }
//!
//! // Call it with any estimator that fits the bounds, e.g.:
//! // let preds = train_and_predict(&mut LinearRegression::default(), &x, &y);
//! ```

use crate::error::Error;
#[cfg(feature = "machine_learning")]
use crate::machine_learning::{
    DBSCAN, DecisionTree, IsolationForest, KMeans, KNN, KernelPCA, LDA, LinearRegression,
    LinearSVC, LogisticRegression, MeanShift, PCA, SVC, TSNE,
};
#[cfg(feature = "utils")]
use crate::utils::{MaxAbsScaler, MinMaxScaler, Normalizer, RobustScaler, StandardScaler};
#[cfg(feature = "machine_learning")]
use ndarray::{Array1, Ix1};
use ndarray::{Array2, ArrayBase, Data, Ix2};
#[cfg(feature = "machine_learning")]
use std::hash::Hash;

/// Trains an estimator from input data
///
/// `D` is the shape of the training data: a `(&features, &targets)` tuple for
/// supervised models and `&features` for unsupervised ones
///
/// # Type Parameters
///
/// - `D` - the training data accepted by this estimator
pub trait Fit<D> {
    /// Fits the model to `data`, returning a mutable reference to `self` for chaining
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if the input is invalid or training fails
    fn fit(&mut self, data: D) -> Result<&mut Self, Error>;
}

/// Runs inference with a fitted estimator
///
/// `X` is the input type, the feature matrix `&ArrayBase<S, Ix2>` for every model,
/// and [`Predict::Output`] is the prediction type the model produces
///
/// # Type Parameters
///
/// - `X` - the input accepted by this estimator's `predict`
pub trait Predict<X> {
    /// The prediction type produced by this estimator
    type Output;

    /// Predicts outputs for `input`
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if the model is not fitted or the input is invalid
    fn predict(&self, input: X) -> Result<Self::Output, Error>;
}

/// Projects new data through a fitted transformer (out-of-sample transform)
///
/// `X` is the input type (a feature matrix `&ArrayBase<S, Ix2>`), and
/// [`Transform::Output`] is the projected representation. The decomposition estimators that
/// learn a reusable projection ([`PCA`], [`KernelPCA`]) implement this trait. Manifold methods
/// such as [`TSNE`] embed only the data they are fitted on, and implement [`FitTransform`]
/// instead
///
/// # Type Parameters
///
/// - `X` - the input accepted by this transformer's `transform`
pub trait Transform<X> {
    /// The transformed representation produced by this transformer
    type Output;

    /// Transforms `input` using the fitted transformer
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if the transformer is not fitted or the input is invalid
    fn transform(&self, input: X) -> Result<Self::Output, Error>;
}

/// Fits a transformer and transforms the training data in a single call
///
/// `D` is the shape of the training data (`&features` for the unsupervised
/// transformers), and [`FitTransform::Output`] is the transformed training data.
/// It is the only entry point for transformers without an out-of-sample
/// projection, such as [`TSNE`]
///
/// # Type Parameters
///
/// - `D` - the training data accepted by this transformer
pub trait FitTransform<D> {
    /// The transformed representation produced by this transformer
    type Output;

    /// Fits the transformer to `data` and returns the transformed `data`
    ///
    /// # Errors
    ///
    /// Returns an [`Error`] if the input is invalid or fitting fails
    fn fit_transform(&mut self, data: D) -> Result<Self::Output, Error>;
}

/// Trait implementations for the [`machine_learning`](crate::machine_learning) estimators,
/// each forwarding to the model's inherent method of the same name
#[cfg(feature = "machine_learning")]
mod machine_learning_impls {
    use super::*;

    // Supervised estimators: Fit<(&X, &Y)>

    impl<'a, S1, S2> Fit<(&'a ArrayBase<S1, Ix2>, &'a ArrayBase<S2, Ix1>)> for LinearRegression
    where
        S1: Data<Elem = f64>,
        S2: Data<Elem = f64>,
    {
        fn fit(
            &mut self,
            data: (&'a ArrayBase<S1, Ix2>, &'a ArrayBase<S2, Ix1>),
        ) -> Result<&mut Self, Error> {
            let (x, y) = data;
            self.fit(x, y)
        }
    }

    impl<'a, S1, S2> Fit<(&'a ArrayBase<S1, Ix2>, &'a ArrayBase<S2, Ix1>)> for LogisticRegression
    where
        S1: Data<Elem = f64>,
        S2: Data<Elem = f64>,
    {
        fn fit(
            &mut self,
            data: (&'a ArrayBase<S1, Ix2>, &'a ArrayBase<S2, Ix1>),
        ) -> Result<&mut Self, Error> {
            let (x, y) = data;
            self.fit(x, y)
        }
    }

    impl<'a, S1, S2> Fit<(&'a ArrayBase<S1, Ix2>, &'a ArrayBase<S2, Ix1>)> for DecisionTree
    where
        S1: Data<Elem = f64> + Send + Sync,
        S2: Data<Elem = f64> + Send + Sync,
    {
        fn fit(
            &mut self,
            data: (&'a ArrayBase<S1, Ix2>, &'a ArrayBase<S2, Ix1>),
        ) -> Result<&mut Self, Error> {
            let (x, y) = data;
            self.fit(x, y)
        }
    }

    impl<'a, S1, S2> Fit<(&'a ArrayBase<S1, Ix2>, &'a ArrayBase<S2, Ix1>)> for LinearSVC
    where
        S1: Data<Elem = f64> + Send + Sync,
        S2: Data<Elem = f64> + Send + Sync,
    {
        fn fit(
            &mut self,
            data: (&'a ArrayBase<S1, Ix2>, &'a ArrayBase<S2, Ix1>),
        ) -> Result<&mut Self, Error> {
            let (x, y) = data;
            self.fit(x, y)
        }
    }

    impl<'a, S1, S2> Fit<(&'a ArrayBase<S1, Ix2>, &'a ArrayBase<S2, Ix1>)> for SVC
    where
        S1: Data<Elem = f64> + Send + Sync,
        S2: Data<Elem = f64> + Send + Sync,
    {
        fn fit(
            &mut self,
            data: (&'a ArrayBase<S1, Ix2>, &'a ArrayBase<S2, Ix1>),
        ) -> Result<&mut Self, Error> {
            let (x, y) = data;
            self.fit(x, y)
        }
    }

    impl<'a, T, S1, S2> Fit<(&'a ArrayBase<S1, Ix2>, &'a ArrayBase<S2, Ix1>)> for KNN<T>
    where
        T: Clone + Hash + Eq,
        S1: Data<Elem = f64>,
        S2: Data<Elem = T>,
    {
        fn fit(
            &mut self,
            data: (&'a ArrayBase<S1, Ix2>, &'a ArrayBase<S2, Ix1>),
        ) -> Result<&mut Self, Error> {
            let (x, y) = data;
            self.fit(x, y)
        }
    }

    impl<'a, S1, S2> Fit<(&'a ArrayBase<S1, Ix2>, &'a ArrayBase<S2, Ix1>)> for LDA
    where
        S1: Data<Elem = f64>,
        S2: Data<Elem = i32>,
    {
        fn fit(
            &mut self,
            data: (&'a ArrayBase<S1, Ix2>, &'a ArrayBase<S2, Ix1>),
        ) -> Result<&mut Self, Error> {
            let (x, y) = data;
            self.fit(x, y)
        }
    }

    // Unsupervised estimators: Fit<&X>

    impl<'a, S> Fit<&'a ArrayBase<S, Ix2>> for KMeans
    where
        S: Data<Elem = f64>,
    {
        fn fit(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<&mut Self, Error> {
            self.fit(data)
        }
    }

    impl<'a, S> Fit<&'a ArrayBase<S, Ix2>> for DBSCAN
    where
        S: Data<Elem = f64> + Send + Sync,
    {
        fn fit(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<&mut Self, Error> {
            self.fit(data)
        }
    }

    impl<'a, S> Fit<&'a ArrayBase<S, Ix2>> for MeanShift
    where
        S: Data<Elem = f64> + Send + Sync,
    {
        fn fit(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<&mut Self, Error> {
            self.fit(data)
        }
    }

    impl<'a, S> Fit<&'a ArrayBase<S, Ix2>> for IsolationForest
    where
        S: Data<Elem = f64> + Send + Sync,
    {
        fn fit(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<&mut Self, Error> {
            self.fit(data)
        }
    }

    impl<'a, S> Fit<&'a ArrayBase<S, Ix2>> for PCA
    where
        S: Data<Elem = f64>,
    {
        fn fit(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<&mut Self, Error> {
            self.fit(data)
        }
    }

    impl<'a, S> Fit<&'a ArrayBase<S, Ix2>> for KernelPCA
    where
        S: Data<Elem = f64> + Send + Sync,
    {
        fn fit(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<&mut Self, Error> {
            self.fit(data)
        }
    }

    // Predict<&X>

    impl<'a, S> Predict<&'a ArrayBase<S, Ix2>> for LinearRegression
    where
        S: Data<Elem = f64>,
    {
        type Output = Array1<f64>;
        fn predict(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.predict(input)
        }
    }

    impl<'a, S> Predict<&'a ArrayBase<S, Ix2>> for LogisticRegression
    where
        S: Data<Elem = f64>,
    {
        type Output = Array1<f64>;
        fn predict(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.predict(input)
        }
    }

    impl<'a, S> Predict<&'a ArrayBase<S, Ix2>> for DecisionTree
    where
        S: Data<Elem = f64>,
    {
        type Output = Array1<f64>;
        fn predict(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.predict(input)
        }
    }

    impl<'a, S> Predict<&'a ArrayBase<S, Ix2>> for LinearSVC
    where
        S: Data<Elem = f64>,
    {
        type Output = Array1<f64>;
        fn predict(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.predict(input)
        }
    }

    impl<'a, S> Predict<&'a ArrayBase<S, Ix2>> for SVC
    where
        S: Data<Elem = f64> + Send + Sync,
    {
        type Output = Array1<f64>;
        fn predict(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.predict(input)
        }
    }

    impl<'a, S> Predict<&'a ArrayBase<S, Ix2>> for KMeans
    where
        S: Data<Elem = f64>,
    {
        type Output = Array1<isize>;
        fn predict(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.predict(input)
        }
    }

    impl<'a, S> Predict<&'a ArrayBase<S, Ix2>> for MeanShift
    where
        S: Data<Elem = f64> + Sync,
    {
        type Output = Array1<isize>;
        fn predict(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.predict(input)
        }
    }

    impl<'a, S> Predict<&'a ArrayBase<S, Ix2>> for IsolationForest
    where
        S: Data<Elem = f64>,
    {
        type Output = Array1<i32>;
        fn predict(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.predict(input)
        }
    }

    impl<'a, T, S> Predict<&'a ArrayBase<S, Ix2>> for KNN<T>
    where
        T: Clone + Hash + Eq,
        S: Data<Elem = f64>,
    {
        type Output = Array1<T>;
        fn predict(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.predict(input)
        }
    }

    impl<'a, S> Predict<&'a ArrayBase<S, Ix2>> for LDA
    where
        S: Data<Elem = f64>,
    {
        type Output = Array1<i32>;
        fn predict(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.predict(input)
        }
    }

    impl<'a, S> Predict<&'a ArrayBase<S, Ix2>> for DBSCAN
    where
        S: Data<Elem = f64> + Send + Sync,
    {
        type Output = Array1<isize>;
        fn predict(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.predict(input)
        }
    }

    // Transformers: Transform<&X>

    impl<'a, S> Transform<&'a ArrayBase<S, Ix2>> for PCA
    where
        S: Data<Elem = f64>,
    {
        type Output = Array2<f64>;
        fn transform(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.transform(input)
        }
    }

    impl<'a, S> Transform<&'a ArrayBase<S, Ix2>> for KernelPCA
    where
        S: Data<Elem = f64> + Send + Sync,
    {
        type Output = Array2<f64>;
        fn transform(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.transform(input)
        }
    }

    // Transformers: FitTransform<&X>

    impl<'a, S> FitTransform<&'a ArrayBase<S, Ix2>> for PCA
    where
        S: Data<Elem = f64>,
    {
        type Output = Array2<f64>;
        fn fit_transform(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.fit_transform(data)
        }
    }

    impl<'a, S> FitTransform<&'a ArrayBase<S, Ix2>> for KernelPCA
    where
        S: Data<Elem = f64> + Send + Sync,
    {
        type Output = Array2<f64>;
        fn fit_transform(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.fit_transform(data)
        }
    }

    impl<'a, S> FitTransform<&'a ArrayBase<S, Ix2>> for TSNE
    where
        S: Data<Elem = f64>,
    {
        type Output = Array2<f64>;
        fn fit_transform(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            // t-SNE's inherent `fit_transform` takes `&self` (it stores no fitted state), while
            // this trait method takes `&mut self`. Reborrow as shared so method resolution
            // selects the inherent method here rather than recursing into this trait impl
            let this: &TSNE = self;
            this.fit_transform(data)
        }
    }
} // mod machine_learning_impls

/// Trait implementations for the stateful [`utils`](crate::utils) transformers, each
/// forwarding to the transformer's inherent method of the same name
#[cfg(feature = "utils")]
mod utils_impls {
    use super::*;

    impl<'a, S> Fit<&'a ArrayBase<S, Ix2>> for StandardScaler
    where
        S: Data<Elem = f64>,
    {
        fn fit(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<&mut Self, Error> {
            self.fit(data)
        }
    }

    impl<'a, S> Transform<&'a ArrayBase<S, Ix2>> for StandardScaler
    where
        S: Data<Elem = f64>,
    {
        type Output = Array2<f64>;
        fn transform(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.transform(input)
        }
    }

    impl<'a, S> FitTransform<&'a ArrayBase<S, Ix2>> for StandardScaler
    where
        S: Data<Elem = f64>,
    {
        type Output = Array2<f64>;
        fn fit_transform(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.fit_transform(data)
        }
    }

    impl<'a, S> Fit<&'a ArrayBase<S, Ix2>> for MinMaxScaler
    where
        S: Data<Elem = f64>,
    {
        fn fit(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<&mut Self, Error> {
            self.fit(data)
        }
    }

    impl<'a, S> Transform<&'a ArrayBase<S, Ix2>> for MinMaxScaler
    where
        S: Data<Elem = f64>,
    {
        type Output = Array2<f64>;
        fn transform(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.transform(input)
        }
    }

    impl<'a, S> FitTransform<&'a ArrayBase<S, Ix2>> for MinMaxScaler
    where
        S: Data<Elem = f64>,
    {
        type Output = Array2<f64>;
        fn fit_transform(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.fit_transform(data)
        }
    }

    impl<'a, S> Fit<&'a ArrayBase<S, Ix2>> for MaxAbsScaler
    where
        S: Data<Elem = f64>,
    {
        fn fit(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<&mut Self, Error> {
            self.fit(data)
        }
    }

    impl<'a, S> Transform<&'a ArrayBase<S, Ix2>> for MaxAbsScaler
    where
        S: Data<Elem = f64>,
    {
        type Output = Array2<f64>;
        fn transform(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.transform(input)
        }
    }

    impl<'a, S> FitTransform<&'a ArrayBase<S, Ix2>> for MaxAbsScaler
    where
        S: Data<Elem = f64>,
    {
        type Output = Array2<f64>;
        fn fit_transform(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.fit_transform(data)
        }
    }

    impl<'a, S> Fit<&'a ArrayBase<S, Ix2>> for RobustScaler
    where
        S: Data<Elem = f64>,
    {
        fn fit(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<&mut Self, Error> {
            self.fit(data)
        }
    }

    impl<'a, S> Transform<&'a ArrayBase<S, Ix2>> for RobustScaler
    where
        S: Data<Elem = f64>,
    {
        type Output = Array2<f64>;
        fn transform(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.transform(input)
        }
    }

    impl<'a, S> FitTransform<&'a ArrayBase<S, Ix2>> for RobustScaler
    where
        S: Data<Elem = f64>,
    {
        type Output = Array2<f64>;
        fn fit_transform(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.fit_transform(data)
        }
    }

    impl<'a, S> Fit<&'a ArrayBase<S, Ix2>> for Normalizer
    where
        S: Data<Elem = f64>,
    {
        fn fit(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<&mut Self, Error> {
            self.fit(data)
        }
    }

    impl<'a, S> Transform<&'a ArrayBase<S, Ix2>> for Normalizer
    where
        S: Data<Elem = f64>,
    {
        type Output = Array2<f64>;
        fn transform(&self, input: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.transform(input)
        }
    }

    impl<'a, S> FitTransform<&'a ArrayBase<S, Ix2>> for Normalizer
    where
        S: Data<Elem = f64>,
    {
        type Output = Array2<f64>;
        fn fit_transform(&mut self, data: &'a ArrayBase<S, Ix2>) -> Result<Self::Output, Error> {
            self.fit_transform(data)
        }
    }
}

/// Unit tests for the `Fit`, `Predict`, `Transform`, and `FitTransform` forwarding impls
#[cfg(all(test, feature = "machine_learning"))]
mod tests {
    use super::*;
    use crate::machine_learning::linear_model::LeastSquaresSolver;
    use ndarray::{Array1, Array2};

    #[test]
    fn fit_and_predict_through_traits() {
        // Trait methods forward to the inherent methods, so they do not recurse
        let x = Array2::from_shape_vec((3, 1), vec![1.0, 2.0, 3.0]).unwrap();
        let y = Array1::from_vec(vec![2.0, 4.0, 6.0]);

        let mut model = LinearRegression::new(true)
            .with_solver(LeastSquaresSolver::GradientDescent {
                learning_rate: 0.05,
                max_iter: 5000,
                tol: 1e-9,
            })
            .unwrap();
        Fit::fit(&mut model, (&x, &y)).unwrap();
        let preds = Predict::predict(&model, &x).unwrap();

        assert_eq!(preds.len(), 3);
        for (p, t) in preds.iter().zip(y.iter()) {
            assert!((p - t).abs() < 0.5);
        }
    }

    #[test]
    fn transformers_through_traits() {
        // The transformer trait methods forward to the inherent methods. For t-SNE, the inherent
        // method takes `&self` while the trait takes `&mut self`. This also guards against a
        // forwarding recursion, which would overflow the stack instead of failing an assert.
        use crate::machine_learning::{KernelPCA, KernelType, PCA, TSNE, TSNEMethod};

        let x = ndarray::array![[0.0, 1.0], [1.0, 0.0], [2.0, 2.0]];

        // PCA: Fit, then Transform, both through the traits
        let mut pca = PCA::new(2).unwrap();
        Fit::fit(&mut pca, &x).unwrap();
        let projected = Transform::transform(&pca, &x).unwrap();
        assert_eq!(projected.ncols(), 2);

        // PCA / KernelPCA: FitTransform through the trait
        let mut pca2 = PCA::new(2).unwrap();
        assert_eq!(
            FitTransform::fit_transform(&mut pca2, &x).unwrap().ncols(),
            2
        );
        let mut kpca = KernelPCA::new(KernelType::Linear, 2).unwrap();
        assert_eq!(
            FitTransform::fit_transform(&mut kpca, &x).unwrap().ncols(),
            2
        );

        // TSNE: FitTransform through the trait (forwards `&mut self` -> inherent `&self`)
        let mut tsne = TSNE::new(2, 2.0, 200.0, 50)
            .unwrap()
            .with_random_state(42)
            .with_method(TSNEMethod::Exact)
            .unwrap();
        let embedding = FitTransform::fit_transform(&mut tsne, &x).unwrap();
        assert_eq!(embedding.ncols(), 2);
    }
}

/// Unit tests for the `Fit`/`Transform` forwarding impls on the `utils` scalers
#[cfg(all(test, feature = "utils"))]
mod utils_tests {
    use super::*;
    use crate::utils::StandardScaler;

    /// The scaler's trait methods forward to its inherent methods without recursing, and the
    /// fitted statistics survive the `Fit` -> `Transform` hand-off
    #[test]
    fn scaler_through_traits() {
        let x_train = ndarray::array![[1.0, 100.0], [2.0, 150.0], [3.0, 200.0]];
        let x_test = ndarray::array![[4.0, 250.0]];

        let mut scaler = StandardScaler::new();
        Fit::fit(&mut scaler, &x_train).unwrap();
        let scaled = Transform::transform(&scaler, &x_test).unwrap();
        assert_eq!(scaled.dim(), (1, 2));
        // Scaled by the training statistics: (4 - 2) / sqrt(2/3)
        assert!((scaled[[0, 0]] - 2.449_489_742_783_178).abs() < 1e-12);

        let mut scaler2 = StandardScaler::new();
        assert_eq!(
            FitTransform::fit_transform(&mut scaler2, &x_train)
                .unwrap()
                .dim(),
            (3, 2)
        );
    }
}