sklears-datasets 0.2.0

Dataset utilities and generation for sklears
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
//! Basic synthetic data generators
//!
//! This module contains fundamental dataset generation functions including
//! blobs, classification, regression, circles, moons, and other basic patterns.

use scirs2_core::ndarray::{s, Array1, Array2};
use scirs2_core::random::prelude::*;
use scirs2_core::random::rngs::StdRng;
use scirs2_core::random::{Normal, RngExt, StandardNormal};
use sklears_core::error::{Result, SklearsError};
use std::f64::consts::PI;

pub fn make_blobs(
    n_samples: usize,
    n_features: usize,
    centers: usize,
    cluster_std: f64,
    random_state: Option<u64>,
) -> Result<(Array2<f64>, Array1<i32>)> {
    if n_samples == 0 || n_features == 0 || centers == 0 {
        return Err(SklearsError::InvalidInput(
            "n_samples, n_features, and centers must be positive".to_string(),
        ));
    }

    let mut rng = if let Some(seed) = random_state {
        StdRng::seed_from_u64(seed)
    } else {
        StdRng::from_rng(&mut scirs2_core::random::thread_rng())
    };

    // Generate random centers
    let mut center_points = Array2::zeros((centers, n_features));
    for i in 0..centers {
        for j in 0..n_features {
            center_points[[i, j]] = rng.random_range(-10.0..10.0);
        }
    }

    // Assign samples to centers
    let samples_per_center = n_samples / centers;
    let extra_samples = n_samples % centers;

    let mut x = Array2::zeros((n_samples, n_features));
    let mut y = Array1::zeros(n_samples);

    let mut sample_idx = 0;

    for center_idx in 0..centers {
        let n_samples_for_center = if center_idx < extra_samples {
            samples_per_center + 1
        } else {
            samples_per_center
        };

        let normal = Normal::new(0.0, cluster_std).expect("operation should succeed");

        for _ in 0..n_samples_for_center {
            y[sample_idx] = center_idx as i32;

            for feature_idx in 0..n_features {
                let center_value = center_points[[center_idx, feature_idx]];
                let noise: f64 = rng.sample(normal);
                x[[sample_idx, feature_idx]] = center_value + noise;
            }

            sample_idx += 1;
        }
    }

    Ok((x, y))
}

pub fn make_classification(
    n_samples: usize,
    n_features: usize,
    n_informative: usize,
    n_redundant: usize,
    n_classes: usize,
    random_state: Option<u64>,
) -> Result<(Array2<f64>, Array1<i32>)> {
    if n_samples == 0 || n_features == 0 || n_classes < 2 {
        return Err(SklearsError::InvalidInput(
            "n_samples and n_features must be positive, n_classes must be >= 2".to_string(),
        ));
    }

    if n_informative + n_redundant > n_features {
        return Err(SklearsError::InvalidInput(
            "n_informative + n_redundant cannot exceed n_features".to_string(),
        ));
    }

    let mut rng = if let Some(seed) = random_state {
        StdRng::seed_from_u64(seed)
    } else {
        StdRng::from_rng(&mut scirs2_core::random::thread_rng())
    };

    let normal = StandardNormal;

    // Generate informative features
    let mut x = Array2::zeros((n_samples, n_features));
    let mut y = Array1::zeros(n_samples);

    // Assign classes randomly
    for i in 0..n_samples {
        y[i] = rng.random_range(0..n_classes) as i32;
    }

    // Generate informative features based on class
    for i in 0..n_samples {
        let class = y[i] as usize;
        for j in 0..n_informative {
            let class_offset = (class as f64 - (n_classes as f64 - 1.0) / 2.0) * 2.0;
            x[[i, j]] = rng.sample::<f64, _>(normal) + class_offset;
        }
    }

    // Generate redundant features as linear combinations of informative features
    for j in n_informative..(n_informative + n_redundant) {
        let informative_idx = rng.random_range(0..n_informative);
        let weight = rng.random_range(-1.0..1.0);
        for i in 0..n_samples {
            x[[i, j]] = x[[i, informative_idx]] * weight + rng.sample::<f64, _>(normal) * 0.1;
        }
    }

    // Fill remaining features with random noise
    for j in (n_informative + n_redundant)..n_features {
        for i in 0..n_samples {
            x[[i, j]] = rng.sample::<f64, _>(normal);
        }
    }

    Ok((x, y))
}

pub fn make_regression(
    n_samples: usize,
    n_features: usize,
    n_informative: usize,
    noise: f64,
    random_state: Option<u64>,
) -> Result<(Array2<f64>, Array1<f64>)> {
    if n_samples == 0 || n_features == 0 {
        return Err(SklearsError::InvalidInput(
            "n_samples and n_features must be positive".to_string(),
        ));
    }

    if n_informative > n_features {
        return Err(SklearsError::InvalidInput(
            "n_informative cannot exceed n_features".to_string(),
        ));
    }

    let mut rng = if let Some(seed) = random_state {
        StdRng::seed_from_u64(seed)
    } else {
        StdRng::from_rng(&mut scirs2_core::random::thread_rng())
    };

    let normal = StandardNormal;

    // Generate feature matrix
    let mut x = Array2::zeros((n_samples, n_features));
    for i in 0..n_samples {
        for j in 0..n_features {
            x[[i, j]] = rng.sample::<f64, _>(normal);
        }
    }

    // Generate true coefficients for informative features
    let mut coef = Array1::zeros(n_features);
    for i in 0..n_informative {
        coef[i] = rng.random_range(-1.0..1.0) * 100.0;
    }

    // Compute target values
    let mut y = Array1::zeros(n_samples);
    for i in 0..n_samples {
        let mut target = 0.0;
        for j in 0..n_informative {
            target += x[[i, j]] * coef[j];
        }

        // Add noise
        if noise > 0.0 {
            let noise_dist = Normal::new(0.0, noise).expect("operation should succeed");
            target += rng.sample(noise_dist);
        }

        y[i] = target;
    }

    Ok((x, y))
}

pub fn make_circles(
    n_samples: usize,
    noise: Option<f64>,
    factor: f64,
    random_state: Option<u64>,
) -> Result<(Array2<f64>, Array1<i32>)> {
    if n_samples == 0 {
        return Err(SklearsError::InvalidInput(
            "n_samples must be positive".to_string(),
        ));
    }

    if factor <= 0.0 || factor >= 1.0 {
        return Err(SklearsError::InvalidInput(
            "factor must be between 0 and 1".to_string(),
        ));
    }

    let mut rng = if let Some(seed) = random_state {
        StdRng::seed_from_u64(seed)
    } else {
        StdRng::from_rng(&mut scirs2_core::random::thread_rng())
    };

    let n_samples_out = n_samples / 2;
    let n_samples_in = n_samples - n_samples_out;

    let mut x = Array2::zeros((n_samples, 2));
    let mut y = Array1::zeros(n_samples);

    // Generate outer circle
    for i in 0..n_samples_out {
        let angle = rng.random::<f64>() * 2.0 * PI;
        x[[i, 0]] = angle.cos();
        x[[i, 1]] = angle.sin();
        y[i] = 0;
    }

    // Generate inner circle
    for i in 0..n_samples_in {
        let angle = rng.random::<f64>() * 2.0 * PI;
        x[[n_samples_out + i, 0]] = factor * angle.cos();
        x[[n_samples_out + i, 1]] = factor * angle.sin();
        y[n_samples_out + i] = 1;
    }

    // Add noise if specified
    if let Some(noise_level) = noise {
        if noise_level > 0.0 {
            let noise_dist = Normal::new(0.0, noise_level).expect("operation should succeed");
            for i in 0..n_samples {
                x[[i, 0]] += rng.sample(noise_dist);
                x[[i, 1]] += rng.sample(noise_dist);
            }
        }
    }

    Ok((x, y))
}

pub fn make_moons(
    n_samples: usize,
    noise: Option<f64>,
    random_state: Option<u64>,
) -> Result<(Array2<f64>, Array1<i32>)> {
    if n_samples == 0 {
        return Err(SklearsError::InvalidInput(
            "n_samples must be positive".to_string(),
        ));
    }

    let mut rng = if let Some(seed) = random_state {
        StdRng::seed_from_u64(seed)
    } else {
        StdRng::from_rng(&mut scirs2_core::random::thread_rng())
    };

    let n_samples_out = n_samples / 2;
    let n_samples_in = n_samples - n_samples_out;

    let mut x = Array2::zeros((n_samples, 2));
    let mut y = Array1::zeros(n_samples);

    // Generate outer moon
    for i in 0..n_samples_out {
        let t = rng.random::<f64>() * PI;
        x[[i, 0]] = t.cos();
        x[[i, 1]] = t.sin();
        y[i] = 0;
    }

    // Generate inner moon
    for i in 0..n_samples_in {
        let t = rng.random::<f64>() * PI;
        x[[n_samples_out + i, 0]] = 1.0 - t.cos();
        x[[n_samples_out + i, 1]] = 1.0 - t.sin() - 0.5;
        y[n_samples_out + i] = 1;
    }

    // Add noise if specified
    if let Some(noise_level) = noise {
        if noise_level > 0.0 {
            let noise_dist = Normal::new(0.0, noise_level).expect("operation should succeed");
            for i in 0..n_samples {
                x[[i, 0]] += rng.sample(noise_dist);
                x[[i, 1]] += rng.sample(noise_dist);
            }
        }
    }

    Ok((x, y))
}

pub fn make_gaussian_quantiles(
    n_samples: usize,
    n_features: usize,
    n_classes: usize,
    random_state: Option<u64>,
) -> Result<(Array2<f64>, Array1<i32>)> {
    if n_samples == 0 || n_features == 0 || n_classes < 2 {
        return Err(SklearsError::InvalidInput(
            "n_samples, n_features must be positive, n_classes must be >= 2".to_string(),
        ));
    }

    let mut rng = if let Some(seed) = random_state {
        StdRng::seed_from_u64(seed)
    } else {
        StdRng::from_rng(&mut scirs2_core::random::thread_rng())
    };

    let normal = StandardNormal;

    // Generate multivariate normal data
    let mut x = Array2::zeros((n_samples, n_features));
    for i in 0..n_samples {
        for j in 0..n_features {
            x[[i, j]] = rng.sample::<f64, _>(normal);
        }
    }

    // Calculate norm for each sample
    let mut norms = Array1::zeros(n_samples);
    for i in 0..n_samples {
        let norm = x.slice(s![i, ..]).mapv(|x| x * x).sum().sqrt();
        norms[i] = norm;
    }

    // Sort by norm to create quantiles
    let mut indices: Vec<usize> = (0..n_samples).collect();
    indices.sort_by(|&a, &b| {
        norms[a]
            .partial_cmp(&norms[b])
            .expect("operation should succeed")
    });

    // Assign classes based on quantiles
    let mut y = Array1::zeros(n_samples);
    let samples_per_class = n_samples / n_classes;

    for (class_idx, chunk) in indices.chunks(samples_per_class).enumerate() {
        let class = (class_idx.min(n_classes - 1)) as i32;
        for &sample_idx in chunk {
            y[sample_idx] = class;
        }
    }

    Ok((x, y))
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_make_blobs() {
        let (x, y) = make_blobs(100, 2, 3, 1.0, Some(42)).expect("operation should succeed");
        assert_eq!(x.shape(), &[100, 2]);
        assert_eq!(y.len(), 100);

        // Check that we have the right number of classes
        let mut classes = y.iter().cloned().collect::<Vec<_>>();
        classes.sort();
        classes.dedup();
        assert_eq!(classes.len(), 3);
    }

    #[test]
    fn test_make_classification() {
        let (x, y) =
            make_classification(100, 20, 10, 5, 3, Some(42)).expect("operation should succeed");
        assert_eq!(x.shape(), &[100, 20]);
        assert_eq!(y.len(), 100);

        // Check that we have the right number of classes
        let mut classes = y.iter().cloned().collect::<Vec<_>>();
        classes.sort();
        classes.dedup();
        assert!(classes.len() <= 3);
    }

    #[test]
    fn test_make_regression() {
        let (x, y) = make_regression(50, 10, 5, 0.1, Some(42)).expect("operation should succeed");
        assert_eq!(x.shape(), &[50, 10]);
        assert_eq!(y.len(), 50);

        // Check that target values have some variation
        let mean = y
            .mean()
            .expect("array should have elements for mean computation");
        let variance = y
            .mapv(|v| (v - mean).powi(2))
            .mean()
            .expect("array should have elements for mean computation");
        assert!(variance > 0.0);
    }

    #[test]
    fn test_make_circles() {
        let (x, y) = make_circles(100, Some(0.1), 0.4, Some(42)).expect("operation should succeed");
        assert_eq!(x.shape(), &[100, 2]);
        assert_eq!(y.len(), 100);

        // Check that we have two classes
        let mut classes = y.iter().cloned().collect::<Vec<_>>();
        classes.sort();
        classes.dedup();
        assert_eq!(classes.len(), 2);
    }

    #[test]
    fn test_make_moons() {
        let (x, y) = make_moons(80, Some(0.15), Some(42)).expect("operation should succeed");
        assert_eq!(x.shape(), &[80, 2]);
        assert_eq!(y.len(), 80);

        // Check that we have two classes
        let mut classes = y.iter().cloned().collect::<Vec<_>>();
        classes.sort();
        classes.dedup();
        assert_eq!(classes.len(), 2);
    }

    #[test]
    fn test_make_gaussian_quantiles() {
        let (x, y) =
            make_gaussian_quantiles(120, 5, 3, Some(42)).expect("operation should succeed");
        assert_eq!(x.shape(), &[120, 5]);
        assert_eq!(y.len(), 120);

        // Check that we have the right number of classes
        let mut classes = y.iter().cloned().collect::<Vec<_>>();
        classes.sort();
        classes.dedup();
        assert!(classes.len() <= 3);
    }

    #[test]
    fn test_invalid_inputs() {
        // Test invalid n_samples
        assert!(make_blobs(0, 2, 3, 1.0, Some(42)).is_err());

        // Test invalid factor for circles
        assert!(make_circles(100, Some(0.1), 1.5, Some(42)).is_err());

        // Test invalid n_informative for classification
        assert!(make_classification(100, 5, 10, 0, 3, Some(42)).is_err());
    }
}