Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gen tabular datasets #260

Merged
merged 14 commits into from
Nov 5, 2022
Merged

Gen tabular datasets #260

merged 14 commits into from
Nov 5, 2022

Conversation

oojo12
Copy link
Contributor

@oojo12 oojo12 commented Nov 2, 2022

Goal:

This PR aims to make writing benchmarking code a bit easier for developers by parametrizing the datasets dimension.

Additions to linfa-datasets:

  1. A function to allow generation of fictitious tabular datasets.
  2. Two optional dependencies (statrs and rand)

resolves #259

Example

use ndarray::Array;
use linfa::Dataset;
use ndarray_rand::RandomExt;
use statrs::distribution::{Binomial, DiscreteUniform};
use rand::distributions::Distribution as randDistribution;

fn main() {
    let feat_distr = Binomial::new(0.5, 5 ).unwrap();
    let target_distr = DiscreteUniform::new(0, 5).unwrap();
    let data = make_dataset(5, 5, 2, feat_distr, target_distr);
    
    println!("{:#?}", data);
}

fn make_dataset<X, Y>(num_rows: usize, num_feats: usize, num_targets: usize, feat_distr: X, target_distr: Y) -> Dataset<f64, f64>
where
    X: randDistribution<f64>,
    Y: randDistribution<f64>
{
    let features = Array::random((num_rows, num_feats), feat_distr);
    let targets = Array::random((num_rows, num_targets), target_distr);

    println!("{:#?}", features);
    println!("{:#?}", targets);

    Dataset::new(features, targets)
}

Associated output

[[1.0, 2.0, 0.0, 3.0, 2.0],
 [3.0, 1.0, 2.0, 4.0, 2.0],
 [2.0, 3.0, 5.0, 2.0, 2.0],
 [2.0, 1.0, 4.0, 2.0, 2.0],
 [2.0, 4.0, 3.0, 1.0, 3.0]], shape=[5, 5], strides=[5, 1], layout=Cc (0x5), const ndim=2
[[3.0, 4.0],
 [5.0, 3.0],
 [5.0, 5.0],
 [2.0, 1.0],
 [2.0, 5.0]], shape=[5, 2], strides=[2, 1], layout=Cc (0x5), const ndim=2
DatasetBase {
    records: [[1.0, 2.0, 0.0, 3.0, 2.0],
     [3.0, 1.0, 2.0, 4.0, 2.0],
     [2.0, 3.0, 5.0, 2.0, 2.0],
     [2.0, 1.0, 4.0, 2.0, 2.0],
     [2.0, 4.0, 3.0, 1.0, 3.0]], shape=[5, 5], strides=[5, 1], layout=Cc (0x5), const ndim=2,
    targets: [[3.0, 4.0],
     [5.0, 3.0],
     [5.0, 5.0],
     [2.0, 1.0],
     [2.0, 5.0]], shape=[5, 2], strides=[2, 1], layout=Cc (0x5), const ndim=2,
    weights: [], shape=[0], strides=[0], layout=CFcf (0xf), const ndim=1,
    feature_names: [],
}

the feature array can look like this with a Laplace distribution so we are able to handle both continuous and discrete cases

[[-4.360595740165293, 8.155513405816892, -8.478208753936332, 17.23637839788624, 1.7384987675286867],
 [2.6468478875196775, -0.0055614415309673815, 3.6241064859319967, 5.072362710780806, 7.617604811173227],
 [0.79789581354939, -1.4199269716778213, 0.18709110069858814, 1.4115467400849417, -0.8056722910319407],
 [-11.46311409107569, 3.2708729527840843, 4.16040511713887, 1.6879416430935898, 2.451106903259817],
 [-2.7718365458998515, 1.941470709966388, 14.061929511357462, 5.111381148046025, 0.7341567591021234]], shape=[5, 5], strides=[5, 1], layout=Cc (0x5), const ndim=2

Adjustments to replicate with the above example

use statrs::distribution::{Binomial, DiscreteUniform, Laplace};
let feat_distr = Laplace::new(0.5, 5. ).unwrap();

Outstanding Questions

  1. Should an example be added to an "examples" directory?
  2. How do we go about documenting?

@codecov-commenter
Copy link

codecov-commenter commented Nov 2, 2022

Codecov Report

Base: 39.17% // Head: 38.68% // Decreases project coverage by -0.48% ⚠️

Coverage data is based on head (2829ed2) compared to base (1f260de).
Patch coverage: 0.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #260      +/-   ##
==========================================
- Coverage   39.17%   38.68%   -0.49%     
==========================================
  Files          92       92              
  Lines        6096     6080      -16     
==========================================
- Hits         2388     2352      -36     
- Misses       3708     3728      +20     
Impacted Files Coverage Δ
datasets/src/generate.rs 60.00% <0.00%> (-25.72%) ⬇️
algorithms/linfa-linear/src/float.rs 20.00% <0.00%> (-13.34%) ⬇️
algorithms/linfa-nn/src/heap_elem.rs 0.00% <0.00%> (-10.00%) ⬇️
algorithms/linfa-logistic/src/argmin_param.rs 41.17% <0.00%> (-8.83%) ⬇️
...rithms/linfa-clustering/src/k_means/hyperparams.rs 27.08% <0.00%> (-6.25%) ⬇️
...linfa-clustering/src/appx_dbscan/cells_grid/mod.rs 46.42% <0.00%> (-6.21%) ⬇️
...orithms/linfa-clustering/src/optics/hyperparams.rs 20.58% <0.00%> (-5.89%) ⬇️
algorithms/linfa-linear/src/glm/distribution.rs 54.90% <0.00%> (-5.89%) ⬇️
algorithms/linfa-svm/src/classification.rs 42.10% <0.00%> (-5.27%) ⬇️
src/composing/platt_scaling.rs 30.43% <0.00%> (-5.22%) ⬇️
... and 52 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

datasets/Cargo.toml Outdated Show resolved Hide resolved
datasets/Cargo.toml Outdated Show resolved Hide resolved
@YuhanLiin
Copy link
Collaborator

For documentation just describe what the function generates. Something like: "This function generates a random, multi-target dataset with the specified number of rows, features, and targets. The values of the features and targets are determined using the provided distributions". You can just put the example into the function docs as a doctest.

@oojo12
Copy link
Contributor Author

oojo12 commented Nov 3, 2022

cool I'll do that tomorrow.

datasets/src/generate.rs Outdated Show resolved Hide resolved
@oojo12 oojo12 marked this pull request as draft November 5, 2022 17:11
@oojo12 oojo12 marked this pull request as ready for review November 5, 2022 19:21
@YuhanLiin YuhanLiin merged commit 213f60d into rust-ml:master Nov 5, 2022
@oojo12 oojo12 deleted the gen-tabular-datasets branch November 9, 2022 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tabular Data Generating Func for Benchmarks?
3 participants