{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"# Link prediction"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"This notebook illustrates the prediction of links of a graph by nearest neighbors in the embedding space. Most links are supposed to be actual edges of the graph."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-15T12:29:50.554431Z",
"start_time": "2019-07-15T12:29:50.414075Z"
},
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"from IPython.display import SVG"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"from sknetwork.data import karate_club, painters, movie_actor\n",
"from sknetwork.linkpred import NNLinker\n",
"from sknetwork.embedding import Spectral\n",
"from sknetwork.visualization import visualize_graph, visualize_bigraph"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Graphs"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"graph = karate_club(metadata=True)\n",
"adjacency = graph.adjacency\n",
"position = graph.position\n",
"labels_true = graph.labels"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image = visualize_graph(adjacency, position)\n",
"SVG(image)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"linker = NNLinker(n_neighbors=3)\n",
"links = linker.fit_predict(adjacency)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image = visualize_graph(links, position, directed=False, display_edge_weight=False)\n",
"SVG(image)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"linker = NNLinker(threshold=0.5)\n",
"links = linker.fit_predict(adjacency)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image = visualize_graph(links, position, directed=False, display_edge_weight=False)\n",
"SVG(image)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"# Nearest neighbors in embedding space\n",
"linker = NNLinker(n_neighbors=5, threshold=0.5, embedding_method=Spectral(2))\n",
"links = linker.fit_predict(adjacency)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image = visualize_graph(links, position, directed=False, display_edge_weight=False)\n",
"SVG(image)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Directed graphs"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"graph = painters(metadata=True)\n",
"adjacency = graph.adjacency\n",
"position = graph.position\n",
"names = graph.names"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image = visualize_graph(adjacency, position, names)\n",
"SVG(image)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"linker = NNLinker(embedding_method=Spectral(3))\n",
"links = linker.fit_predict(adjacency)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image = visualize_graph(links, position, names, directed=True, display_edge_weight=False)\n",
"SVG(image)"
]
},
{
"cell_type": "markdown",
"metadata": {
"pycharm": {
"name": "#%% md\n"
}
},
"source": [
"## Bipartite graphs"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"graph = movie_actor(metadata=True)\n",
"biadjacency = graph.biadjacency\n",
"names_row = graph.names_row\n",
"names_col = graph.names_col"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image = visualize_bigraph(biadjacency, names_row, names_col)\n",
"SVG(image)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"linker = NNLinker(n_neighbors=5, threshold=0.5, embedding_method=Spectral(3))\n",
"links = linker.fit_predict(biadjacency)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [
{
"data": {
"image/svg+xml": [
""
],
"text/plain": [
""
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"image = visualize_bigraph(links, names_row, names_col, display_edge_weight=False)\n",
"SVG(image)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
},
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"metadata": {
"collapsed": false
},
"source": []
}
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}