networks.embed.py¶
- class apparent.networks.embed.NetworkEmbedder(pairwise_distances=None, method=<class 'sklearn.manifold._t_sne.TSNE'>, **kwargs)¶
A class for embedding networks into a lower-dimensional space using pairwise distances or raw data.
This class provides functionality to embed networks into a lower-dimensional space for visualization and analysis. It supports both precomputed distance matrices and raw data, using t-SNE as the default embedding method.
- Parameters:
pairwise_distances (np.ndarray, optional) – A pairwise distance matrix (n x n) between networks. Used for precomputed embedding methods.
method (object, optional) – An embedding method class. Default is TSNE from scikit-learn.
**kwargs (dict) – Additional arguments passed to the embedding method.
- D¶
A pairwise distance matrix (n x n) between networks.
- Type:
np.ndarray or None
- method¶
An embedding method class (default: TSNE).
- Type:
object
- scaler¶
Scaler for standardizing the embedding output.
- Type:
StandardScaler
- kwargs¶
Additional arguments for the embedding method.
- Type:
dict
Examples
>>> import numpy as np >>> from apparent.networks import NetworkEmbedder >>> >>> # Create sample distance matrix >>> distances = np.random.rand(10, 10) >>> distances = (distances + distances.T) / 2 # Make symmetric >>> np.fill_diagonal(distances, 0) # Zero diagonal >>> >>> # Create embedding >>> embedder = NetworkEmbedder(pairwise_distances=distances) >>> embedding = embedder.embed() >>> print(f"Embedding shape: {embedding.shape}")
- __init__(pairwise_distances=None, method=<class 'sklearn.manifold._t_sne.TSNE'>, **kwargs)¶
Initialize the NetworkEmbedder.
- Parameters:
pairwise_distances (np.ndarray, optional) – Pairwise distance matrix for precomputed embedding methods. Should be a symmetric matrix with zeros on the diagonal.
method (object, optional) – The embedding method class to use. Default is TSNE from scikit-learn. Must implement fit_transform method.
**kwargs (dict) – Additional arguments for the embedding method. Common parameters for t-SNE include ‘n_components’, ‘perplexity’, ‘learning_rate’.
Examples
>>> embedder = NetworkEmbedder() # Default t-SNE >>> embedder = NetworkEmbedder(method=TSNE, perplexity=30.0) >>> embedder = NetworkEmbedder(pairwise_distances=distances)
- embed(data=None)¶
Perform the embedding based on the selected method.
This method creates a low-dimensional embedding of the networks using either precomputed pairwise distances or raw data. The embedding is standardized using StandardScaler before being returned.
- Parameters:
data (np.ndarray, optional) – Raw input data for embedding (used if pairwise_distances is None). Should be a 2D array with shape (n_samples, n_features).
- Returns:
embedding – Low-dimensional embedding of the networks, typically 2D for visualization. The embedding is standardized to have zero mean and unit variance.
- Return type:
np.ndarray
- Raises:
ValueError – If neither pairwise distances nor data are provided.
Examples
>>> embedder = NetworkEmbedder(pairwise_distances=distances) >>> embedding = embedder.embed() >>> print(f"Embedding shape: {embedding.shape}") # (n_networks, 2) >>> >>> # Using raw data instead >>> embedder = NetworkEmbedder() >>> embedding = embedder.embed(data=feature_matrix)