Fix randomness issue in spark_stratified_split() #1654
Merged
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.
Description
Bhrigu reported this issue when he used spark_stratified_split() to split data into
train
andtest
. He found when the data were big enough there were duplicate data intrain
andtest
which should not be there. Below is a Databricks notebook source file that shows a simple example to reproduce the issue.Because Spark distributes data with partitions across the nodes in the cluster,
F.rand()
used in the code may be invoked several times in parallel on different partitions over the window defined in the code, which is non-deterministic even the seed is set.This PR moves the random number generation to the level of the entire DataFrame instead of within each window to avoid the random invocation of
F.rand()
.Related Issues
Checklist:
staging branch
and not tomain branch
.