Skip to content

Instantly share code, notes, and snippets.

@luerhard
Last active February 6, 2018 16:56
Show Gist options
  • Save luerhard/703716567879afefa59c06e5af48c371 to your computer and use it in GitHub Desktop.
Save luerhard/703716567879afefa59c06e5af48c371 to your computer and use it in GitHub Desktop.
my example for data school
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"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>number_of_nodes</th>\n",
" <th>number_of_edges</th>\n",
" <th>avg_shortest_path</th>\n",
" <th>transitivity</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2000</th>\n",
" <td>58</td>\n",
" <td>363</td>\n",
" <td>11.274652</td>\n",
" <td>0.990460</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2001</th>\n",
" <td>35</td>\n",
" <td>64</td>\n",
" <td>10.470588</td>\n",
" <td>0.864198</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2002</th>\n",
" <td>14</td>\n",
" <td>19</td>\n",
" <td>4.252747</td>\n",
" <td>0.666667</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2003</th>\n",
" <td>28</td>\n",
" <td>83</td>\n",
" <td>6.407407</td>\n",
" <td>0.950943</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2004</th>\n",
" <td>26</td>\n",
" <td>55</td>\n",
" <td>7.030769</td>\n",
" <td>0.897436</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" number_of_nodes number_of_edges avg_shortest_path transitivity\n",
"2000 58 363 11.274652 0.990460\n",
"2001 35 64 10.470588 0.864198\n",
"2002 14 19 4.252747 0.666667\n",
"2003 28 83 6.407407 0.950943\n",
"2004 26 55 7.030769 0.897436"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import networkx as nx\n",
"import pandas as pd\n",
"import random\n",
"\n",
"years, graphs = list(zip(*[(year, nx.barbell_graph(random.randint(3,25),random.randint(3,25))) for year in range(2000,2005)]))\n",
"df = pd.DataFrame({\"graph\": graphs}, index=years)\n",
"\n",
"df[\"number_of_nodes\"] = df.graph.apply(nx.number_of_nodes)\n",
"df[\"number_of_edges\"] = df.graph.apply(nx.number_of_edges)\n",
"df[\"avg_shortest_path\"] = df.graph.apply(nx.average_shortest_path_length)\n",
"df[\"transitivity\"] = df.graph.apply(nx.transitivity)\n",
"\n",
"\n",
"df.drop(\"graph\", axis=1)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The great thing about this is that you can easily compute multiple statistics at once and show them in a nice table."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment