Skip to main content

GANFS: GAN-based Feature Selection for Machine Learning

Project description

GANFS: GAN-Based Feature Selection

PyPI version Python 3.8+ License: MIT

A Python library for feature selection using Generative Adversarial Networks. GANFS trains a GAN on your data and uses perturbation-based sensitivity analysis on the discriminator to rank and select the most important features.

Installation

Install via PyPI (Recommended):

pip install ganfs

Install from GitHub (Latest Development Version):

pip install git+https://github.com/patelharsh15/GANFS-GAN-based-feature-selection.git

From source:

git clone https://github.com/patelharsh15/GANFS-GAN-based-feature-selection.git
cd GANFS-GAN-based-feature-selection
pip install -e .

Quick Start

from ganfs import GANFS
import pandas as pd

# Load your dataset
df = pd.read_csv("my_data.csv")
X = df.drop("label", axis=1)
y = df["label"]

# Initialize and train GANFS
selector = GANFS(epochs=200, batch_size=4096)
selector.fit(X, y)

# View feature ranking
ranking = selector.get_feature_ranking()
print(ranking)

# Select top 20 features
X_selected = selector.transform(X, k=20)

# Save/load trained models
selector.save("my_ganfs_model")
loaded = GANFS.load("my_ganfs_model")

API Reference

GANFS Class

Constructor Parameters

Parameter Type Default Description
epochs int 500 Number of GAN training epochs
batch_size int 4096 Batch size for GAN training
learning_rate float 0.001 Adam optimizer learning rate
label_smoothing tuple (0.9, 0.1) Label smoothing for (real, fake)
perturbation_mode str 'dynamic' 'dynamic' or 'static' perturbation scaling
perturbation_factors list [0.5, 1.0, 2.0, 5.0, 10.0] Perturbation multipliers
checkpoint_dir str/None None Directory for training checkpoints
verbose bool True Print progress information
random_state int/None None Random seed for reproducibility
patience int/None None Early stopping patience (epochs to wait)
generator_hidden_layers tuple (64, 128) Generator Dense layer architecture
discriminator_hidden_layers tuple (128, 64) Discriminator Dense layer architecture

Methods

Method Description
fit(X, y) Train GAN and compute feature sensitivities
transform(X, k) Select top-K features from X
fit_transform(X, y, k) Fit and transform in one step
get_feature_ranking() Get DataFrame of features ranked by sensitivity
get_feature_pairs_from_data(X, top_n) Analyze synergistic feature pair interactions
save(path) Save trained model to disk
GANFS.load(path) Load a saved model from disk

Usage with scikit-learn

from ganfs import GANFS
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# Feature selection
selector = GANFS(epochs=200)
selector.fit(X_train, y_train)
X_train_selected = selector.transform(X_train, k=20)
X_test_selected = selector.transform(X_test, k=20)

# Downstream classification
clf = RandomForestClassifier()
clf.fit(X_train_selected, y_train)
accuracy = accuracy_score(y_test, clf.predict(X_test_selected))
print(f"Accuracy with top-20 GANFS features: {accuracy:.4f}")

How It Works

  1. GAN Training — A Generator-Discriminator pair is trained on the feature data. The Generator learns to produce realistic synthetic samples, while the Discriminator learns to distinguish real from fake.

  2. Sensitivity Analysis — After training, each feature is perturbed (using dynamic perturbation magnitudes scaled to each feature's natural granularity) and the discriminator's response is measured. Features that cause the largest output changes are the most discriminative.

  3. Feature Ranking — Features are ranked by their average sensitivity scores across multiple perturbation levels and directions.

  4. Feature Selection — The top-K features can be selected for downstream tasks (classification, regression, etc.).

Project Structure

├── ganfs/                           # Python package source code
│   ├── __init__.py                  # Public API
│   ├── ganfs.py                     # Main GANFS class
│   ├── models.py                    # Generator & Discriminator networks
│   ├── sensitivity.py               # Sensitivity analysis functions
│   └── utils.py                     # GPU setup & preprocessing utilities
├── pyproject.toml                   # Package build configuration
└── research_archive/                # Original research files and experiments
    ├── GAN Algo Final.ipynb         # Original research notebook
    ├── benchmarking.ipynb           # Benchmarking vs traditional methods
    ├── training_checkpoints/        # Saved model checkpoints
    ├── feature_pair_interactions.csv
    └── feature_sensitivity_results.csv

Dataset Setup (for reproducing research results)

The original research uses the CIC-DDoS2019 dataset. The dataset files are too large (~12 GB) to host on GitHub.

Download Instructions

  1. Visit the CIC-DDoS2019 dataset page
  2. Request access and download the following CSV files:
    • DrDoS_DNS.csv, DrDoS_LDAP.csv, DrDoS_MSSQL.csv, DrDoS_NTP.csv
    • DrDoS_NetBIOS.csv, DrDoS_SNMP.csv, DrDoS_SSDP.csv, DrDoS_UDP.csv
  3. Place all files in a CIC-DDoS2019/ folder at the repository root
  4. Update the base_path in the notebook to "./CIC-DDoS2019/"

Requirements

  • Python 3.8+
  • TensorFlow 2.x (GPU support recommended)
  • NumPy, Pandas, scikit-learn

Acknowledgments

This project uses the CIC-DDoS2019 dataset provided by the Canadian Institute for Cybersecurity, University of New Brunswick.

If you use the dataset, please cite the original authors:

Iman Sharafaldin, Arash Habibi Lashkari, Saqib Hakak, and Ali A. Ghorbani,
"Developing Realistic Distributed Denial of Service (DDoS) Attack Dataset and Taxonomy",
IEEE 53rd International Carnahan Conference on Security Technology, Chennai, India, 2019.

License

MIT License — see LICENSE for details.

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

ganfs-0.2.0.tar.gz (19.0 kB view details)

Uploaded Source

Built Distribution

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

ganfs-0.2.0-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file ganfs-0.2.0.tar.gz.

File metadata

  • Download URL: ganfs-0.2.0.tar.gz
  • Upload date:
  • Size: 19.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for ganfs-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8c1ff23a96e177d9071c3be445cba003dc30fb962c6e8f0db8d69d1d380073d0
MD5 8665024dc303db516773cf54af5de406
BLAKE2b-256 d1bedd748613a26bcc8ade7f715361cf9694cdcc9100ed699ef357bc85aad71d

See more details on using hashes here.

Provenance

The following attestation bundles were made for ganfs-0.2.0.tar.gz:

Publisher: publish.yml on patelharsh15/GANFS-GAN-based-feature-selection

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ganfs-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: ganfs-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for ganfs-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 548d0916972212ca636ef4793430e9d1089d5af16c17c77029ff90fb07f5ba71
MD5 f3b492634c53a40a268b955c60e19116
BLAKE2b-256 e708206a8a7eecc98c6ff8ebd426a2de99b731ef1bfbb1ee739799f3b3960c61

See more details on using hashes here.

Provenance

The following attestation bundles were made for ganfs-0.2.0-py3-none-any.whl:

Publisher: publish.yml on patelharsh15/GANFS-GAN-based-feature-selection

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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