Stochastic Blockmodel.ipynb - Colab
Stochastic Blockmodel.ipynb - Colab
ipynb - Colab
https://colab.research.google.com/drive/1J5VPLqq75tEOfmWKGVpSm0fCEP5MP6LC 1/11
12/4/24, 8:08 AM Stochastic Blockmodel.ipynb - Colab
<matplotlib.colorbar.Colorbar at 0x167fb20bc20>
<matplotlib.image.AxesImage at 0x167fca4fec0>
np.random.seed(42)
order = np.random.permutation(len(A_caveman))
A_caveman2 = A_caveman[order,:][:,order]
plt.matshow(A_caveman2)
https://colab.research.google.com/drive/1J5VPLqq75tEOfmWKGVpSm0fCEP5MP6LC 2/11
12/4/24, 8:08 AM Stochastic Blockmodel.ipynb - Colab
<matplotlib.image.AxesImage at 0x167fcad7c80>
keyboard_arrow_down How can we figure out which nodes are in the same cave?
Let's look at a few rows of the adjacency matrix.
Idea: Run K-Nearest Neighbors clustering with these rows as the feature vectors
It would group rows 0/4/15 into one cluster, and 2/5/12 into another
Clusters = Communities
We would reorder the nodes by grouping people from the same club together
Then, we would look at the new adjacency matrix
np.where(order>=10)[0]
https://colab.research.google.com/drive/1J5VPLqq75tEOfmWKGVpSm0fCEP5MP6LC 3/11
12/4/24, 8:08 AM Stochastic Blockmodel.ipynb - Colab
array([ 2, 5, 7, 8, 9, 12, 14, 16, 17], dtype=int64)
<matplotlib.image.AxesImage at 0x167fcb2acf0>
SUPPOSE someone told us here are the communities. How would we check?
IF the memberships are correct, the reordered adjacency matrix is block-structured.
Note: Whether the big block is first or the small block doesn't matter.
n = 10 # number of nodes
array([1, 1, 1, 0, 0, 1, 1, 1, 0, 1])
array([[0., 1.],
[0., 1.],
[0., 1.],
[1., 0.],
[1., 0.],
[0., 1.],
https://colab.research.google.com/drive/1J5VPLqq75tEOfmWKGVpSm0fCEP5MP6LC 4/11
12/4/24, 8:08 AM Stochastic Blockmodel.ipynb - Colab
[0., 1.],
[0., 1.],
[1., 0.],
[0., 1.]])
array([0., 0., 0., 1., 1., 0., 0., 0., 1., 0.])
plt.matshow(A1)
<matplotlib.image.AxesImage at 0x167fde95e50>
array([1., 1., 1., 0., 0., 1., 1., 1., 0., 1.])
A2 = np.outer(club2_fans, club2_fans)
plt.matshow(A2)
https://colab.research.google.com/drive/1J5VPLqq75tEOfmWKGVpSm0fCEP5MP6LC 5/11
12/4/24, 8:08 AM Stochastic Blockmodel.ipynb - Colab
<matplotlib.image.AxesImage at 0x167fdf3c410>
<matplotlib.image.AxesImage at 0x167fca65460>
https://colab.research.google.com/drive/1J5VPLqq75tEOfmWKGVpSm0fCEP5MP6LC 6/11
12/4/24, 8:08 AM Stochastic Blockmodel.ipynb - Colab
<matplotlib.image.AxesImage at 0x167fdf3f9b0>
⇒ To find the right memberships, we need to find the ordering that makes the matrix block-structured.
<matplotlib.image.AxesImage at 0x167fe47d100>
https://colab.research.google.com/drive/1J5VPLqq75tEOfmWKGVpSm0fCEP5MP6LC 7/11
12/4/24, 8:08 AM Stochastic Blockmodel.ipynb - Colab
fig, ax = plt.subplots(figsize=(10,5))
ax.matshow(eigenvectors[:,-2:])
<matplotlib.image.AxesImage at 0x167fca66300>
model = KMeans(n_clusters=2)
model.fit(eigenvectors[:,-2:]) # K-Means on the eigenvector rows
predicted_clubs = model.labels_ # Clusters found by K-Means
predicted_clubs
predicted_club1_members = np.where(predicted_clubs==0)[0]
predicted_club2_members = np.where(predicted_clubs==1)[0]
print('Predicted clubs', predicted_club1_members, 'and', predicted_club2_members)
https://colab.research.google.com/drive/1J5VPLqq75tEOfmWKGVpSm0fCEP5MP6LC 8/11
12/4/24, 8:08 AM Stochastic Blockmodel.ipynb - Colab
<matplotlib.image.AxesImage at 0x16780407fb0>
keyboard_arrow_down Generalization
Story so far:
Generalization:
Fans of the same club become friends with probability 0.8 (say)
Fans of different clubs become friends with probability 0.10 (say)
# B = cluster-connection matrix
B = np.array([[0.8, 0.1], [0.1, 0.8]])
sympy.Matrix(B)
Now: We create a probability matrix, from which we sample the adjacency matrix
array([[0.8, 0.8, 0.8, 0.1, 0.1, 0.8, 0.8, 0.8, 0.1, 0.8],
[0.8, 0.8, 0.8, 0.1, 0.1, 0.8, 0.8, 0.8, 0.1, 0.8],
[0.8, 0.8, 0.8, 0.1, 0.1, 0.8, 0.8, 0.8, 0.1, 0.8],
[0.1, 0.1, 0.1, 0.8, 0.8, 0.1, 0.1, 0.1, 0.8, 0.1],
[0.1, 0.1, 0.1, 0.8, 0.8, 0.1, 0.1, 0.1, 0.8, 0.1],
[0.8, 0.8, 0.8, 0.1, 0.1, 0.8, 0.8, 0.8, 0.1, 0.8],
[0.8, 0.8, 0.8, 0.1, 0.1, 0.8, 0.8, 0.8, 0.1, 0.8],
[0.8, 0.8, 0.8, 0.1, 0.1, 0.8, 0.8, 0.8, 0.1, 0.8],
[0.1, 0.1, 0.1, 0.8, 0.8, 0.1, 0.1, 0.1, 0.8, 0.1],
[0.8, 0.8, 0.8, 0.1, 0.1, 0.8, 0.8, 0.8, 0.1, 0.8]])
array([[1, 0, 1, 0, 0, 1, 0, 1, 0, 1],
[1, 1, 0, 0, 0, 1, 1, 1, 0, 1],
[0, 1, 1, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0, 0, 1, 0],
[1, 1, 1, 0, 0, 1, 0, 1, 1, 0],
[1, 1, 1, 0, 0, 1, 1, 1, 0, 0],
https://colab.research.google.com/drive/1J5VPLqq75tEOfmWKGVpSm0fCEP5MP6LC 9/11
12/4/24, 8:08 AM Stochastic Blockmodel.ipynb - Colab
[0, 1, 1, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 1, 1, 0],
[0, 1, 1, 0, 0, 1, 1, 0, 0, 1]])
plt.matshow(A)
<matplotlib.image.AxesImage at 0x167808be510>
<matplotlib.image.AxesImage at 0x167808e0950>
https://colab.research.google.com/drive/1J5VPLqq75tEOfmWKGVpSm0fCEP5MP6LC 10/11
12/4/24, 8:08 AM Stochastic Blockmodel.ipynb - Colab
G = nx.from_numpy_array(A)
communities = nx.community.louvain_communities(G)
communities
<matplotlib.image.AxesImage at 0x167809c6510>
https://colab.research.google.com/drive/1J5VPLqq75tEOfmWKGVpSm0fCEP5MP6LC 11/11