# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Pairplot: Visualizing High Dimensional Data
#
# This example provides how to visualize high dimensional data using the pairplot.

# +
import graspy

import numpy as np
# %matplotlib inline
# -

# ## Simulate a binary graph using stochastic block model
# The 3-block model is defined as below:
#
# \begin{align*}
# n &= [50, 50, 50]\\
# P &= 
# \begin{bmatrix}0.5 & 0.1 & 0.05 \\
# 0.1 & 0.4 & 0.15 \\
# 0.05 & 0.15 & 0.3
# \end{bmatrix}
# \end{align*}
#
# Thus, the first 50 vertices belong to block 1, the second 50 vertices belong to block 2, and the last 50 vertices belong to block 3.

# +
from graspy.simulations import sbm

n_communities = [50, 50, 50]
p = [[0.5, 0.1, 0.05], 
     [0.1, 0.4, 0.15], 
     [0.05, 0.15, 0.3],]

np.random.seed(2)
A = sbm(n_communities, p)
# -

# ## Embed using adjacency spectral embedding to obtain lower dimensional representation of the graph
#
# The embedding dimension is automatically chosen. It should embed to 3 dimensions.

# +
from graspy.embed import AdjacencySpectralEmbed

ase = AdjacencySpectralEmbed()
X = ase.fit_transform(A)

print(X.shape)
# -

# ## Use pairplot to plot the embedded data
#
# First we generate labels that correspond to blocks. We pass the labels along with the data for pair plot.

# +
from graspy.plot import pairplot

labels = ['Block 1'] * 50 + ['Block 2'] * 50 + ['Block 3'] * 50

plot = pairplot(X, labels)

StarcoderdataJupyterScriptsDedupFiltered