DSTO-GAN: Balancing Data with GAN
DSTO-GAN is a Python library that uses a Generative Adversarial Network (GAN) to generate synthetic samples and balance imbalanced datasets. It is beneficial for classification problems where classes are disproportionate.
Features
- Generating synthetic samples for class balancing.
- Training a custom GAN for tabular data.
- Saving the balanced dataset to a
.csvfile.
Prerequisites
- Python 3.7 or higher.
pippackage manager.
Installation
You can install the library directly via pip:
pip install dsto-gan
Dependencies
The dependencies will be installed automatically during the installation. If you prefer to install manually, run:
pip install numpy torch pandas scikit-learn xgboost scikit-optimize
How to Use
1. Import and Initialization
First, import the DSTO_GAN class and initialize the object:
from dsto_gan import DSTO_GAN
Initialize DSTO-GAN
dsto_gan = DSTO_GAN(dim_h=64, n_z=10, lr=0.0002, epochs=100, batch_size=64)
2. Balancing Data
Use the fit_resample method to balance the data:
# Imbalanced data
X = ... # Features (numpy array or pandas DataFrame)
y = ... # Labels (numpy array or pandas Series)
# Balance the data
X_resampled, y_resampled = dsto_gan.fit_resample(X, y)
print(f"Shape of the balanced data: {X_resampled.shape}, {y_resampled.shape}")
3. Integration with Scikit-Learn
DSTO_GAN is compatible with Scikit-Learn pipelines. You can use it as part of a preprocessing pipeline:
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
# Split the data into training and testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a pipeline with DSTO-GAN and a classifier
pipeline = Pipeline([
('dsto_gan', DSTO_GAN()), # Balancing with DSTO-GAN
('classifier', RandomForestClassifier()) # Classifier
])
# Train the model
pipeline.fit(X_train, y_train)
# Evaluate the model
accuracy = pipeline.score(X_test, y_test)
print(f"Model accuracy: {accuracy:.2f}")
---
## Usage Example
Here is a complete usage example of DSTO-GAN:
```bash
import pandas as pd
from sklearn.model_selection import train_test_split
from dsto_gan import DSTO_GAN
# 1. Load imbalanced data
file_path = "path/to/imbalanced.csv"
df = pd.read_csv(file_path)
# 2. Separate features (X) and labels (y)
X = df.iloc[:, :-1].values # All columns except the last one
y = df.iloc[:, -1].values # Last column is the class
# 3. Split data into training and testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 4. Balance training data with DSTO-GAN
dsto_gan = DSTO_GAN()
X_train_resampled, y_train_resampled = dsto_gan.fit_resample(X_train, y_train)
print(f"Shape of balanced data: {X_train_resampled.shape}, {y_train_resampled.shape}")
Project Structure
dsto_gan/
│
├── dsto_gan/ # Main package
│ ├── __init__.py # Package initialization
│ ├── dsto_gan.py # Core code for data balancing
├── setup.py # Package configuration
├── README.md # Project documentation
└── LICENSE # Project license
Contribution
Contributions are welcome! If you encounter problems or have suggestions for improvements, feel free to open an issue or submit a pull request.
License
This project is licensed under the MIT License. See the [LICENSE] file for more details.
Contact
- Author: Erika Assis
- Email: dudabh@gmail.com
- Repository: GitHub
Acknowledgements
This project was developed as part of research on data balancing using GANs. The open-source community provided the libraries used.
Metadata
Release files for dsto-gan 0.1.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| dsto_gan-0.1.9.tar.gz | 6.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dsto_gan-0.1.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.4 kB
Release files / dsto_gan-0.1.9.tar.gz
| Download URL | dsto_gan-0.1.9.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
17644d6f95f4067c18d786d68dbbfda7b889b0bc0e7fa8a96794b9b603e29db6
|
|
BLAKE2b-256 checksum How to use checksums |
0e90eda3f94a05ce1906eb9ce6e8a00dee2e957c791c6fdcae98f139402c351c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Release files / dsto_gan-0.1.9-py3-none-any.whl
| Download URL | dsto_gan-0.1.9-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
77625a5aa5db03b533be22015232466a7635ad96faead69830ec51b493af6c22
|
|
BLAKE2b-256 checksum How to use checksums |
364b33cb8e8de450b5db47627a20c87cf8a635a36ba07f44c74b189d93ade64c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Security for dsto-gan 0.1.9
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.