-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Stratified sampling #33762
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
Closed
Stratified sampling #33762
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fb9cec6
initial commit
flaboss f6ea6bd
ENH: Added new method stratified sample
flaboss 153858b
ENH: Added docstrings to stratified sample function
flaboss bdbf267
ENH: Add test_stratified_sample.py
flaboss 6aa6359
ENH: Create stratified_sample method for DataFrame
flaboss 3b47523
ENH: Added methods for stratified sampling.
flaboss 766d2d1
ENH: Added method stratified_sample
flaboss 9a35384
ENH: Added test for stratified_sample
flaboss 191e4f6
ENH: Added stratified_sample method
flaboss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ Version 1.0 | |
.. toctree:: | ||
:maxdepth: 2 | ||
|
||
v1.0.4 | ||
v1.0.3 | ||
v1.0.2 | ||
v1.0.1 | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
.. _whatsnew_104: | ||
|
||
What's new in 1.0.4 (??) | ||
------------------------ | ||
|
||
These are the changes in pandas 1.0.4. See :ref:`release` for a full changelog | ||
including other versions of pandas. | ||
|
||
|
||
.. --------------------------------------------------------------------------- | ||
|
||
Enhancements | ||
~~~~~~~~~~~~ | ||
|
||
.. _whatsnew_104.stratified_sample: | ||
|
||
Stratified Sampling in Pandas | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
We've added :meth:`~DataFrame.stratified_sample` for sampling a DataFrame object using strata. | ||
|
||
A stratified sample is used when it is desired to keep the same proportions of the variables in the original population when sampling. | ||
|
||
For example: | ||
|
||
.. code-block:: ipython | ||
|
||
In [1]: df = pd.DataFrame({'gender': ['male', 'male', 'female', 'female', 'female', | ||
... 'female', 'female', 'female', 'male', 'male'], | ||
... 'age': [25, 26, 25, 26, 30, 25, 25, 30, 30, 25], | ||
... 'country': ['US', 'CAN', 'MEX', 'CAN', 'IN', 'CAN', | ||
... 'CAN', 'US', 'CAN', 'IN'], | ||
... 'income_K' : [100, 110, 99, 110, 110, 100, 100, 110, | ||
... 100, 99]} | ||
) | ||
In [2]: df.stratified_sample(strata=['age'], n=5, random_state=0, reset_index=True) | ||
Out[2]: | ||
gender age country income_K | ||
0 female 25 CAN 100 | ||
1 male 25 US 100 | ||
2 female 26 CAN 110 | ||
3 male 30 CAN 100 | ||
4 female 30 US 110 | ||
|
||
We've also added :meth:`~DataFrame.stratified_sample_counts` to return the counts that would be generated by a stratified sampling. | ||
|
||
.. code-block:: ipython | ||
|
||
In [1]: df = pd.DataFrame({'gender': ['male', 'male', 'female', 'female', 'female', | ||
... 'female', 'female', 'female', 'male', 'male'], | ||
... 'age': [25, 26, 25, 26, 30, 25, 25, 30, 30, 25], | ||
... 'country': ['US', 'CAN', 'MEX', 'CAN', 'IN', 'CAN', | ||
... 'CAN', 'US', 'CAN', 'IN'], | ||
... 'income_K' : [100, 110, 99, 110, 110, 100, 100, 110, | ||
... 100, 99]} | ||
) | ||
In [2]: df.stratified_sample_counts(strata=['age'], n=5) | ||
Out[2]: | ||
age size sample_size | ||
0 25 5 2 | ||
1 26 2 1 | ||
2 30 3 2 | ||
|
||
|
||
.. _whatsnew_104.contributors: | ||
|
||
Contributors | ||
~~~~~~~~~~~~ | ||
* Flavio Bossolan + |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this is a new feature, it would go in 1.1