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 kept.
embedding_method (
BaseEmbedding
) – Embedding method used to represent nodes in vector space. IfNone
(default), use identity.
- Variables:
links (sparse.csr_matrix) – Link matrix.
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