scikit-network

repository·master·Indexed 20 days ago

https://github.com/sknetwork-team/scikit-network

A Python library for machine learning on graphs, version 0.33.5. It provides memory-efficient graph representations using scipy sparse matrices and an API inspired by scikit-learn. The library includes tools for node classification (DiffusionClassifier, NNClassifier, Propagation, PageRankClassifier), community detection (Louvain, Leiden), graph embedding (Spectral, SVD, PCA, ForceAtlas), and Graph Neural Networks (GNNClassifier). It also features utilities for loading graphs from CSV, GraphML, and standard datasets like NetSet and KONECT.

Tokens
33.4K
Snippets
151
Records
190
Agent score
70%

What's inside scikit-network

  1. Use graph embedding algorithms in scikit-network

    master

    Graph embedding algorithms in scikit-network assign a vector to each node of a graph. The resulting vectors are stored in the embedding_ attribute of the estimator after fitting.

    Available embedding methods include:

    • Spectral: Spectral embedding.
    • SVD: Singular Value Decomposition.
    • GSVD: Generalized Singular Value Decomposition.
    • PCA: Principal Component Analysis.
    • Random Projection: Random projection methods.
    • Louvain: Louvain-based embedding.
    • Force Atlas: Force Atlas layout-based embedding.
    • Spring: Spring layout-based embedding.
  2. Understand the dendrogram structure in hierarchical clustering

    master

    In scikit-network hierarchical clustering algorithms, the dendrogram_ attribute provides a representation of the successive merges of nodes.

    The dendrogram is an array of shape (n-1) imes 4. Each row in the array contains four values representing a single merge operation:

    1. The index of the first merged node.
    2. The index of the second merged node.
    3. The distance between the two nodes.
    4. The size of the resulting cluster.

    New nodes created by a merge are assigned the next available index (for example, in a dataset of size n, the first merge results in node index n).

  3. How graphs and algorithms work in scikit-network

    master

    Graph Representation

    Graphs are represented by their adjacency matrix in the sparse CSR format of scipy. For bipartite graphs, you can use a biadjacency matrix (typically a rectangular matrix).

    Algorithm API

    Every algorithm in the package is represented as an object that implements a .fit() method. You pass the adjacency matrix to this method to execute the algorithm.

    Bipartite Graph Outputs

    When working with bipartite graphs, algorithms apply to the nodes corresponding to the rows of the biadjacency matrix. To access specific outputs for the rows and columns respectively, use the suffixes _row_ and _col_ on the resulting attributes.

    from sknetwork.data import karate_club
    from sknetwork.clustering import Louvain
    
    # Load adjacency matrix
    adjacency = karate_club()
    
    # Initialize and run algorithm
    algorithm = Louvain()
    algorithm.fit(adjacency)
  4. Understand key graph theory terms in scikit-network

    master

    To use scikit-network effectively, you should be familiar with its core mathematical representations of graphs:

    • Adjacency Matrix: A square matrix (usually denoted as $A$) where entries indicate the edges between nodes in a graph.
    • Biadjacency Matrix: A rectangular matrix (usually denoted as $B$) used to represent edges between nodes in a bipartite graph.
    • Embedding: The process of mapping graph nodes to points in a vector space, often used for dimensionality reduction or visualization.
  5. Install scikit-network via pip

    master

    You can install the scikit-network library using pip. It is a free software library in Python designed for machine learning on graphs, featuring memory-efficient sparse matrix representations and a simple API inspired by scikit-learn.

    $ pip install scikit-network
  6. Represent graphs using adjacency matrices

    master

    In scikit-network, graphs are represented by square adjacency matrices $A$ of size $n imes n$.

    • Undirected graphs: The matrix $A$ is symmetric.
    • Directed graphs: The matrix $A$ is not necessarily symmetric (a link from $i$ to $j$ does not imply a link from $j$ to $i$).
    • Weighted graphs: Edge weights are represented by non-negative entries in the adjacency matrix.
    • Bipartite graphs: Represented by a rectangular biadjacency matrix $B$ of size $n_{row} imes n_{col}$, where $B_{ij}=1$ indicates an edge between node $i$ of the first set and node $j$ of the second set.

    Note on implementation: scikit-network uses the sparse CSR format from scipy for adjacency matrices.

    import numpy as np
    from scipy import sparse
    
    # Create a dense adjacency matrix
    adjacency = np.array([[0, 1, 1, 1, 0], [1, 0, 0, 0, 1], [1, 0, 0, 1, 0], [1, 0, 1, 0, 1], [0, 1, 0, 1, 0]])
    
    # Convert to sparse CSR format for scikit-network
    adjacency = sparse.csr_matrix(adjacency)
  7. How machine learning algorithms work in scikit-network

    master

    Machine learning algorithms in scikit-network follow a consistent API pattern. Each algorithm is an object that implements one or more of the following methods:

    • fit(adjacency, labels): Mandatory. Fits the algorithm to the graph. For bipartite graphs, fit is applied to the biadjacency matrix.
    • predict(): Predicts the output (e.g., node labels).
    • predict_proba(): Predicts the output with probabilities (e.g., probability distribution over labels).
    • transform(adjacency): Transforms the graph.
    • fit_predict(...): Combines fit and predict (or predict_proba / transform).

    Outputs: Results are stored as attributes of the object with a trailing underscore (e.g., labels_, embedding_, scores_, values_, links_, dendrogram_).