Skip to content

Reproducibility in train_test_apart_stratify() #41

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

Closed
stephengmatthews opened this issue Aug 16, 2024 · 1 comment
Closed

Reproducibility in train_test_apart_stratify() #41

stephengmatthews opened this issue Aug 16, 2024 · 1 comment

Comments

@stephengmatthews
Copy link

train_test_apart_stratify() produces different results for the same input data, even when setting random_state=0.

To reproduce this, I've adapted the example from the function's docstring to contain only strings (i.e., the values for a are now str instead of int). Run this several times to see different results.

import pandas
from pandas_streaming.df import train_test_apart_stratify

df = pandas.DataFrame([dict(a="1", b="e"),
                       dict(a="1", b="f"),
                       dict(a="2", b="e"),
                       dict(a="2", b="f")])

train, test = train_test_apart_stratify(
    df, group="a", stratify="b", test_size=0.5)
print(train)
print('-----------')
print(test)

The cause seems to be the sets created in connex_split.py#L530 are then iterated over in connex_split.py#L543 but a set is an unordered collection. Replacing ids[k] with sorted(ids[k]) on L543 seems to fix this.

@stephengmatthews
Copy link
Author

Resolved in PR #43. There's now a sorted_indices flag.

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

No branches or pull requests

1 participant