×
BigML is working hard to support a wide range of browsers. Your experience will be better with:
English
Sign up / in
Sign up
Sign in
or Sign in with...
Amazon
Github
GitLab
Google
Toggle navigation
PRODUCT
Features
Releases
What's New
GETTING STARTED
PRICING
Subscriptions
Private Deployments
Training
Certifications
BigML for Education
SUPPORT
Toggle navigation
PRODUCT
Features
Releases
What's New
GETTING STARTED
PRICING
Subscriptions
Private Deployments
Training
Certifications
BigML for Education
SUPPORT
License
Oops!
We could not process your request!
Stratified sampling | BigML.com
×
Embed this Script in your web site
Copy & Paste this code into your HTML code:
whizzml
Public
whizzml's
Scripts
77
Models
0
Datasets
0
0
FREE
BUY
Stratified Sampling
Details:
Created
Thu, 8 Oct 2026 15:03:01 +0000
Published
Thu, 8 Oct 2026 17:25:48 +0000
Description:
An implementation of stratified sampling
Tags:
BigMLer
BigMLer_ThuOct0826_170301
URL:
https://bigml.com/user/whizzml/gallery/script/6ac7b0a58d0970b175487f01
Script
FREE
Source code
;; Takes a dataset id, a categorical field id, and a list of the ;; categories in that field, and breaks the dataset into pieces, one ;; per category. Returns the list of datasets. (define (split-it ds-id fld-id bucket-list) (iterate (ds-list [] bucket bucket-list) (let (split-filter (flatline "(= (f {{fld-id}}) {{bucket}})") split-ds (create-and-wait-dataset ds-id {"lisp_filter" split-filter})) (append ds-list split-ds)))) ;; Takes a list of dataset ids and a list of counts, and randomly ;; samples each dataset at the count indicated. Returns the list of ;; sampled dataset ids. (define (sample-it split-list sample-list) (iterate (ds-list [] ds split-list sample-size sample-list) (let (ds-size ((fetch (wait ds)) "rows")) (append ds-list (create-dataset ds {"sample_rate" (min (/ sample-size ds-size) 1.0 )}))))) ;; Given a dataset-id, a categorical field-id, and a map of weights, computes ;; the relative counts from the class distribution (define (weighted-counts ds-id field-id weights) (let (dists (field-distribution (find-field (resource-fields ds-id) field-id)) class-values (keys weights) smallest-factor (min (iterate (ratios [] dist dists) (append ratios (floor (/ (dist 1) (weights (dist 0)))))))) (iterate (counts [] class-val class-values) (append counts (* smallest-factor (weights class-val)))))) ;; Putting it together, takes in a dataset, a field id/name, and a map of ;; field values to sample counts. Breaks the dataset into pieces by ;; field value, samples them according to the sample counts, and puts ;; them back together into a single returned dataset. Deletes the ;; intermediate datasets. (define (stratified-sampling ds-id field sample-map weighted) (let (field-id (get (find-field (resource-fields ds-id) field) "id") field-values (keys sample-map) sample-count (if weighted (weighted-counts ds-id field-id sample-map) (values sample-map)) split-list (split-it ds-id field-id field-values) sample-list (sample-it split-list sample-count) collected (create-and-wait-dataset {"origin_datasets" sample-list})) (delete* split-list) (delete* sample-list) collected)) (define stratified-dataset (stratified-sampling dataset field counts weighted))
Description
An implementation of stratified sampling
An implementation of stratified sampling
Show more
Inputs
Outputs
License:
Creative Commons Zero V1.0
Script
FREE
Stratified sampling | BigML.com
Sending Request...
Sending Request...