Skip to main content

PyLAFS — Learning Attention-based Feature Selection: a PyTorch-powered, scikit-learn compatible feature selector using trainable attention.

Project description

PyLAFS — Learning Attention-based Feature Selection

A PyTorch-powered, scikit-learn compatible feature selection method that uses a trainable attention mechanism to rank and select the most informative features for classification tasks.

Installation

pip install pylafs

Or in development/editable mode:

pip install -e .

Quick Start

from pylafs import LAFSSelector
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import pandas as pd

# Generate sample data
X, y = make_classification(n_samples=1000, n_features=50,
                           n_informative=10, random_state=42)
X = pd.DataFrame(X, columns=[f"f_{i}" for i in range(50)])
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Scale features
scaler = StandardScaler()
X_train = pd.DataFrame(scaler.fit_transform(X_train), columns=X.columns)
X_test  = pd.DataFrame(scaler.transform(X_test), columns=X.columns)

# Select top-10 features with LAFS
selector = LAFSSelector(k=10, epochs=50, lr=0.001, lambda_reg=0.01)
selector.fit(X_train, y_train)

X_train_selected = selector.transform(X_train)
X_test_selected  = selector.transform(X_test)

print("Selected features:", selector.selected_features_)
print("Feature importances:", selector.feature_importances_)

API Reference

LAFSSelector

Parameter Type Default Description
k int 10 Number of features to select
lr float 0.001 Adam learning rate
lambda_reg float 0.01 Entropy regularisation coefficient
epochs int 50 Training epochs
batch_size int 256 Mini-batch size
attn_hidden_size int 128 Attention network hidden layer width
attn_dropout_rate float 0.5 Dropout rate in attention network
cls_hidden_size int 128 Classifier hidden layer width
weight_decay float 1e-4 L2 regularisation for Adam
device str/None None "cuda", "cpu", or auto-detect
random_state int/None None Seed for reproducibility
verbose bool False Print training progress

Methods

Method Description
fit(X, y) Train the LAFS model and compute feature importances
transform(X) Reduce X to the selected features
fit_transform(X, y) Fit and transform in one step
get_support(indices=False) Boolean mask or indices of selected features
get_feature_names_out() Names of the selected features

Attributes (available after fit)

Attribute Description
feature_importances_ Mean attention weight per feature (numpy array)
ranking_ Feature indices sorted by importance (descending)
selected_features_ List of selected feature names
selected_indices_ Array of selected feature indices
model_ The trained PyTorch LAFSModel

How It Works

LAFS jointly trains two neural networks:

  1. AttnNet — produces per-feature softmax attention weights
  2. ClassNet — a feedforward classifier that operates on attention-weighted features

The loss function combines cross-entropy classification loss with an entropy regularisation term on the attention weights. This encourages the attention to focus on the most discriminative features.

After training, global feature importance is computed as the mean attention weight across all training samples. The top-k features by importance are selected.

License

MIT

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

pylafs-0.1.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

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

pylafs-0.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file pylafs-0.1.0.tar.gz.

File metadata

  • Download URL: pylafs-0.1.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for pylafs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7a28f4bdf1810572a40c348b8e9b7d92a8c62198adc4dc9d953ef99c15565b17
MD5 0feb0ca785019cc69510d51b5b7539ff
BLAKE2b-256 c147f2229cf12609c0e60e7301d457668e020d237495a029c226f18ace74bc29

See more details on using hashes here.

File details

Details for the file pylafs-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pylafs-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for pylafs-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c623b2b1ac2def17869cc1cb406d1e50fa381bb8888f7923d2956270149efec5
MD5 bd45fd2ce157b6d80f6aa7e53fa01043
BLAKE2b-256 8ce1c5ab1cee6f6cde12f20ddfae704aef8f6cab7e38ee633a09e0960b0153da

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