Steven
Steven (Sample Things EVENly) helps you sample your data in nice easy ways, evenly across the range of the data!
Steven is available on PyPI: pip install steven.
How to use Steven
The main method of steven is sample_data_evenly. This takes as input a sequence-liked object such as a list, tuple, np.ndarray or pd.Series, and samples it in such a way that the items returned represent a balanced distribution across the data range.
This is useful for balancing both continuous and discrete data for machine learning applications, among other things!
Let's set up an example and plot the distribution.
import numpy as np
import matplotlib.pyplot as plt
from steven.sampling import sample_data_evenly
# Seed for reproducibility
seed = 8675309
np.random.seed(seed)
# Create some data...
data = np.exp(np.random.rand(100_000))
plt.hist(data, bins=50, range=[data.min(), data.max()], label='All data')
# Now sample the data...
data_sampled = sample_data_evenly(data, n_bins=50, sample_size=20_000, random_state=seed)
plt.hist(data_sampled, bins=50, range=[data.min(), data.max()], label='Sampled')
plt.title('Sample data evenly, example')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.legend()
plt.show()
The result should look like this:
Keeping track of sampled indices
Optionally, subset_data_evenly accepts a return_ixs argument, which allows us to keep track of which indexes have been sampled from the inputted data. Continuing with the above example, we can do:
sampled_data, ixs = subset_data_evenly(data, n_bins=50, sample_size=10, random_state=seed, return_ixs=True)
This will return the sampled data and the ixs as a tuple:
>>> sampled_data, ixs
(array([2.29744662, 1.56124329, 1.75257412, 1.39012692, 1.04761057,
1.32016874, 1.98088368, 1.84552982, 2.6627304 , 1.5303134 ]),
[49023, 44730, 83142, 98395, 37441, 81177, 9769, 38017, 3088, 59028])
Metadata
Release files for steven 0.3.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| steven-0.3.3.tar.gz | 8.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| steven-0.3.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.3 kB
Release files / steven-0.3.3.tar.gz
| Download URL | steven-0.3.3.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
14dcf15dde6fa30c7eccea8a9d883113b967ec097b4f01dfc54607eb6781556d
|
|
BLAKE2b-256 checksum How to use checksums |
5778e9db86c227eab5ec214a1c6bc243ea942380e6eb178620551e90e6d497d8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / steven-0.3.3-py3-none-any.whl
| Download URL | steven-0.3.3-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b9f17afb1026b8c3c795cff05609d26d407e82dd9c2ec22e9ad16b31cdb9e5ab
|
|
BLAKE2b-256 checksum How to use checksums |
40181941e5395457c874dd566019640140e44c4e27df268ce3ae76567bc418c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|