{ "cells": [ { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "# Propagation" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "This notebook illustrates the classification of the nodes of a graph by [label propagation](https://en.wikipedia.org/wiki/Label_propagation_algorithm)." ] }, { "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.classification import Propagation\n", "from sknetwork.visualization import svg_graph, visualize_bigraph" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Graphs" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2019-07-15T12:29:51.261957Z", "start_time": "2019-07-15T12:29:51.249107Z" }, "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": 5, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "labels = {i: labels_true[i] for i in [0, 33]}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "propagation = Propagation()\n", "labels_pred = propagation.fit_predict(adjacency, labels)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "image = svg_graph(adjacency, position, labels=labels_pred, seeds=labels)\n", "SVG(image)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "# probability distribution over labels\n", "label = 1\n", "probs = propagation.predict_proba()\n", "scores = probs[:,label]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "image = svg_graph(adjacency, position, scores=scores, seeds=labels)\n", "SVG(image)" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Directed graphs" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2019-07-15T12:29:58.542147Z", "start_time": "2019-07-15T12:29:58.529699Z" }, "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": 14, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "rembrandt = 5\n", "klimt = 6\n", "cezanne = 11\n", "labels = {cezanne: 0, rembrandt: 1, klimt: 2}" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "propagation = Propagation()\n", "labels_pred = propagation.fit_predict(adjacency, labels)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "image = svg_graph(adjacency, position, names, labels=labels_pred, seeds=labels)\n", "SVG(image)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "# probability distribution over labels\n", "probs = propagation.predict_proba(())\n", "scores = probs[:,0]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "image = svg_graph(adjacency, position, names, scores=scores, seeds=[cezanne])\n", "SVG(image)" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Bipartite graphs" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "ExecuteTime": { "end_time": "2019-07-15T12:29:51.261957Z", "start_time": "2019-07-15T12:29:51.249107Z" }, "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": 23, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "inception = 0\n", "drive = 3\n", "budapest = 8" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "labels_row = {inception: 0, drive: 1, budapest: 2}" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "propagation = Propagation()\n", "propagation.fit(biadjacency, labels_row)\n", "labels_row_pred = propagation.labels_row_\n", "labels_col_pred = propagation.labels_col_" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "image = visualize_bigraph(biadjacency, names_row, names_col, labels_row_pred, labels_col_pred, seeds_row=labels_row)\n", "SVG(image)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "# probability distribution over labels\n", "probs_row = propagation.predict_proba()\n", "probs_col = propagation.predict_proba(columns=True)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "scores_row = probs_row[:,1]\n", "scores_col = probs_col[:,1]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "image = visualize_bigraph(biadjacency, names_row, names_col, scores_row=scores_row, scores_col=scores_col,\n", " seeds_row=labels_row)\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 }