auto-scaler that picks standard/minmax/robust scaling per column
Project description
SDScaler
Small auto-scaler for tabular data. Instead of manually picking StandardScaler vs MinMaxScaler vs RobustScaler for each column, this just looks at the column and picks one for you (checks for outliers and skew). You can also just force one method if you don't trust the auto part.
Made this for a uni project, kept it simple on purpose - no sklearn
dependency for the core logic (sklearn's only needed if you use
to_column_transformer()), just numpy and pandas.
Install
``` cd SDScaler pip install -e . ```
Usage
```python import pandas as pd from sdscaler import SDScaler
df = pd.DataFrame({ "age": [22, 25, 130, 28, 24], "income": [30000, 32000, 31000, 500000, 29000], })
scaler = SDScaler() scaled = scaler.fit_transform(df)
print(scaled) print(scaler.summary()) # which method got used per column ```
Force one method instead of auto:
```python scaler = SDScaler(method="standard") # or "minmax" / "robust" / "log" ```
Undo the scaling later:
```python scaler.inverse_transform(scaled) ```
Remember to fit only on your training data, then just .transform() the
test set with those same stats - don't fit_transform both separately or
you'll leak info / get mismatched scales.
```python train_scaled = scaler.fit_transform(train_df) test_scaled = scaler.transform(test_df) ```
How the auto part decides
For each numeric column:
- if more than 5% of points are outliers (1.5*IQR rule) -> robust
- else if it's pretty skewed (skew > 1) and strictly positive -> log
- else if it's pretty skewed but has zero/negative values -> minmax
- otherwise -> standard
Both cutoffs can be changed:
```python SDScaler(outlier_cutoff=0.1, skew_cutoff=1.5) ```
Heads up - on small datasets one weird value can be enough to trip the outlier check even if the column mostly looks normal, so don't be surprised if a small demo dataset gets more "robust" columns than you'd expect.
Forcing log scaling on specific columns
```python scaler = SDScaler(log_cols=["income"]) ```
This scales income with log regardless of what auto-pick would've chosen,
everything else still gets decided normally. Log scaling shifts the column
to be positive first if it isn't already, so it won't blow up on zeros or
negative values, but it's really meant for stuff that's naturally always
positive and skewed (income, population, prices, that kind of thing).
Non-numeric columns
If you pass a dataframe that still has a categorical/text column in it (like forgot to encode it, or it's a column you're not scaling on purpose), SDScaler just leaves it alone instead of erroring out. Only numeric columns get touched.
```python scaler.summary()
column method
0 age standard
1 city passthrough (not numeric)
```
Missing values
If a column has NaN in it, .fit() still works and warns you about which
columns have missing data. The stats (mean, median, etc.) get computed
ignoring the NaNs, but the scaled output will still have NaN in those exact
spots - didn't want to silently fill anything in without you knowing.
Using it with sklearn Pipelines
```python scaler = SDScaler().fit(df) ct = scaler.to_column_transformer() ```
Builds an sklearn ColumnTransformer using the same decisions SDScaler
already made, for anyone who'd rather have it inside a Pipeline instead of
calling .transform() directly. Non-numeric columns get passed through here
too. Needs scikit-learn installed, only used if you actually call this.
Files
``` sdscaler/ scaler.py - the SDScaler class helpers.py - skew/outlier detection logic examples/ example_usage.py tests/ test_scaler.py ```
Running tests
``` pip install pytest pytest tests/ ```
Todo / ideas
- frequency encoding style option for very high-cardinality numeric IDs
- maybe a plot function to compare before/after distributions
- log scaling on negative-heavy data currently just shifts everything, could be smarter about it for columns that are mostly negative
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sdscaler-0.2.0.tar.gz.
File metadata
- Download URL: sdscaler-0.2.0.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
218351347d7f0dadabd9dd5a3744bf423c269a50b0ec02179d718ce577d104dc
|
|
| MD5 |
29fd12f10150406ec71cbb23c67803ff
|
|
| BLAKE2b-256 |
f0d5752870be34abe6c5484e41cc115f87d4b06b1dac2f14f14ebf4593999751
|
File details
Details for the file sdscaler-0.2.0-py3-none-any.whl.
File metadata
- Download URL: sdscaler-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bad0735fe29779ea49cb54812a39beadffd7e3ba4afea9c6d308cb98a5533150
|
|
| MD5 |
f7ee9e0e1e636df0edfe9683212dd809
|
|
| BLAKE2b-256 |
4b38cf44564056f17fb7cd6b1cd62e20ba675daf8768e37b25291795e5d98cef
|