Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better comments in OOS tutorial #605

Merged
merged 1 commit into from
Dec 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions docs/tutorials/embedding/OutOfSampleEmbed.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@
"P = np.array([[0.8, 0.2],\n",
" [0.2, 0.8]])\n",
"\n",
"# Generate a directed and undirected Stochastic Block Model (SBM)\n",
"undirected, labels = sbm(2*[nodes_per_community], P, return_labels=True)\n",
"labels = list(labels)\n",
"# Generate an undirected Stochastic Block Model (SBM)\n",
"undirected, labels_ = sbm(2*[nodes_per_community], P, return_labels=True)\n",
"labels = list(labels_)\n",
"\n",
"# Grab out-of-sample vertices\n",
"oos_idx = 0\n",
"oos_labels = labels.pop(oos_idx)\n",
"A, a = remove_vertices(undirected, indices=oos_idx, return_removed=True)\n",
"\n",
"# plot our SBMs\n",
"# plot our SBM\n",
"heatmap(A, title=f'2-block SBM (undirected), shape {A.shape}', inner_hier_labels=labels);"
]
},
Expand Down Expand Up @@ -142,7 +142,7 @@
" # Plot the in-sample latent positions\n",
" plot = pairplot(X_hat, labels=labels, title=title)\n",
"\n",
" # generate out-of-sample dataframe\n",
" # generate an out-of-sample dataframe\n",
" oos_vertices = np.atleast_2d(oos_vertices)\n",
" data = {'Type': oos_labels, \n",
" 'Dimension 1': oos_vertices[:, 0], \n",
Expand All @@ -153,11 +153,12 @@
" # plotting out-of-sample latent positions as stars\n",
" plot.data = oos_df\n",
" plot.hue_vals = oos_df[\"Type\"]\n",
" plot.map_offdiag(sns.scatterplot, s=500, marker=\"*\", edgecolor=\"black\")\n",
" plot.map_offdiag(sns.scatterplot, s=500,\n",
" marker=\"*\", edgecolor=\"black\")\n",
" plot.tight_layout()\n",
" return plot\n",
"\n",
" \n",
"# Plot all latent positions\n",
"plot_oos(X_hat, w, labels=labels, oos_labels=[0], title=\"Out-of-Sample Embeddings (2-block SBM)\");"
]
},
Expand All @@ -182,7 +183,7 @@
"outputs": [],
"source": [
"# Grab out-of-sample vertices\n",
"labels = [0]*nodes_per_community + [1]*nodes_per_community\n",
"labels = list(labels_)\n",
"oos_idx = [0, -1]\n",
"oos_labels = [labels.pop(i) for i in oos_idx]\n",
"A, a = remove_vertices(undirected, indices=oos_idx, return_removed=True)\n",
Expand All @@ -205,6 +206,7 @@
"w = ase.transform(a)\n",
"print(f\"The out-of-sample prediction output has dimensions {w.shape}\\n\")\n",
"\n",
"# Plot all latent positions\n",
"plot_oos(X_hat, w, labels, oos_labels=oos_labels,\n",
" title=\"Out-of-Sample Embeddings (2-block SBM)\");"
]
Expand Down Expand Up @@ -232,10 +234,14 @@
"metadata": {},
"outputs": [],
"source": [
"# a is a tuple of (out_oos, in_oos)\n",
"# Generate a directed SBM\n",
"directed = sbm(2*[nodes_per_community], P, directed=True)\n",
"oos_idx = [0, -1]\n",
"\n",
"# a is a tuple of (out_oos, in_oos)\n",
"A, a = remove_vertices(directed, indices=oos_idx, return_removed=True)\n",
"\n",
"# Plot the new adjacency matrix\n",
"heatmap(directed, title=f'2-block SBM (directed), shape {A.shape}');"
]
},
Expand All @@ -245,7 +251,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Fit a directed graph\n",
"# Fit our directed graph\n",
"X_hat, Y_hat = ase.fit_transform(A)\n",
"\n",
"# predicted latent positions\n",
Expand Down Expand Up @@ -292,14 +298,18 @@
"metadata": {},
"outputs": [],
"source": [
"# Generate a weighted graph and out-of-sample vertices\n",
"# Generate a weighted, directed SBM\n",
"wt = [[normal, poisson],\n",
" [poisson, normal]]\n",
"wtargs = [[dict(loc=3, scale=1), dict(lam=5)],\n",
" [dict(lam=5), dict(loc=3, scale=1)]]\n",
"weighted = sbm(2*[nodes_per_community], P, wt=wt, wtargs=wtargs, directed=True)\n",
"\n",
"# Generate out-of-sample vertices\n",
"oos_idx = [0, -1]\n",
"A, a = remove_vertices(weighted, indices=oos_idx, return_removed=True)\n",
"\n",
"# Plot our weighted, directed SBM\n",
"heatmap(A, title=f'2-block SBM (directed, weighted), shape {A.shape}')"
]
},
Expand Down