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

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 (same as scikit-learn)

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.4.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.

tsne_pso-1.1.4-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tsne_pso-1.1.4.tar.gz
  • Upload date:
  • Size: 19.0 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.4.tar.gz
Algorithm Hash digest
SHA256 d25d6a6627c4aaa0c89f1d458c125411fb1a21535ad0cf0f35ee701c1e6e0177
MD5 b657d9bfa6dc57ba01cb95868de560b8
BLAKE2b-256 f5231b14bf4dcf14465e678624b680f153435e5ceb9a2f7a174006978a2e676f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tsne_pso-1.1.4-py3-none-any.whl
  • Upload date:
  • Size: 17.7 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f5e8a20264c4a1839e3c8fa2cb3d2102a74b5dc908068b0de9b404ea4988417a
MD5 5b001fce4244e9c46d86645e2714c56d
BLAKE2b-256 02725c999d2e2e83af6bfc55e7e186b6c024bd41809ca5b9ae28e6fe451df33e

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