{ "cells": [ { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "# Katz centrality" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "This notebook illustrates the ranking of the nodes of a graph by [Katz centrality](https://en.wikipedia.org/wiki/Katz_centrality), a weighted average of number of paths of different lengths to each node." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "from IPython.display import SVG" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "from sknetwork.data import karate_club, painters, movie_actor\n", "from sknetwork.ranking import Katz\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" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "katz = Katz()\n", "scores = katz.fit_predict(adjacency)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "image = visualize_graph(adjacency, position, scores=scores)\n", "SVG(image)" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Directed graphs" ] }, { "cell_type": "code", "execution_count": 18, "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", "names = graph.names\n", "position = graph.position" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "katz = Katz()\n", "scores = katz.fit_predict(adjacency)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "image/svg+xml": [ "Pablo PicassoClaude MonetMichel AngeloEdouard ManetPeter Paul RubensRembrandtGustav KlimtEdgar DegasVincent van GoghLeonardo da VinciHenri MatissePaul CezannePierre-Auguste RenoirEgon Schiele" ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "image = visualize_graph(adjacency, position, scores=scores, names=names)\n", "SVG(image)" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Bipartite graphs" ] }, { "cell_type": "code", "execution_count": 21, "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": 22, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "katz = Katz()\n", "katz.fit(biadjacency)\n", "scores_row = katz.scores_row_\n", "scores_col = katz.scores_col_" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "image/svg+xml": [ "InceptionThe Dark Knight RisesThe Big ShortDriveThe Great GatsbyLa La LandCrazy Stupid LoveViceThe Grand Budapest HotelAviator007 SpectreInglourious BasterdsMidnight In ParisMurder on the Orient ExpressFantastic Beasts 2Leonardo DiCaprioMarion CotillardJoseph Gordon LewittChristian BaleRyan GoslingBrad PittCarey MulliganEmma StoneSteve CarellLea SeydouxRalph FiennesJude LawWillem DafoeChristophe WaltzJohnny DeppOwen Wilson" ], "text/plain": [ "" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "image = visualize_bigraph(biadjacency, names_row, names_col, scores_row=scores_row, scores_col=scores_col)\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": 4 }