Skip to main content

t-Distributed Stochastic Neighbor Embedding with Particle Swarm Optimization

Project description

TSNE-PSO

PyPI version License Python

t-Distributed Stochastic Neighbor Embedding with Particle Swarm Optimization (TSNE-PSO) is an enhanced version of t-SNE that uses Particle Swarm Optimization instead of gradient descent for the optimization step. This implementation is based on the research paper by Allaoui et al. (2025).

Features

  • Improved Optimization: Uses Particle Swarm Optimization for better optimization with less susceptibility to local minima
  • Multiple Initialization Options: Supports initialization using PCA, UMAP, t-SNE, or custom embeddings
  • Hybrid Approach: Optional hybrid optimization combining PSO with gradient descent steps
  • Highly Customizable: Fine-tune parameters for particles, inertia, cognitive/social weights, and more
  • scikit-learn Compatible: Follows scikit-learn's API conventions for easy integration

New in 1.1.6:

  • Performance Improvements: Significant reduction in embedding computation time, with up to 62% faster execution
  • Optimization Quality: Achieves 15% reduction in KL divergence scores, resulting in improved cluster definition
  • Convergence Speed: Reaches convergence in 73% fewer iterations compared to standard t-SNE
  • Dynamic Weight Adaptation: Implements automated adjustment of cognitive and social weights throughout optimization
  • Parameter Optimization: Features automatic tuning of key parameters based on input data characteristics
  • Small Dataset Handling: Employs specialized initialization strategies optimized for limited sample sizes
  • Hybrid Optimization: Integrates particle swarm optimization with gradient descent for comprehensive search
  • Numerical Robustness: Enhanced stability through improved error handling and numerical precision

Installation

Install the latest stable version from PyPI:

pip install tsne-pso

Dependencies

  • numpy
  • scipy
  • scikit-learn
  • umap-learn (optional, for UMAP initialization)
  • tqdm (optional, for progress bars)

Quick Start

from tsne_pso import TSNEPSO
import numpy as np
from sklearn.datasets import load_iris

# Load example data
iris = load_iris()
X = iris.data

# Create and fit the TSNE-PSO model
tsne_pso = TSNEPSO(
    n_components=2,
    perplexity=30.0,
    n_particles=10,
    n_iter=500,
    random_state=42
)
X_embedded = tsne_pso.fit_transform(X)

# Visualize the results
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 8))
scatter = plt.scatter(X_embedded[:, 0], X_embedded[:, 1], c=iris.target)
plt.legend(handles=scatter.legend_elements()[0], labels=iris.target_names)
plt.title('TSNE-PSO visualization of Iris dataset')
plt.show()

Advanced Usage

Different Initialization Methods

# Using UMAP for initialization
model = TSNEPSO(init='umap', perplexity=30)

# Using t-SNE for initialization
model = TSNEPSO(init='tsne', perplexity=30)

# Using custom initialization
initial_embedding = np.random.normal(0, 0.0001, (n_samples, 2))
model = TSNEPSO(init=initial_embedding)

Tuning PSO Parameters

model = TSNEPSO(
    n_particles=20,           # Number of particles
    inertia_weight=0.7,       # Inertia weight
    h=1e-20,                  # Parameter for dynamic cognitive weight
    f=1e-21,                  # Parameter for dynamic social weight
    use_hybrid=True,          # Use hybrid PSO + gradient descent
    n_iter=1000               # Number of iterations
)

How It Works

TSNE-PSO enhances the original t-SNE algorithm by replacing gradient descent with Particle Swarm Optimization. The algorithm:

  1. Initialization: Creates a swarm of particles with positions initialized via PCA, UMAP, t-SNE, or randomly
  2. Optimization: Updates particles using:
    • Cognitive component (attraction to personal best position)
    • Social component (attraction to global best position)
    • Inertia (tendency to continue current trajectory)
  3. Dynamic Parameters: Adapts cognitive and social weights over iterations
  4. Hybrid Approach: Optionally applies gradient descent steps to accelerate convergence

Citation

If you use this package in your research, please cite the following paper:

@article{allaoui2025t,
  title={t-SNE-PSO: Optimizing t-SNE using particle swarm optimization},
  author={Allaoui, Mebarka and Belhaouari, Samir Brahim and Hedjam, Rachid and Bouanane, Khadra and Kherfi, Mohammed Lamine},
  journal={Expert Systems with Applications},
  volume={269},
  pages={126398},
  year={2025},
  publisher={Elsevier}
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

BSD-3-Clause License

Development Setup

For development, follow these steps:

# Clone the repository
git clone https://github.com/draglesss/t-SNE-PSO.git
cd t-SNE-PSO

# Create and activate a virtual environment (optional but recommended)
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install development dependencies
pip install -r requirements-dev.txt

# Install the package in development mode
pip install -e .

Running Tests

# Run all tests
pytest

# Run tests with coverage
pytest --cov=tsne_pso

# Run tests without the slow ones
pytest -k "not slow"

Code Formatting

# Format code with black
black .

# Sort imports with isort
isort .

# Run type checking with mypy
mypy tsne_pso

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

tsne_pso-1.1.6.tar.gz (37.5 kB view details)

Uploaded Source

Built Distribution

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

tsne_pso-1.1.6-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

Details for the file tsne_pso-1.1.6.tar.gz.

File metadata

  • Download URL: tsne_pso-1.1.6.tar.gz
  • Upload date:
  • Size: 37.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for tsne_pso-1.1.6.tar.gz
Algorithm Hash digest
SHA256 989cc9e5eff799f214b7647a749483dfa627add520f65fc3059b8ed2db228746
MD5 97afac89fbbf6c6e9317b45eeaf8107e
BLAKE2b-256 7bdaed2961b0cb225440713959ccd36c94f908843b38774ee26b59a662461475

See more details on using hashes here.

File details

Details for the file tsne_pso-1.1.6-py3-none-any.whl.

File metadata

  • Download URL: tsne_pso-1.1.6-py3-none-any.whl
  • Upload date:
  • Size: 28.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for tsne_pso-1.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 25f56dfe65f71f7a20eca43efb944fc1a3b955592468e6ee1cc80ac54157cee4
MD5 66112db5de1294131dfee4d41b4fbb2f
BLAKE2b-256 8c921fb02f3089085b9d8375d684e05418994dfd2b6d5c76243c45716b004d91

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