{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# ForceAtlas\n", "\n", "This notebook illustrates the embedding of a graph through the force-directed algorithm Force Atlas 2.\n", "\n" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "from IPython.display import SVG" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "from sknetwork.data import karate_club\n", "from sknetwork.embedding.force_atlas import ForceAtlas\n", "from sknetwork.visualization import visualize_graph" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "graph = karate_club(metadata=True)\n", "adjacency = graph.adjacency\n", "labels = graph.labels" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "forceatlas2 = ForceAtlas()\n", "embedding = forceatlas2.fit_transform(adjacency)\n", "image = visualize_graph(adjacency, embedding, labels=labels)\n", "SVG(image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Options" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we illustrate the influences of the different settings offered to the user." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Replace the linear attraction force with a logarithmic attraction force." ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "forceatlas2 = ForceAtlas(lin_log = True)\n", "embedding = forceatlas2.fit_transform(adjacency)\n", "image = visualize_graph(adjacency, embedding, labels=labels)\n", "SVG(image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set the gravity and repulsion force constants (`gravity_factor` and `repulsion_factor`)\n", "to set the importance of each force in the layout. Keep values between 0.01 and 0.1." ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "forceatlas2 = ForceAtlas(gravity_factor = 0.1)\n", "embedding = forceatlas2.fit_transform(adjacency)\n", "image = visualize_graph(adjacency, embedding, labels=labels)\n", "SVG(image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set the amount of swinging tolerated. Lower swinging yields less speed and more precision." ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "forceatlas2 = ForceAtlas(tolerance=1.5)\n", "embedding = forceatlas2.fit_transform(adjacency)\n", "image = visualize_graph(adjacency, embedding, labels=labels)\n", "SVG(image)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "image/svg+xml": [ "" ], "text/plain": [ "" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "forceatlas2 = ForceAtlas(approx_radius=2)\n", "embedding = forceatlas2.fit_transform(adjacency)\n", "image = visualize_graph(adjacency, embedding, labels=labels)\n", "SVG(image)" ] } ], "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" } }, "nbformat": 4, "nbformat_minor": 4 }