aprender-core 0.30.0

Next-generation machine learning library in pure Rust
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

impl TinyModelRepr {
    /// Create a linear model representation
    ///
    /// # Arguments
    /// * `coefficients` - Feature coefficients
    /// * `intercept` - Bias term
    #[must_use]
    pub fn linear(coefficients: Vec<f32>, intercept: f32) -> Self {
        Self::Linear {
            coefficients,
            intercept,
        }
    }

    /// Create a decision stump representation
    ///
    /// # Arguments
    /// * `feature_idx` - Index of feature to split on
    /// * `threshold` - Split threshold
    /// * `left_value` - Prediction for values < threshold
    /// * `right_value` - Prediction for values >= threshold
    #[must_use]
    pub const fn stump(
        feature_idx: u16,
        threshold: f32,
        left_value: f32,
        right_value: f32,
    ) -> Self {
        Self::Stump {
            feature_idx,
            threshold,
            left_value,
            right_value,
        }
    }

    /// Create a Naive Bayes model representation
    ///
    /// # Arguments
    /// * `class_priors` - Prior probability for each class
    /// * `means` - Mean values `[n_classes][n_features]`
    /// * `variances` - Variance values `[n_classes][n_features]`
    #[must_use]
    pub fn naive_bayes(
        class_priors: Vec<f32>,
        means: Vec<Vec<f32>>,
        variances: Vec<Vec<f32>>,
    ) -> Self {
        Self::NaiveBayes {
            class_priors,
            means,
            variances,
        }
    }

    /// Create a K-Means model representation
    ///
    /// # Arguments
    /// * `centroids` - Cluster centroids `[n_clusters][n_features]`
    #[must_use]
    pub fn kmeans(centroids: Vec<Vec<f32>>) -> Self {
        Self::KMeans { centroids }
    }

    /// Create a logistic regression representation
    ///
    /// # Arguments
    /// * `coefficients` - Coefficients `[n_classes][n_features]`
    /// * `intercepts` - Intercept per class
    #[must_use]
    pub fn logistic_regression(coefficients: Vec<Vec<f32>>, intercepts: Vec<f32>) -> Self {
        Self::LogisticRegression {
            coefficients,
            intercepts,
        }
    }

    /// Create a KNN model representation
    ///
    /// # Arguments
    /// * `reference_points` - Stored reference samples
    /// * `labels` - Labels for each reference point
    /// * `k` - Number of neighbors
    #[must_use]
    pub fn knn(reference_points: Vec<Vec<f32>>, labels: Vec<u32>, k: u32) -> Self {
        Self::KNN {
            reference_points,
            labels,
            k,
        }
    }

    /// Create a compressed model representation
    ///
    /// # Arguments
    /// * `compression` - Compression strategy
    /// * `data` - Compressed bytes
    /// * `original_size` - Size before compression
    #[must_use]
    pub fn compressed(compression: DataCompression, data: Vec<u8>, original_size: usize) -> Self {
        Self::Compressed {
            compression,
            data,
            original_size,
        }
    }

    /// Total size in bytes (for bundling decisions)
    #[must_use]
    pub fn size_bytes(&self) -> usize {
        match self {
            Self::Linear { coefficients, .. } => coefficients.len() * 4 + 4,
            Self::Stump { .. } => 2 + 4 + 4 + 4, // u16 + 3*f32
            Self::NaiveBayes {
                class_priors,
                means,
                ..
            } => {
                let n_classes = class_priors.len();
                let n_features = means.first().map_or(0, Vec::len);
                n_classes * 4 + n_classes * n_features * 8 // priors + means + variances
            }
            Self::KMeans { centroids } => centroids.iter().map(|c| c.len() * 4).sum(),
            Self::LogisticRegression {
                coefficients,
                intercepts,
            } => {
                let coef_size: usize = coefficients.iter().map(|c| c.len() * 4).sum();
                coef_size + intercepts.len() * 4
            }
            Self::KNN {
                reference_points,
                labels,
                ..
            } => {
                let points_size: usize = reference_points.iter().map(|p| p.len() * 4).sum();
                points_size + labels.len() * 4 + 4 // points + labels + k
            }
            Self::Compressed { data, .. } => data.len(),
        }
    }

    /// Model type name
    #[must_use]
    pub const fn model_type(&self) -> &'static str {
        match self {
            Self::Linear { .. } => "linear",
            Self::Stump { .. } => "stump",
            Self::NaiveBayes { .. } => "naive_bayes",
            Self::KMeans { .. } => "kmeans",
            Self::LogisticRegression { .. } => "logistic_regression",
            Self::KNN { .. } => "knn",
            Self::Compressed { .. } => "compressed",
        }
    }

    /// Number of parameters in the model
    #[must_use]
    pub fn n_parameters(&self) -> usize {
        match self {
            Self::Linear { coefficients, .. } => coefficients.len() + 1,
            Self::Stump { .. } => 4, // feature_idx, threshold, left, right
            Self::NaiveBayes {
                class_priors,
                means,
                variances,
            } => {
                class_priors.len()
                    + means.iter().map(Vec::len).sum::<usize>()
                    + variances.iter().map(Vec::len).sum::<usize>()
            }
            Self::KMeans { centroids } => centroids.iter().map(Vec::len).sum(),
            Self::LogisticRegression {
                coefficients,
                intercepts,
            } => coefficients.iter().map(Vec::len).sum::<usize>() + intercepts.len(),
            Self::KNN {
                reference_points, ..
            } => reference_points.iter().map(Vec::len).sum(),
            Self::Compressed { original_size, .. } => original_size / 4, // approximate
        }
    }

    /// Number of features expected in input
    #[must_use]
    pub fn n_features(&self) -> Option<usize> {
        match self {
            Self::Linear { coefficients, .. } => Some(coefficients.len()),
            Self::NaiveBayes { means, .. } => means.first().map(Vec::len),
            Self::KMeans { centroids } => centroids.first().map(Vec::len),
            Self::LogisticRegression { coefficients, .. } => coefficients.first().map(Vec::len),
            Self::KNN {
                reference_points, ..
            } => reference_points.first().map(Vec::len),
            // Stump depends on feature_idx, Compressed unknown
            Self::Stump { .. } | Self::Compressed { .. } => None,
        }
    }

    /// Predict for a single sample (linear models only)
    ///
    /// # Arguments
    /// * `features` - Input feature vector
    ///
    /// # Returns
    /// Prediction or None if model type doesn't support direct prediction
    #[must_use]
    pub fn predict_linear(&self, features: &[f32]) -> Option<f32> {
        match self {
            Self::Linear {
                coefficients,
                intercept,
            } => {
                if features.len() != coefficients.len() {
                    return None;
                }
                let dot: f32 = coefficients
                    .iter()
                    .zip(features.iter())
                    .map(|(c, x)| c * x)
                    .sum();
                Some(dot + intercept)
            }
            _ => None,
        }
    }

    /// Predict for a single sample (stump only)
    ///
    /// # Arguments
    /// * `features` - Input feature vector
    ///
    /// # Returns
    /// Prediction or None if model type is not stump
    #[must_use]
    pub fn predict_stump(&self, features: &[f32]) -> Option<f32> {
        match self {
            Self::Stump {
                feature_idx,
                threshold,
                left_value,
                right_value,
            } => {
                let idx = *feature_idx as usize;
                features.get(idx).map(|&x| {
                    if x < *threshold {
                        *left_value
                    } else {
                        *right_value
                    }
                })
            }
            _ => None,
        }
    }

    /// Find nearest centroid (K-Means only)
    ///
    /// # Arguments
    /// * `features` - Input feature vector
    ///
    /// # Returns
    /// Index of nearest centroid or None if not K-Means
    #[must_use]
    pub fn predict_kmeans(&self, features: &[f32]) -> Option<usize> {
        match self {
            Self::KMeans { centroids } => {
                if centroids.is_empty() {
                    return None;
                }
                let expected_len = centroids[0].len();
                if features.len() != expected_len {
                    return None;
                }

                centroids
                    .iter()
                    .enumerate()
                    .map(|(idx, centroid)| {
                        let dist: f32 = centroid
                            .iter()
                            .zip(features.iter())
                            .map(|(c, x)| (c - x).powi(2))
                            .sum();
                        (idx, dist)
                    })
                    .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
                    .map(|(idx, _)| idx)
            }
            _ => None,
        }
    }

    /// Validate model integrity
    pub fn validate(&self) -> Result<(), TinyModelError> {
        match self {
            Self::Linear { coefficients, .. } => {
                if coefficients.is_empty() {
                    return Err(TinyModelError::EmptyModel);
                }
                for (i, &c) in coefficients.iter().enumerate() {
                    if !c.is_finite() {
                        return Err(TinyModelError::InvalidCoefficient { index: i, value: c });
                    }
                }
                Ok(())
            }
            Self::NaiveBayes {
                class_priors,
                means,
                variances,
            } => {
                if class_priors.is_empty() {
                    return Err(TinyModelError::EmptyModel);
                }
                if means.len() != class_priors.len() || variances.len() != class_priors.len() {
                    return Err(TinyModelError::ShapeMismatch {
                        message: "Means/variances length must match class_priors".into(),
                    });
                }
                // Check all variances are positive
                for (class_idx, var) in variances.iter().enumerate() {
                    for (feat_idx, &v) in var.iter().enumerate() {
                        if v <= 0.0 {
                            return Err(TinyModelError::InvalidVariance {
                                class: class_idx,
                                feature: feat_idx,
                                value: v,
                            });
                        }
                    }
                }
                Ok(())
            }
            Self::KMeans { centroids } => {
                if centroids.is_empty() {
                    return Err(TinyModelError::EmptyModel);
                }
                let first_len = centroids[0].len();
                for (i, c) in centroids.iter().enumerate() {
                    if c.len() != first_len {
                        return Err(TinyModelError::ShapeMismatch {
                            message: format!(
                                "Centroid {i} has length {}, expected {first_len}",
                                c.len()
                            ),
                        });
                    }
                }
                Ok(())
            }
            Self::KNN {
                reference_points,
                labels,
                k,
            } => {
                if reference_points.is_empty() {
                    return Err(TinyModelError::EmptyModel);
                }
                if reference_points.len() != labels.len() {
                    return Err(TinyModelError::ShapeMismatch {
                        message: "Reference points and labels must have same length".into(),
                    });
                }
                if *k == 0 || *k as usize > reference_points.len() {
                    return Err(TinyModelError::InvalidK {
                        k: *k,
                        n_samples: reference_points.len(),
                    });
                }
                Ok(())
            }
            _ => Ok(()),
        }
    }

    /// Check if this representation fits within a size budget
    #[must_use]
    pub fn fits_within(&self, max_bytes: usize) -> bool {
        self.size_bytes() <= max_bytes
    }

    /// Human-readable summary
    #[must_use]
    pub fn summary(&self) -> String {
        match self {
            Self::Linear { coefficients, .. } => {
                format!(
                    "Linear({} features, {} bytes)",
                    coefficients.len(),
                    self.size_bytes()
                )
            }
            Self::Stump {
                feature_idx,
                threshold,
                ..
            } => {
                format!("Stump(feature[{feature_idx}] < {threshold:.3})")
            }
            Self::NaiveBayes {
                class_priors,
                means,
                ..
            } => {
                let n_features = means.first().map_or(0, Vec::len);
                format!(
                    "NaiveBayes({} classes, {} features, {} bytes)",
                    class_priors.len(),
                    n_features,
                    self.size_bytes()
                )
            }
            Self::KMeans { centroids } => {
                let n_features = centroids.first().map_or(0, Vec::len);
                format!(
                    "KMeans({} clusters, {} features, {} bytes)",
                    centroids.len(),
                    n_features,
                    self.size_bytes()
                )
            }
            Self::LogisticRegression { coefficients, .. } => {
                let n_features = coefficients.first().map_or(0, Vec::len);
                format!(
                    "LogisticRegression({} classes, {} features, {} bytes)",
                    coefficients.len(),
                    n_features,
                    self.size_bytes()
                )
            }
            Self::KNN {
                reference_points,
                k,
                ..
            } => {
                let n_features = reference_points.first().map_or(0, Vec::len);
                format!(
                    "KNN(k={k}, {} samples, {} features, {} bytes)",
                    reference_points.len(),
                    n_features,
                    self.size_bytes()
                )
            }
            Self::Compressed {
                compression,
                data,
                original_size,
            } => {
                let ratio = if data.is_empty() {
                    1.0
                } else {
                    *original_size as f32 / data.len() as f32
                };
                format!(
                    "Compressed({}, {} bytes, {:.1}x ratio)",
                    compression.name(),
                    data.len(),
                    ratio
                )
            }
        }
    }
}