json - Javascript - Stratified Sampling -
json - Javascript - Stratified Sampling -
i have json info source this:
var ds=[{"id":1,"group":"a"},{"id":2,"group":"c"},{"id":3,"group":"b"},{"id":4,"group":"a"},{"id":5,"group":"c"},{"id":6,"group":"b"},{"id":7,"group":"a"},{"id":8,"group":"c"},{"id":9,"group":"b"},{"id":10,"group":"a"},{"id":11,"group":"c"}];
suppose every grouping has @ to the lowest degree m records(here m=3),i randomly pick n(n<=m) records each grouping , merge samples new array this:
var output=[{"id":1,"group":"a"},{"id":7,"group":"a"},{"id":3,"group":"b"},{"id":6,"group":"b"},{id":2,"group":"c",{"id":11,"group":"c"}]
any algorithm case?
yeah, can pretty cleanly lodash:
var output = _(ds) //begin chaining syntax .groupby("group") //split groups .map(function(group) { //for each grouping homecoming _.sample(group, n); //sample n items randomly }) .flatten() //flatten array of arrays single array .value(); //end chaining syntax
javascript json underscore.js lodash
Comments
Post a Comment