Last active
January 13, 2025 21:34
-
-
Save JoeHelbing/ed328158c450d708083dfe5d5400cb4c to your computer and use it in GitHub Desktop.
network-graph-short-python-intro-via-runnable-code.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "colab_type": "text", | |
| "id": "view-in-github" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/JoeHelbing/ed328158c450d708083dfe5d5400cb4c/network-graph-short-python-intro-via-runnable-code.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "QslKXDO3xMTD" | |
| }, | |
| "source": [ | |
| "# Network Graph Short Python Intro via Runnable Code\n", | |
| "To run each code block you can use:\n", | |
| "- ctrl+enter to run the specific code cell\n", | |
| "- shift+enter to run the selected cell, and automatically move down to the next cell\n", | |
| "- The play button on the left side will also run the code on a mouseclick" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "id": "zuoj0A4us6IG" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Use BASH commands to install packages into the development environment\n", | |
| "# !pip install networkx pandas matplotlib seaborn plotly -q\n", | |
| "# !pip install pyvis -q # -q is a flag to make the install \"quiet\" i.e. not show a bunch of status logging" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "id": "1SV2yqzeqewN" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Import the main packages for data manipulation\n", | |
| "import pandas as pd # Dataframes in Python\n", | |
| "import networkx as nx # Handles graph objects, the datastructures, not the actual visualizatons\n", | |
| "import numpy as np # Matrix operations\n", | |
| "from collections import Counter # Python standard package to create dictionaries with counts\n", | |
| "\n", | |
| "# Create random data to graph\n", | |
| "from random import randint\n", | |
| "\n", | |
| "# Graphing packages\n", | |
| "import matplotlib.pyplot as plt # Graph plotting package used as the foundation that many graphing packages build off of\n", | |
| "import seaborn as sns # Good static graph package, simple and straightforward, easy to work with\n", | |
| "import plotly.graph_objects as go # More extensible graphing package, bit less user friendly, can export .html\n", | |
| "from pyvis.network import Network # Less popular and orphaned network graphing package, but still fantastic at what it does" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "id": "d9RqKc5Tvv8m" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Number of data points to generate\n", | |
| "num_points = 20 # Choose however large you'd like your graph to be.\n", | |
| "\n", | |
| "# Generate random data to create a Sankey Diagram\n", | |
| "# We want to have different source and target values aka one thing leads to a different thing\n", | |
| "source = [int(randint(0, 4)) for _ in range(num_points)] # Sources 0-4\n", | |
| "target = [int(randint(5, 9)) for _ in range(num_points)] # Targets 5-9\n", | |
| "value = value=[1] * len(source) # We will start with all flows of weight=1 (unweighted)\n", | |
| "\n", | |
| "# If you want to create new data and graph again, you can run this cell and the\n", | |
| "# cells below again and it will produce new graphs with the new data." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "id": "K-4jKa3qyGKf" | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>source</th>\n", | |
| " <th>target</th>\n", | |
| " <th>value</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>1</td>\n", | |
| " <td>8</td>\n", | |
| " <td>1</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>3</td>\n", | |
| " <td>8</td>\n", | |
| " <td>1</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>0</td>\n", | |
| " <td>5</td>\n", | |
| " <td>1</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>3</td>\n", | |
| " <td>7</td>\n", | |
| " <td>1</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>3</td>\n", | |
| " <td>7</td>\n", | |
| " <td>1</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " source target value\n", | |
| "0 1 8 1\n", | |
| "1 3 8 1\n", | |
| "2 0 5 1\n", | |
| "3 3 7 1\n", | |
| "4 3 7 1" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# Let's inspect the data.\n", | |
| "# Create a dictionary with the lists to convert the list values into a dataframe\n", | |
| "data = {'source': source, 'target': target, 'value': value}\n", | |
| "\n", | |
| "# Create the pandas DataFrame\n", | |
| "df = pd.DataFrame(data)\n", | |
| "\n", | |
| "# Display the DataFrame first 5 rows\n", | |
| "df.head()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": { | |
| "id": "_sOiqbYdq3dE" | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| " <script type=\"text/javascript\">\n", | |
| " window.PlotlyConfig = {MathJaxConfig: 'local'};\n", | |
| " if (window.MathJax && window.MathJax.Hub && window.MathJax.Hub.Config) {window.MathJax.Hub.Config({SVG: {font: \"STIX-Web\"}});}\n", | |
| " if (typeof require !== 'undefined') {\n", | |
| " require.undef(\"plotly\");\n", | |
| " define('plotly', function(require, exports, module) {\n", | |
| " /**\n", | |
| "* plotly.js v2.35.2\n", | |
| "* Copyright 2012-2024, Plotly, Inc.\n", | |
| "* All rights reserved.\n", | |
| "* Licensed under the MIT license\n", | |
| "*/\n", | |
| "/*! For license information please see plotly.min.js.LICENSE.txt */\n", |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment