feat(datasets): oversample and weighted resampling strategies#32
Merged
Conversation
sample_oversample() duplicates positives to hit a target neg:pos ratio.
sample_weighted() does class-proportional resampling w/ replacement.
Both are exported from pyhealth.datasets and wired into the e2e script
via --sampling-strategy {undersample,oversample,weighted}.
Full-scale results show all three strategies underperform the unbalanced
baseline at MIMIC-IV full scale (see results.md). Resampling is most
useful at small dataset sizes or w/ a frozen encoder.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
will-pang
approved these changes
May 28, 2026
will-pang
left a comment
There was a problem hiding this comment.
looks good - will test a bit more later
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR: Additional Sampling Strategies for Class-Imbalanced EHR Data
Problem
MIMIC-IV in-hospital mortality is heavily imbalanced (~10% positive rate). PR #28 added
sample_balanced()(undersampling) as the first strategy. Two additional strategies are worth comparing:Note:
WeightedRandomSampler(PyTorch) is incompatible w/litdata.StreamingDataset(IterableDataset). Theweightedstrategy uses dataset-level resampling viasample_weighted()instead of an external sampler.Changes
pyhealth/datasets/splitter.pysample_oversample(dataset, ratio=1.0, seed=None, label_key="label")n_pos ~= n_neg / ratio.patient_to_indexandrecord_to_indexon the oversampled result.sample_balanced()API exactly.sample_weighted(dataset, seed=None, label_key="label")p[i] = 1 / class_count[label[i]]to each sample, normalized to a probability distribution.SampleDataset.subset()of the same length as the original.ValueErrorif a class is missing.pyhealth/datasets/__init__.pyExports:
sample_oversample,sample_weighted.examples/mortality_prediction/unified_embedding_e2e_mimic4.py--sampling-strategy {none, undersample, oversample, weighted}(default:none)--balanced-samplingremains as a legacy alias. If set and--sampling-strategyis stillnone, the strategy is promoted toundersample.--balanced-ratioapplies to bothundersampleandoversample(negatives per positive).scripts/condor/run_e2e_full_scale.shTwo new condition cases added:
oversampled->--sampling-strategy oversample --balanced-ratio 1.0weighted_sampler->--sampling-strategy weightedscripts/condor/batch_e2e_smoke.subAdds
oversampledandweighted_samplersmoke jobs (MLP, seed=42, 5 epochs, dev=1000). Total smoke jobs: 4 -> 9.Full-Scale Results (full MIMIC-IV, 50 epochs, seed=42, A100 80GB)
Actual ranking:
none >> oversampled > weighted > undersampleThis is the reverse of the original hypothesis. Why:
balancedundersamples to ~14k samples, a 9x size reduction. The model sees far fewer negatives overall.oversampleduplicates positives ~10x from ~12k to ~120k. W/ frozen BERT, all duplicated positives have identical embeddings. The model memorizes a small set of fixed vectors rather than generalizing. Transformer peaks early (ep3) and stagnates.weighted_sampler(dataset-level resampling) is functionally identical to oversample in expectation, w/ a similar ceiling.pos_weight=10baseline trains on the full negative distribution w/ a scalar gradient correction and continues improving through ep28.Takeaway: for MIMIC-IV mortality at full scale,
pos_weightcorrection w/o resampling outperforms all three resampling strategies. Resampling may be more beneficial at smaller dataset sizes or w/ non-frozen encoders.Files Changed
pyhealth/datasets/splitter.pysample_oversample, +sample_weighted(replacesmake_weighted_sampler)pyhealth/datasets/__init__.pyexamples/mortality_prediction/unified_embedding_e2e_mimic4.py--sampling-strategyflag, routing logicscripts/condor/run_e2e_full_scale.shoversampled+weighted_samplercondition casesscripts/condor/batch_e2e_smoke.subscripts/condor/batch_e2e_weighted_sampler.sub