Skip to content

Commit

Permalink
Updated advanced notebook.
Browse files Browse the repository at this point in the history
Now using Bipartite method of leidenalg. Now also mention that the
leiden algorithm is also available from igraph directly.
  • Loading branch information
vtraag committed Jun 11, 2020
1 parent 9528c58 commit d366c72
Showing 1 changed file with 26 additions and 42 deletions.
68 changes: 26 additions & 42 deletions 02-advanced.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"clusters[30]"
]
"source": []
},
{
"cell_type": "markdown",
Expand All @@ -246,9 +244,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"clusters.membership[:10]"
]
"source": []
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -371,9 +367,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"optimiser.optimise_partition(clusters, n_iterations=10)"
]
"source": []
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -431,6 +425,25 @@
"clusters.compare_to(clustering, method='nmi')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, the Leiden algorithm is also directly implemented in `igraph` itself nowadays. It is somewhat less elaborate than the `leidenalg` package, but it is also substantially faster. If you are analysing very large networks, it might be better to use the `igraph` Leiden algorithm. Using it is straightforward."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"clusters = G_vosviewer.community_leiden(objective_function='CPM',weights='weight_normalized', \n",
" resolution_parameter=1.0, n_iterations=10)\n",
"\n",
"clusters.compare_to(clustering, method='nmi')"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -588,7 +601,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We now employ a small trick in order to do clustering in a bipartite network. We will not explain the full details, but it involves creating two empty networks. Please see the [documentation](https://leidenalg.readthedocs.io/en/latest/multiplex.html#bipartite) for a brief explanation of this approach."
"We now employ a small trick in the `leidenalg` package in order to do clustering in a bipartite network. We will not explain the full details here, please see the [documentation](https://leidenalg.readthedocs.io/en/latest/multiplex.html#bipartite) for a brief explanation of this approach. Please note that this approach is *not* possible using the internal `igraph` Leiden algorithm."
]
},
{
Expand All @@ -597,37 +610,8 @@
"metadata": {},
"outputs": [],
"source": [
"H_docs = G_doc_term.subgraph_edges([], delete_vertices=False);\n",
"H_docs.vs['node_sizes'] = [1 if v['type'] == 'doc' else 0 for v in H_docs.vs];\n",
"\n",
"H_terms = G_doc_term.subgraph_edges([], delete_vertices=False);\n",
"H_terms.vs['node_sizes'] = [1 if v['type'] == 'term' else 0 for v in H_terms.vs];"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In order to make this trick work, we now also have to create three separate partitions as follows. The `res_param` contains the resolution parameter that we previously used, and again plays a similar role. A value of around `1` seems to give reasonable results in this case."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"res_param = 1\n",
"partition = leidenalg.CPMVertexPartition(G_doc_term, weights='weight', \n",
" resolution_parameter=res_param)\n",
"\n",
"partition_docs = leidenalg.CPMVertexPartition(H_docs, weights='weight', \n",
" node_sizes=H_docs.vs['node_sizes'], \n",
" resolution_parameter=res_param)\n",
"\n",
"partition_terms = leidenalg.CPMVertexPartition(H_terms, weights='weight', \n",
" node_sizes=H_terms.vs['node_sizes'], \n",
" resolution_parameter=res_param)"
"partition, partition_docs, partition_terms = leidenalg.CPMVertexPartition.Bipartite(\n",
" G_doc_term, types='type', weights='weight', resolution_parameter_01=1)"
]
},
{
Expand Down Expand Up @@ -944,7 +928,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.8.2"
}
},
"nbformat": 4,
Expand Down

0 comments on commit d366c72

Please sign in to comment.