rust-ml 0.1.5

A collection of machine learning algorithms implemented in pure Rust (personal project for practice).
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
use crate::bench::classification_metrics::ClassificationMetrics;
use crate::builders::logistic_regression::LogisticRegressionBuilder;
use crate::core::activations::activation::Activation;
use crate::core::activations::activation_functions::ActivationFn;
use crate::core::activations::leaky_relu::LeakyReLU;
use crate::core::activations::relu::ReLU;
use crate::core::activations::sigmoid::Sigmoid;
use crate::core::activations::tanh::Tanh;
use crate::core::error::ModelError;
use crate::core::types::{Matrix, Vector};
use crate::model::core::base::{BaseModel, OptimizableModel};
use crate::model::core::classification_model::ClassificationModel;
use crate::model::core::param_collection::{GradientCollection, ParamCollection};
use ndarray::{ArrayView, ArrayViewMut, Dimension, IxDyn};

/// A logistic regression model for binary classification.
///
/// This model uses a linear combination of features and weights, followed by an activation function,
/// to predict the probability of an input belonging to a positive class.
#[derive(Debug)]
pub struct LogisticRegression {
    /// Model weights for each feature
    pub weights: Vector,
    /// Bias term (intercept)
    pub bias: Vector,
    /// Activation function used for prediction
    pub activation_fn: ActivationFn,
    /// Gradient of the loss function with respect to the weights
    dw: Vector,
    /// Gradient of the loss function with respect to the bias
    db: Vector,
    threshold: f64,
}

impl LogisticRegression {
    /// Creates a new LogisticRegression model with the specified number of features and activation function.
    ///
    /// # Arguments
    ///
    /// * `n_features` - The number of input features
    /// * `activation_fn` - The activation function to use
    ///
    /// # Returns
    ///
    /// A new LogisticRegression instance with weights initialized to zeros
    pub fn new(n_features: usize, activation_fn: ActivationFn, threshold: f64) -> Self {
        let weights = Vector::zeros(n_features);
        let bias = Vector::from_elem(1, 0.0);

        if !(0.0..=1.0).contains(&threshold) {
            panic!("Threshold must be between 0 and 1");
        }

        Self {
            weights,
            bias,
            activation_fn,
            threshold,
            dw: Vector::zeros(n_features),
            db: Vector::from_elem(1, 0.0),
        }
    }

    /// Returns a builder for creating LogisticRegression models with custom configurations.
    ///
    /// # Returns
    ///
    /// A new LogisticRegressionBuilder instance
    pub fn builder() -> LogisticRegressionBuilder {
        LogisticRegressionBuilder::new()
    }

    /// Computes the activation for the given input vector.
    ///
    /// # Arguments
    ///
    /// * `z` - Input vector
    ///
    /// # Returns
    ///
    /// The result of applying the activation function to the input vector
    fn compute_activation(&self, z: &Vector) -> Result<Vector, ModelError> {
        match self.activation_fn {
            ActivationFn::Sigmoid => Ok(Sigmoid::activate(z)),
            ActivationFn::ReLU => Ok(ReLU::activate(z)),
            ActivationFn::Tanh => Ok(Tanh::activate(z)),
            ActivationFn::LeakyReLU => Ok(LeakyReLU::activate(z)),
        }
    }

    fn compute_z(&self, x: &Matrix) -> Result<Vector, ModelError> {
        let z = self.weights.t().dot(x) + &self.bias;
        Ok(z)
    }

    /// Computes the derivative of the activation function for the given input vector.
    ///
    /// # Arguments
    ///
    /// * `z` - Input vector
    ///
    /// # Returns
    ///
    /// The derivative of the activation function applied to the input
    fn compute_derivative(&self, z: &Vector) -> Result<Vector, ModelError> {
        match self.activation_fn {
            ActivationFn::Sigmoid => Ok(Sigmoid::derivative(z)),
            ActivationFn::ReLU => Ok(ReLU::derivative(z)),
            ActivationFn::Tanh => Ok(Tanh::derivative(z)),
            ActivationFn::LeakyReLU => Ok(LeakyReLU::derivative(z)),
        }
    }
}

impl ParamCollection for LogisticRegression {
    fn get<D: Dimension>(&self, key: &str) -> Result<ArrayView<f64, D>, ModelError> {
        match key {
            "weights" => Ok(self.weights.view().into_dimensionality::<D>()?),
            "bias" => Ok(self.bias.view().into_dimensionality::<D>()?),
            _ => Err(ModelError::KeyError(key.to_string())),
        }
    }

    fn get_mut<D: Dimension>(&mut self, key: &str) -> Result<ArrayViewMut<f64, D>, ModelError> {
        match key {
            "weights" => Ok(self.weights.view_mut().into_dimensionality::<D>()?),
            "bias" => Ok(self.bias.view_mut().into_dimensionality::<D>()?),
            _ => Err(ModelError::KeyError(key.to_string())),
        }
    }

    fn set<D: Dimension>(&mut self, key: &str, value: ArrayView<f64, D>) -> Result<(), ModelError> {
        match key {
            "weights" => {
                self.weights.assign(&value.to_shape(self.weights.shape())?);
                Ok(())
            }
            "bias" => {
                self.bias.assign(&value.to_shape(self.bias.shape())?);
                Ok(())
            }
            _ => Err(ModelError::KeyError(key.to_string())),
        }
    }

    fn param_iter(&self) -> Vec<(&str, ArrayView<f64, IxDyn>)> {
        vec![
            ("weights", self.weights.view().into_dyn()),
            ("bias", self.bias.view().into_dyn()),
        ]
    }
}

impl GradientCollection for LogisticRegression {
    fn get_gradient<D: Dimension>(&self, key: &str) -> Result<ArrayView<f64, D>, ModelError> {
        match key {
            "weights" => Ok(self.dw.view().into_dimensionality::<D>()?),
            "bias" => Ok(self.db.view().into_dimensionality::<D>()?),
            _ => Err(ModelError::KeyError(key.to_string())),
        }
    }

    fn set_gradient<D: Dimension>(
        &mut self,
        key: &str,
        value: ArrayView<f64, D>,
    ) -> Result<(), ModelError> {
        match key {
            "weights" => {
                self.dw.assign(&value.to_shape(self.weights.shape())?);
                Ok(())
            }
            "bias" => {
                self.db.assign(&value.to_shape(self.bias.shape())?);
                Ok(())
            }
            _ => Err(ModelError::KeyError(key.to_string())),
        }
    }
}

impl OptimizableModel<Matrix, Vector> for LogisticRegression {
    fn forward(&self, input: &Matrix) -> Result<Vector, ModelError> {
        let z = self.compute_z(input)?;
        let a = self.compute_activation(&z)?;
        // Make activation numerically safer
        let epsilon = 1e-15;
        let a_safe = a.mapv(|val| val.max(epsilon).min(1.0 - epsilon));
        Ok(a_safe)
    }

    fn backward(&mut self, input: &Matrix, dz: &Vector) -> Result<(), ModelError> {
        let m = input.shape()[1] as f64;

        // Calculate gradients
        // For matrix dimensions to work correctly:
        // - input has shape (n_features, n_samples)
        // - dz has shape (n_samples,)
        // - to get dw with shape (n_features,), we need to do input.dot(dz)
        let dw = input.dot(dz) / m;
        let db = dz.sum() / m;

        // Set the gradients
        self.set_gradient("weights", dw.view())?;
        self.set_gradient("bias", ArrayView::from(&[db]))?;

        Ok(())
    }

    /// Computes the gradient of the loss function with regards to the prediction (dJ/dy)
    ///
    /// # Arguments
    ///
    /// * `x` - Input feature matrix
    /// * `y` - Expected output vector
    ///
    /// # Returns
    ///
    /// The gradient vector
    fn compute_output_gradient(&self, x: &Matrix, y: &Vector) -> Result<Vector, ModelError> {
        // Forward pass to get predictions
        let z = self.compute_z(x)?;
        let y_hat = self.compute_activation(&z)?;

        // Compute the derivative of the activation function
        let g_prime_of_z = self.compute_derivative(&z)?;

        // Compute the gradient of the loss function with respect to the predictions
        let dy = (1.0 - y) / (1.0 - &y_hat) - y / &y_hat;

        // Apply the chain rule to get the gradient of the loss function with respect to z
        // dz = dy * g'(z)
        let dz = dy * g_prime_of_z;
        Ok(dz)
    }
}

#[cfg(test)]
mod optimizable_model_tests {
    use ndarray::{ArrayView1, arr1, arr2};

    use crate::builders::builder::Builder;
    use crate::core::activations::activation::Activation;
    use crate::core::activations::activation_functions::ActivationFn;
    use crate::core::activations::sigmoid::Sigmoid;
    use crate::core::types::{Matrix, Scalar};
    use crate::model::core::base::OptimizableModel;
    use crate::model::core::param_collection::GradientCollection;
    use crate::model::logistic_regression::LogisticRegression;

    #[test]
    fn test_logistic_regression_forward_sigmoid() {
        // Create model
        let mut model = LogisticRegression::builder()
            .n_features(3)
            .activation_function(ActivationFn::Sigmoid)
            .build()
            .unwrap();
        // Assign weights and bias explicitly.
        let weights = arr1(&[0.5, -0.2, 0.1]);
        let bias = Scalar::from_elem((), 0.2);
        model.weights.assign(&weights);
        model.bias.assign(&bias);
        // Create input data
        let input = Matrix::zeros((3, 3));
        // Compute expected output.
        let z = model.weights.t().dot(&input) + bias;
        let a = Sigmoid::activate(&z);
        let expected_output = a;

        // Forward pass
        let output = model.forward(&input).unwrap();

        assert_eq!(output.shape(), [3]);
        assert_eq!(output, expected_output);
    }

    #[test]
    fn test_compute_output_gradient() {
        // Create model with sigmoid activation
        let mut model = LogisticRegression::builder()
            .n_features(2)
            .activation_function(ActivationFn::Sigmoid)
            .build()
            .unwrap();
        let weights = arr1(&[0.5, -0.3]);
        let bias = Scalar::from_elem((), 0.1);
        model.weights.assign(&weights);
        model.bias.assign(&bias);

        // Create test data
        let x = arr2(&[[0.2, 0.7], [0.3, 0.5]]);
        let y = arr1(&[0.5, 1.0]); // Expected outputs

        // Compute forward pass to get predictions
        let y_hat = model.forward(&x).unwrap();
        // Manually compute the gradient using the formula for sigmoid
        // dz = y_hat - y
        let expected_dz = &y_hat - &y;
        // Get the computed gradient
        let dz = model.compute_output_gradient(&x, &y).unwrap();

        // Check dimensions and values (with a small epsilon for floating point comparison)
        assert_eq!(dz.shape(), expected_dz.shape());

        for (a, b) in dz.iter().zip(expected_dz.iter()) {
            assert!((a - b).abs() < 1e-5, "Expected {}, got {}", b, a);
        }
    }

    #[test]
    fn test_backward() {
        // Create model with sigmoid activation
        let mut model = LogisticRegression::builder()
            .n_features(2)
            .activation_function(ActivationFn::Sigmoid)
            .build()
            .unwrap();
        let weights = arr1(&[0.5, -0.3]);
        let bias = Scalar::from_elem((), 0.1);
        model.weights.assign(&weights);
        model.bias.assign(&bias);

        // Create test data
        let x = arr2(&[[0.2, 0.7], [0.3, 0.5]]); // 2x2 matrix with 2 features and 2 samples

        // Create a test gradient vector
        let dz = arr1(&[0.1, -0.2]);

        println!("x.shape(): {:?}", x.shape());
        println!("dz.shape(): {:?}", dz.shape());

        // Call backward
        model.backward(&x, &dz).unwrap();

        // Manually compute the expected gradients
        // For weights: dw = (x.dot(dz)) / m where m is number of samples
        let m = x.shape()[1] as f64;
        let expected_dw = x.dot(&dz) / m;

        // For bias: db = sum(dz) / m
        let expected_db = dz.sum() / m;

        // Get the gradients from the model
        let actual_dw: ArrayView1<f64> = model.get_gradient("weights").unwrap();
        let actual_db: ArrayView1<f64> = model.get_gradient("bias").unwrap();
        let actual_db_value = actual_db[0];

        // Check dimensions and values (with a small epsilon for floating point comparison)
        assert!(
            (actual_db_value - expected_db).abs() < 1e-5,
            "Expected {}, got {}",
            expected_db,
            actual_db_value
        );

        for (a, b) in actual_dw.iter().zip(expected_dw.iter()) {
            assert!((a - b).abs() < 1e-5, "Expected {}, got {}", b, a);
        }
    }
}
/// Implementation of BaseModel trait for LogisticRegression
impl BaseModel<Matrix, Vector> for LogisticRegression {
    /// Makes binary predictions for the given input data.
    ///
    /// # Arguments
    ///
    /// * `x` - Input feature matrix
    ///
    /// # Returns
    ///
    /// A vector of predictions (0.0 or 1.0)
    fn predict(&self, x: &Matrix) -> Result<Vector, ModelError> {
        let bias = self.bias[0];
        let z = self.weights.dot(x) + bias;
        let a = self.compute_activation(&z)?;
        let y_hat = a.mapv(|x| if x >= self.threshold { 1.0 } else { 0.0 });
        Ok(y_hat)
    }

    /// Computes the cost/loss for the given input and expected output.
    ///
    /// # Arguments
    ///
    /// * `x` - Input feature matrix
    /// * `y` - Expected output vector
    ///
    /// # Returns
    ///
    /// The computed loss value
    fn compute_cost(&self, x: &Matrix, y: &Vector) -> Result<f64, ModelError> {
        let m = y.len() as f64;
        let y_hat = self.forward(x)?;
        let loss = -(y * y_hat.ln() + (1.0 - y) * (1.0 - &y_hat).ln());
        let cost = loss.sum() / m;
        Ok(cost)
    }
}

#[cfg(test)]
mod base_model_tests {
    use super::*;
    use crate::builders::builder::Builder;
    use crate::model::core::base::BaseModel;
    use ndarray::{arr1, arr2};

    #[test]
    fn test_predict() {
        // Create a model with known weights and bias
        let mut model = LogisticRegression::builder()
            .n_features(2)
            .activation_function(ActivationFn::Sigmoid)
            .build()
            .unwrap();
        model.weights = arr1(&[0.5, -0.5]);
        model.bias = arr1(&[0.1]);

        // Create test data
        let x = arr2(&[[0.2, 0.8], [0.9, 0.1]]); // 2 features, 2 examples

        // Predict
        let predictions = model.predict(&x).unwrap();
        // The expected output from the activation function is:
        // [0.4378235 , 0.61063923], so the prediction should return:
        // [0.0, 1.0]
        let expected = arr1(&[0.0, 1.0]);

        // Assert predictions are as expected
        assert_eq!(predictions.len(), 2);
        assert_eq!(predictions, expected);
    }

    #[test]
    fn test_compute_cost() {
        // Create a model
        let mut model = LogisticRegression::builder()
            .n_features(2)
            .activation_function(ActivationFn::Sigmoid)
            .build()
            .unwrap();

        model.weights = arr1(&[1.0, 1.0]);
        model.bias = arr1(&[0.0]);

        // Create test data
        let x = arr2(&[[10.0, -10.0], [10.0, -10.0]]); // 2 features, 2 examples
        let y = arr1(&[0.0, 1.0]); // Target values

        // First manually calculate the expected output
        let y_hat = model.forward(&x).unwrap();
        let loss = -(&y * &y_hat.ln() + (1.0 - &y) * (1.0 - &y_hat).ln());
        // &y * &y_hat.ln() + (1.0 - &y) *
        println!("loss: {:?}", loss);
        let expected_cost = loss.sum() / 2.0; // Average over the number of samples

        // Get the cost from the model
        let cost = model.compute_cost(&x, &y).unwrap();

        // Allow for small floating-point differences
        assert!(
            (cost - expected_cost).abs() < 1e-5,
            "Expected cost {}, got {}",
            expected_cost,
            cost
        );
    }

    #[test]
    fn test_predict_all_classes() {
        // Create model with weights that will produce both 0 and 1 predictions
        let mut model = LogisticRegression::builder()
            .n_features(2)
            .activation_function(ActivationFn::Sigmoid)
            .build()
            .unwrap();
        model.weights = arr1(&[2.0, -2.0]);
        model.bias = arr1(&[0.0]);

        // Create test data to generate both classes
        let x = arr2(&[
            [1.0, 0.1], // should predict 1 (positive activation)
            [0.1, 1.0], // should predict 0 (negative activation)
        ]);

        // Get predictions
        let predictions = model.predict(&x).unwrap();

        // Check that we have both classes
        assert_eq!(predictions[0], 1.0);
        assert_eq!(predictions[1], 0.0);
    }
}

/// Implementation of ClassificationModel trait for LogisticRegression
impl ClassificationModel<Matrix, Vector> for LogisticRegression {
    /// Calculates the accuracy of the model on the given data.
    ///
    /// # Arguments
    ///
    /// * `x` - Input feature matrix
    /// * `y` - Expected output vector
    ///
    /// # Returns
    ///
    /// The accuracy as a value between 0.0 and 1.0
    fn accuracy(&self, x: &Matrix, y: &Vector) -> Result<f64, ModelError> {
        let y_pred = self.predict(x)?;
        let y_pred_binary = y_pred.mapv(|val| if val >= self.threshold { 1.0 } else { 0.0 });
        let correct = y_pred_binary
            .iter()
            .zip(y.iter())
            .filter(|&(pred, actual)| (pred - actual).abs() < f64::EPSILON)
            .count();
        Ok(correct as f64 / y.len() as f64)
    }

    /// Calculates the loss (binary cross-entropy) of the model on the given data.
    ///
    /// # Arguments
    ///
    /// * `x` - Input feature matrix
    /// * `y` - Expected output vector
    ///
    /// # Returns
    ///
    /// The computed loss value
    fn loss(&self, x: &Matrix, y: &Vector) -> Result<f64, ModelError> {
        // Binary cross-entropy loss
        let y_pred = self.predict(x)?;
        let epsilon = 1e-15; // prevent log(0)
        let y_pred = y_pred.mapv(|val| val.max(epsilon).min(1.0 - epsilon));
        let loss = y
            .iter()
            .zip(y_pred.iter())
            .map(|(y_i, y_pred_i)| -y_i * y_pred_i.ln() - (1.0 - y_i) * (1.0 - y_pred_i).ln())
            .sum::<f64>()
            / y.len() as f64;
        Ok(loss)
    }

    /// Calculates the recall (sensitivity) of the model on the given data.
    ///
    /// # Arguments
    ///
    /// * `x` - Input feature matrix
    /// * `y` - Expected output vector
    ///
    /// # Returns
    ///
    /// The recall as a value between 0.0 and 1.0
    fn recall(&self, x: &Matrix, y: &Vector) -> Result<f64, ModelError> {
        let y_pred = self.predict(x)?;

        let true_positives = y_pred
            .iter()
            .zip(y.iter())
            .filter(|&(pred, actual)| *pred > 0.5 && *actual > 0.5)
            .count();

        let actual_positives = y.iter().filter(|&&actual| actual > 0.5).count();

        if actual_positives == 0 {
            return Ok(0.0);
        }

        Ok(true_positives as f64 / actual_positives as f64)
    }

    /// Calculates the F1 score of the model on the given data.
    ///
    /// # Arguments
    ///
    /// * `x` - Input feature matrix
    /// * `y` - Expected output vector
    ///
    /// # Returns
    ///
    /// The F1 score as a value between 0.0 and 1.0
    fn f1_score(&self, x: &Matrix, y: &Vector) -> Result<f64, ModelError> {
        let y_pred = self.predict(x)?;
        let y_pred_binary = y_pred.mapv(|val| if val >= 0.5 { 1.0 } else { 0.0 });

        let true_positives = y_pred_binary
            .iter()
            .zip(y.iter())
            .filter(|&(pred, actual)| *pred > 0.5 && *actual > 0.5)
            .count() as f64;

        let false_positives = y_pred_binary
            .iter()
            .zip(y.iter())
            .filter(|&(pred, actual)| *pred > 0.5 && *actual <= 0.5)
            .count() as f64;

        let false_negatives = y_pred_binary
            .iter()
            .zip(y.iter())
            .filter(|&(pred, actual)| *pred <= 0.5 && *actual > 0.5)
            .count() as f64;

        let precision = if true_positives + false_positives == 0.0 {
            0.0
        } else {
            true_positives / (true_positives + false_positives)
        };

        let recall = if true_positives + false_negatives == 0.0 {
            0.0
        } else {
            true_positives / (true_positives + false_negatives)
        };

        if precision + recall == 0.0 {
            return Ok(0.0);
        }

        Ok(2.0 * precision * recall / (precision + recall))
    }

    /// Computes all classification metrics for the model on the given data.
    ///
    /// # Arguments
    ///
    /// * `x` - Input feature matrix
    /// * `y` - Expected output vector
    ///
    /// # Returns
    ///
    /// A ClassificationMetrics struct containing accuracy, loss, precision, recall and F1 score
    fn compute_metrics(&self, x: &Matrix, y: &Vector) -> Result<ClassificationMetrics, ModelError> {
        let accuracy = self.accuracy(x, y)?;
        let loss = self.loss(x, y)?;
        let recall = self.recall(x, y)?;
        let f1 = self.f1_score(x, y)?;

        // Calculate precision
        let y_pred = self.predict(x)?;
        let y_pred_binary = y_pred.mapv(|val| if val >= 0.5 { 1.0 } else { 0.0 });

        let true_positives = y_pred_binary
            .iter()
            .zip(y.iter())
            .filter(|&(pred, actual)| *pred > 0.5 && *actual > 0.5)
            .count() as f64;

        let false_positives = y_pred_binary
            .iter()
            .zip(y.iter())
            .filter(|&(pred, actual)| *pred > 0.5 && *actual <= 0.5)
            .count() as f64;

        let precision = if true_positives + false_positives == 0.0 {
            0.0
        } else {
            true_positives / (true_positives + false_positives)
        };

        Ok(ClassificationMetrics {
            accuracy,
            loss,
            precision,
            recall,
            f1_score: f1,
        })
    }
}