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
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
//! Synthetic data generators

use scirs2_core::ndarray::{s, Array1, Array2, Array3};
use scirs2_core::random::prelude::*;
use scirs2_core::random::rngs::StdRng;
use scirs2_core::random::{Gamma, Normal, 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.gen_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.gen_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.gen_range(0..n_informative);
        let weight = rng.gen_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.gen_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: 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 noise = noise.unwrap_or(0.0);
    let factor = factor.unwrap_or(0.8);

    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.gen() * 2.0 * PI;
        x[[i, 0]] = angle.cos();
        x[[i, 1]] = angle.sin();
        y[i] = 0;
    }

    // Generate inner circle
    for i in n_samples_out..n_samples {
        let angle = rng.gen() * 2.0 * PI;
        x[[i, 0]] = angle.cos() * factor;
        x[[i, 1]] = angle.sin() * factor;
        y[i] = 1;
    }

    // Add noise if specified
    if noise > 0.0 {
        let noise_dist = Normal::new(0.0, noise).expect("operation should succeed");
        for i in 0..n_samples {
            for j in 0..2 {
                x[[i, j]] += 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 noise = noise.unwrap_or(0.0);

    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 angle = rng.gen() * PI;
        x[[i, 0]] = angle.cos();
        x[[i, 1]] = angle.sin();
        y[i] = 0;
    }

    // Generate inner moon (rotated and shifted)
    for i in n_samples_out..n_samples {
        let angle = rng.gen() * PI;
        x[[i, 0]] = 1.0 - angle.cos();
        x[[i, 1]] = 1.0 - angle.sin() - 0.5;
        y[i] = 1;
    }

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

    Ok((x, y))
}

pub fn make_gaussian_quantiles(
    mean: Option<Array1<f64>>,
    cov: Option<f64>,
    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 and 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;
    let cov_value = cov.unwrap_or(1.0);

    // Set mean to zero vector if not provided
    let mean_vec = mean.unwrap_or_else(|| Array1::zeros(n_features));

    if mean_vec.len() != n_features {
        return Err(SklearsError::InvalidInput(
            "Mean vector length must match n_features".to_string(),
        ));
    }

    // Generate multivariate normal samples
    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) * cov_value.sqrt() + mean_vec[j];
        }
    }

    // Calculate distances from the mean for each sample
    let mut distances: Vec<(f64, usize)> = Vec::with_capacity(n_samples);
    for i in 0..n_samples {
        let mut distance_squared = 0.0;
        for j in 0..n_features {
            let diff = x[[i, j]] - mean_vec[j];
            distance_squared += diff * diff;
        }
        distances.push((distance_squared.sqrt() as f64, i));
    }

    // Sort by distance
    distances.sort_by(|a, b| a.0.partial_cmp(&b.0).expect("operation should succeed"));

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

    let mut current_idx = 0;
    for class in 0..n_classes {
        let n_samples_for_class = if class < extra_samples {
            samples_per_class + 1
        } else {
            samples_per_class
        };

        for _ in 0..n_samples_for_class {
            if current_idx < distances.len() {
                let sample_idx = distances[current_idx].1;
                y[sample_idx] = class as i32;
                current_idx += 1;
            }
        }
    }

    Ok((x, y))
}

pub fn make_hastie_10_2(
    n_samples: usize,
    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 normal = StandardNormal;
    let n_features = 10;

    // Generate feature matrix with standard normal distribution
    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);
        }
    }

    // Compute target using Hastie et al. formula:
    // y = 1 if sum(X_i^2) > 9.34, else 0
    // where 9.34 is chosen so that P(y=1) ≈ 0.5 for 10-dimensional standard normal
    let mut y = Array1::zeros(n_samples);
    let threshold = 9.34;

    for i in 0..n_samples {
        let mut sum_squares = 0.0;
        for j in 0..n_features {
            sum_squares += x[[i, j]] * x[[i, j]];
        }
        y[i] = if sum_squares > threshold { 1 } else { 0 };
    }

    Ok((x, y))
}

pub fn make_friedman1(
    n_samples: usize,
    n_features: usize,
    noise: f64,
    random_state: Option<u64>,
) -> Result<(Array2<f64>, Array1<f64>)> {
    if n_samples == 0 || n_features < 5 {
        return Err(SklearsError::InvalidInput(
            "n_samples must be positive and n_features must be >= 5".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 features uniformly between 0 and 1
    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.gen();
        }
    }

    // Compute target using Friedman's formula:
    // y = 10 * sin(π * x1 * x2) + 20 * (x3 - 0.5)² + 10 * x4 + 5 * x5 + noise
    let mut y = Array1::zeros(n_samples);
    for i in 0..n_samples {
        let x1 = x[[i, 0]];
        let x2 = x[[i, 1]];
        let x3 = x[[i, 2]];
        let x4 = x[[i, 3]];
        let x5 = x[[i, 4]];

        y[i] = 10.0 * (PI * x1 * x2).sin() + 20.0 * (x3 - 0.5).powi(2) + 10.0 * x4 + 5.0 * x5;

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

    Ok((x, y))
}

pub fn make_friedman2(
    n_samples: usize,
    noise: f64,
    random_state: Option<u64>,
) -> Result<(Array2<f64>, Array1<f64>)> {
    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())
    };

    // Generate features - Friedman2 uses 4 features
    let n_features = 4;
    let mut x = Array2::zeros((n_samples, n_features));

    for i in 0..n_samples {
        x[[i, 0]] = rng.gen_range(0.0..100.0); // x1: uniform [0, 100]
        x[[i, 1]] = rng.gen_range(40.0 * PI..560.0 * PI); // x2: uniform [40π, 560π]
        x[[i, 2]] = rng.gen(); // x3: uniform [0, 1]
        x[[i, 3]] = rng.gen_range(1.0..11.0); // x4: uniform [1, 11]
    }

    // Compute target using Friedman's formula:
    // y = (x1² + (x2 * x3 - 1/(x2 * x4))²)^0.5 + noise
    let mut y = Array1::zeros(n_samples);
    for i in 0..n_samples {
        let x1 = x[[i, 0]];
        let x2 = x[[i, 1]];
        let x3 = x[[i, 2]];
        let x4 = x[[i, 3]];

        let term1 = x1 * x1;
        let term2 = x2 * x3 - 1.0 / (x2 * x4);
        let term2_squared = term2 * term2;

        y[i] = (term1 + term2_squared).sqrt();

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

    Ok((x, y))
}

pub fn make_friedman3(
    n_samples: usize,
    noise: f64,
    random_state: Option<u64>,
) -> Result<(Array2<f64>, Array1<f64>)> {
    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())
    };

    // Generate features - Friedman3 uses 4 features
    let n_features = 4;
    let mut x = Array2::zeros((n_samples, n_features));

    for i in 0..n_samples {
        x[[i, 0]] = rng.gen_range(0.0..100.0); // x1: uniform [0, 100]
        x[[i, 1]] = rng.gen_range(40.0 * PI..560.0 * PI); // x2: uniform [40π, 560π]
        x[[i, 2]] = rng.gen(); // x3: uniform [0, 1]
        x[[i, 3]] = rng.gen_range(1.0..11.0); // x4: uniform [1, 11]
    }

    // Compute target using Friedman's formula:
    // y = atan((x2 * x3 - 1/(x2 * x4)) / x1) + noise
    let mut y = Array1::zeros(n_samples);
    for i in 0..n_samples {
        let x1 = x[[i, 0]];
        let x2 = x[[i, 1]];
        let x3 = x[[i, 2]];
        let x4 = x[[i, 3]];

        let numerator = x2 * x3 - 1.0 / (x2 * x4);
        let denominator = x1;

        y[i] = (numerator / denominator).atan();

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

    Ok((x, y))
}