Skip to main content

Library for balancing tabular data with Deep SMOTE Optimize.

Project description

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

  1. Generating synthetic samples for class balancing.
  2. Training a custom GAN for tabular data.
  3. Saving the balanced dataset to a .csv file.

Prerequisites

  • Python 3.7 or higher.
  • pip package 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


Acknowledgements

This project was developed as part of research on data balancing using GANs. The open-source community provided the libraries used.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dsto_gan-0.1.9.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dsto_gan-0.1.9-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file dsto_gan-0.1.9.tar.gz.

File metadata

  • Download URL: dsto_gan-0.1.9.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for dsto_gan-0.1.9.tar.gz
Algorithm Hash digest
SHA256 17644d6f95f4067c18d786d68dbbfda7b889b0bc0e7fa8a96794b9b603e29db6
MD5 ff998f57f626147a05498294370a32a5
BLAKE2b-256 0e90eda3f94a05ce1906eb9ce6e8a00dee2e957c791c6fdcae98f139402c351c

See more details on using hashes here.

File details

Details for the file dsto_gan-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: dsto_gan-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for dsto_gan-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 77625a5aa5db03b533be22015232466a7635ad96faead69830ec51b493af6c22
MD5 2e309f038b5df0e6db6bd3b611afffed
BLAKE2b-256 364b33cb8e8de450b5db47627a20c87cf8a635a36ba07f44c74b189d93ade64c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page