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.8.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.8-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dsto_gan-0.1.8.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.8.tar.gz
Algorithm Hash digest
SHA256 4d3155cfe5116ba6381879e5806ea0bb30d9a57a5543e249d097bee80bed5017
MD5 34b2be2dce45a1506ab76a3734579906
BLAKE2b-256 fdb3139a94d19d7f7f357658e7c63daa37d93e705f096ba3146ec42af1636d3b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dsto_gan-0.1.8-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.8-py3-none-any.whl
Algorithm Hash digest
SHA256 efc208d485f356fbb55295dd36f68c5d029c1dae92bb90428d7fea07c7cc497d
MD5 25c5a4847de286919e4a0df147e0b606
BLAKE2b-256 855b7f43d59c65e8bd5f43e793b45602eb29eaa9d34ba3755411e076774fde9a

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