{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Node equivalence with Node2Vec" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Node2Vec is an algorithm designed for detecting structural equivalence and segments of a graph.\n", "\n", "In this notebook, the Leviticus-network will be explored with Node2Vec in a progression of complexity, according to the following criteria:\n", "1. The network as a directed but unweighted graph\n", "2. The network as a multiple directed graph (weights)\n", "3. The network as a multiple directed graph (weighted by average agency values)\n", "\n", "**Other tutorials**\n", "* https://towardsdatascience.com/node2vec-embeddings-for-graph-data-32a866340fef\n", "* https://medium.com/neo4j/machine-learning-on-graphs-fca6eeb8f1d1\n", "* https://www.kaggle.com/ferdzso/knowledge-graph-analysis-with-node2vec\n", "* https://www.kaggle.com/pierremegret/gensim-word2vec-tutorial" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import collections\n", "\n", "import networkx as nx\n", "from node2vec import Node2Vec\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import pandas as pd\n", "import numpy as np\n", "from sklearn.cluster import KMeans\n", "from sklearn.manifold import MDS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Import data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | Source | \n", "Source_agency | \n", "Source_ph | \n", "Target | \n", "Target_agency | \n", "Target_ph | \n", "Label | \n", "Weight | \n", "Clause | \n", "
|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "Aaron's_sons | \n", "5 | \n", "690343 | \n", "YHWH | \n", "0 | \n", "690347 | \n", "swing | \n", "25 | \n", "440323 | \n", "
| 1 | \n", "YHWH | \n", "5 | \n", "690383 | \n", "Moses | \n", "-1 | \n", "690384 | \n", "speak | \n", "36 | \n", "440335 | \n", "
| 2 | \n", "Israelites | \n", "5 | \n", "690397 | \n", "YHWH | \n", "-1 | \n", "690399 | \n", "approach | \n", "36 | \n", "440341 | \n", "
| 3 | \n", "YHWH | \n", "5 | \n", "690402 | \n", "Moses | \n", "-1 | \n", "690403 | \n", "speak | \n", "36 | \n", "440342 | \n", "
| 4 | \n", "Israelites | \n", "5 | \n", "690415 | \n", "YHWH | \n", "-1 | \n", "690417 | \n", "approach | \n", "36 | \n", "440347 | \n", "