Logo

Introduction

  • Overview
  • Installation
  • Import
  • Get started

User manual

  • Data
  • Topology
  • Path
  • Clustering
  • Classification
  • GNN
  • Regression
  • Hierarchy
  • Embedding
  • Ranking
  • Link prediction
    • Nearest neighbors
      • NNLinker
        • NNLinker.fit()
        • NNLinker.fit_predict()
        • NNLinker.get_params()
        • NNLinker.predict()
        • NNLinker.set_params()
  • Linear algebra
  • Utils
  • Visualization

Tutorials

  • Overview
  • Data
  • Topology
  • Path
  • Clustering
  • Classification
  • GNN
  • Regression
  • Hierarchy
  • Embedding
  • Ranking
  • Link prediction
  • Visualization

Use cases

  • Text mining
  • Wikipedia
  • Recommendation
  • Politics
  • Sport

About

  • Credits
  • History
  • Contributing
  • Index
  • Glossary
scikit-network
  • Link prediction
  • View page source

Link prediction

Link prediction algorithms.

The attribute links_ gives the predicted links of each node as a sparse matrix.

Nearest neighbors

class sknetwork.linkpred.NNLinker(n_neighbors: int | None = 10, threshold: float = 0, embedding_method: BaseEmbedding | None = None)[source]

Link prediction by nearest neighbors in the embedding space, using cosine similarity.

For bipartite graphs, predict links between rows and columns only.

Parameters:
  • n_neighbors (int) – Number of nearest neighbors. If None, all nodes are considered.

  • threshold (float) – Threshold on cosine similarity. Only links above this threshold are returned.

  • embedding_method (BaseEmbedding) – Embedding method used to represent nodes in vector space. If None (default), use identity.

Variables:

links (sparse.csr_matrix) – Matrix of links with cosine similarities as link weights.

Example

>>> from sknetwork.linkpred import NNLinker
>>> from sknetwork.data import karate_club
>>> linker = NNLinker(n_neighbors=5, threshold=0.5)
>>> graph = karate_club(metadata=True)
>>> adjacency = graph.adjacency
>>> links = linker.fit_predict(adjacency)
>>> links.shape
(34, 34)
fit(input_matrix: csr_matrix | ndarray, index: ndarray | None = None) → NNLinker[source]

Link prediction by nearest neighbors in the embedding space, using cosine similarity

Parameters:
  • input_matrix (sparse.csr_matrix, np.ndarray) – Adjacency matrix or biadjacency matrix of the graph.

  • index (np.ndarray) – Index of source nodes to consider. If None, the links are predicted for all nodes.

Returns:

self

Return type:

NN

fit_predict(*args, **kwargs) → csr_matrix

Fit algorithm to data and return the links. Same parameters as the fit method.

Returns:

links_ – Link matrix.

Return type:

sparse.csr_matrix

get_params()

Get parameters as dictionary.

Returns:

params – Parameters of the algorithm.

Return type:

dict

predict() → csr_matrix

Return the predicted links.

Returns:

links_ – Link matrix.

Return type:

sparse.csr_matrix

set_params(params: dict) → Algorithm

Set parameters of the algorithm.

Parameters:

params (dict) – Parameters of the algorithm.

Returns:

self

Return type:

Algorithm

Previous Next

© Copyright 2020, scikit-network.

Built with Sphinx using a theme provided by Read the Docs.