<jupyter_start><jupyter_text>INCIDENCE MATRIX  decomposing Incidence matrix and plotting node features(W)<jupyter_code>inci = nx.incidence_matrix(G).todense()
print(inci.shape)
print(inci)<jupyter_output>(34, 78)
[[ 1.  1.  1. ...,  0.  0.  0.]
 [ 1.  0.  0. ...,  0.  0.  0.]
 [ 0.  1.  0. ...,  0.  0.  0.]
 ..., 
 [ 0.  0.  0. ...,  1.  1.  0.]
 [ 0.  0.  0. ...,  1.  0.  1.]
 [ 0.  0.  0. ...,  0.  1.  1.]]<jupyter_text>NMF Decomposition<jupyter_code>from sklearn.decomposition import NMF

model = NMF(n_components=2,init='random', random_state=0)
W = model.fit_transform(inci)
H = model.components_
err = model.reconstruction_err_
it = model.n_iter_

print(err)
print(it)
print(W.shape)
print(H.shape)
# print(W[0])
# print(H[:,0])<jupyter_output>10.941097066
27
(34, 2)
(2, 78)<jupyter_text>NMF displaying learned nodes<jupyter_code># displaying learned nodes
import matplotlib
import numpy as np

fig = plt.figure(figsize=(10,10))
colors=['green','hotpink','yellow', 'cyan']#,'red','purple']#,'blue','black','mediumseagreen','khaki','plum']
svd = fig.add_subplot(1,1,1)

svd.scatter(W[:, 0], W[:, 1],c=np.array(list(partition.values())),marker='o',s=[50,50],cmap=matplotlib.colors.ListedColormap(colors))
svd.title.set_text("W-nodes")

plt.show()<jupyter_output><empty_output><jupyter_text>NMF displaying learned edge vectors(H)<jupyter_code>#color edges
edges = G.edges()
ed_label = []
for ed in edges:
    if partition[ed[0]]==partition[ed[1]] and partition[ed[0]]==0:
        ed_label.append(0)
    elif partition[ed[0]]==partition[ed[1]] and partition[ed[0]]==1:
        ed_label.append(1)
    elif partition[ed[0]]==partition[ed[1]] and partition[ed[0]]==2:
        ed_label.append(2)
    elif partition[ed[0]]==partition[ed[1]] and partition[ed[0]]==3:
        ed_label.append(3)
    elif (partition[ed[0]]==0 and partition[ed[1]]==1) or (partition[ed[1]]==0 and partition[ed[0]]==1):
        ed_label.append(4)
    elif (partition[ed[0]]==0 and partition[ed[1]]==2) or (partition[ed[1]]==0 and partition[ed[0]]==2):
        ed_label.append(5)
    elif (partition[ed[0]]==0 and partition[ed[1]]==3) or (partition[ed[1]]==0 and partition[ed[0]]==3):
        ed_label.append(6)
    elif (partition[ed[0]]==2 and partition[ed[1]]==3) or (partition[ed[1]]==2 and partition[ed[0]]==3):
        ed_label.append(7)
    
print(len(edges))
print(len(ed_label))
# print((ed_label))
        
        
# displaying learned edge vectors(H)
import matplotlib
import numpy as np

fig = plt.figure(figsize=(10,10))
#        0-0      1-1      2-2       0-1    1-2   0-2
colors=['green','hotpink','yellow', 'cyan','red','purple','magenta','brown']
svd = fig.add_subplot(1,1,1)
H1 = np.transpose(H)
svd.scatter(H1[:, 0], H1[:, 1],c=np.array(ed_label),marker='o',s=[50,50],cmap=matplotlib.colors.ListedColormap(colors))
svd.title.set_text("W-edges")
plt.show()
# PCA and t-SNE for node features (W)
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from sklearn.preprocessing import normalize

fig = plt.figure(figsize=(10,10))
colors=['green','hotpink','yellow', 'cyan','red','purple']
# W1 = normalize(W)
tsne = fig.add_subplot(1,2,1)
X_tsne = TSNE(n_components=2, perplexity=40).fit_transform(W)
tsne.scatter(X_tsne[:, 0], X_tsne[:, 1], c=np.array(list(partition.values())),s=[50,50],cmap=matplotlib.colors.ListedColormap(colors))
tsne.title.set_text("t-SNE")

pca = fig.add_subplot(1,2,2)
X_pca = PCA(n_components=2).fit_transform(W)
pca.scatter(X_pca[:, 0], X_pca[:, 1], c=np.array(list(partition.values())), s=[50, 50], cmap=matplotlib.colors.ListedColormap(colors))
pca.title.set_text("PCA")

plt.show()
# PCA and t-SNE for edge features (H)
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from sklearn.preprocessing import normalize

fig = plt.figure(figsize=(10,10))
colors=['green','hotpink','yellow', 'cyan','red','purple']
H1 = np.transpose(H)
tsne = fig.add_subplot(1,2,1)
X_tsne = TSNE(n_components=2, perplexity=40).fit_transform(H1)
tsne.scatter(X_tsne[:, 0], X_tsne[:, 1], c=np.array(ed_label),s=[50,50],cmap=matplotlib.colors.ListedColormap(colors))
tsne.title.set_text("t-SNE")

pca = fig.add_subplot(1,2,2)
X_pca = PCA(n_components=2).fit_transform(H1)
pca.scatter(X_pca[:, 0], X_pca[:, 1], c=np.array(ed_label), s=[50, 50], cmap=matplotlib.colors.ListedColormap(colors))
pca.title.set_text("PCA")

plt.show()<jupyter_output><empty_output><jupyter_text>SVD decomposition of Incidence matrix<jupyter_code># SVD decomposition
ui,si,vi = np.linalg.svd(inci)

print(ui.shape)
# u=np.around(u,decimals=5)
# print(ui)

print(si.shape)
# s=np.around(s)
# print(si)

print(vi.shape)
# v=np.around(v,decimals=5)
# print(vi)<jupyter_output>(34, 34)
(34,)
(78, 78)<jupyter_text>SVD features of nodes decomposed from incidence matrix<jupyter_code>import matplotlib
import numpy as np

fig = plt.figure(figsize=(10,10))
colors=['green','hotpink','yellow', 'cyan','red','purple']
svd = fig.add_subplot(1,1,1)
print(len(list(partition.values())))
print(ui[:,0].shape)
svd.scatter([ui[:, 0]], [ui[:, 1]],c=np.array(list(partition.values())),s=[50,50],cmap=matplotlib.colors.ListedColormap(colors))
svd.title.set_text("U-nodes")
plt.show()<jupyter_output><empty_output><jupyter_text>SVD features of edges decomposed from incidence matrix<jupyter_code># SVD features of edges decomposed from incidence matrix
fig = plt.figure(figsize=(10,10))
colors=['green','hotpink','yellow', 'cyan','red','purple']
svd = fig.add_subplot(1,1,1)
vi1 = np.transpose(vi)
svd.scatter([vi1[:, 0]], [vi1[:, 1]],c=np.array(ed_label),s=[50,50],cmap=matplotlib.colors.ListedColormap(colors))
svd.title.set_text("W-edges")
plt.show()<jupyter_output><empty_output><jupyter_text>NORMALIZED GRAPH LAPLACIAN  Decomposing normalized laplacian and plotting node features(W)<jupyter_code># calculate normalized graph laplacian 
L = nx.normalized_laplacian_matrix(G).todense()
print(L.shape)
print(L[0,0:5])
# NMF does not work on input matrix with negative values
# from sklearn.decomposition import NMF

# model = NMF(n_components=2,init='random', random_state=0)
# # decomposing normalized graph laplacian L
# W = model.fit_transform(L)
# H = model.components_
# err = model.reconstruction_err_
# it = model.n_iter_
# print(err)
# print(it)
# print(W.shape)
# print(H.shape)
# print(W[0])
# print(H[:,0])<jupyter_output><empty_output><jupyter_text>SVD decomposition of normalized graph laplacian<jupyter_code># SVD decomposition
ul,sl,vl = np.linalg.svd(L)

print(ul.shape)
# u=np.around(u,decimals=5)
# print(ui)

print(sl.shape)
# s=np.around(s)
# print(si)

print(vl.shape)
# v=np.around(v,decimals=5)
# print(vi)<jupyter_output><empty_output><jupyter_text>displaying SVD node features(U) of laplacian matrix Doing SVD on normalized graph laplacian gives USV^T, where U and V are same, i.e. rows of U are same as clms of V^T. Hence below I displayed node features from U<jupyter_code>import matplotlib
import numpy as np

fig = plt.figure(figsize=(10,10))
colors=['green','hotpink','yellow', 'cyan','red','purple']
svd = fig.add_subplot(1,1,1)

svd.scatter([ul[:, 0]], [ul[:, 1]],c=np.array(list(partition.values())),s=[50,50],cmap=matplotlib.colors.ListedColormap(colors))
svd.title.set_text("U-nodes:SVD decomposition of normalized graph laplacian")
plt.show()
# applying tsne and pca on U-nde features--laplacian matrix
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from sklearn.preprocessing import normalize

fig = plt.figure(figsize=(10,10))
colors=['green','hotpink','yellow',   'cyan',    'red',        'purple']
# normalize
ul1 = normalize(ul)
tsne = fig.add_subplot(1,2,1)
X_tsne = TSNE(n_components=2, perplexity=40).fit_transform(ul1)
tsne.scatter(X_tsne[:, 0], X_tsne[:, 1], c=np.array(list(partition.values())),s=[50,50],cmap=matplotlib.colors.ListedColormap(colors))
tsne.title.set_text("t-SNE")

pca = fig.add_subplot(1,2,2)
X_pca = PCA(n_components=2).fit_transform(ul1)
pca.scatter(X_pca[:, 0], X_pca[:, 1], c=np.array(list(partition.values())), s=[50, 50], cmap=matplotlib.colors.ListedColormap(colors))
pca.title.set_text("PCA")

plt.show()<jupyter_output><empty_output><jupyter_text>ADJACENCY MATRIX  Decomposing Adjacency matrix and displaying node featues<jupyter_code>Adj = nx.adjacency_matrix(G)
print(Adj.todense().shape)

# convert adjacency matrix to dense matrix(default format is sparse matrix)
AdjDense = Adj.todense()<jupyter_output><empty_output><jupyter_text>NMF decomposition of Adjacency matrix<jupyter_code>from sklearn.decomposition import NMF

model = NMF(n_components=2,init='random', random_state=0)
Wa = model.fit_transform(AdjDense)
Ha= model.components_
erra = model.reconstruction_err_
ita = model.n_iter_
print(erra)
print(ita)
print(Wa.shape)
print(Ha.shape)
print(Wa[0])
print(Ha[:,0])
# displaying learned nodes
import matplotlib
import numpy as np

fig = plt.figure(figsize=(10,10))
colors=['green','hotpink','yellow', 'cyan','red','purple']
svd = fig.add_subplot(1,1,1)

svd.scatter([Wa[:, 0]], [Wa[:, 1]],c=np.array(list(partition.values())),s=[50,50],cmap=matplotlib.colors.ListedColormap(colors))
svd.title.set_text("W-nodes:NMF decomposition of Adjacency matrix")
plt.show()<jupyter_output><empty_output><jupyter_text>SVD Decomposition of adjacency matrix<jupyter_code># Calculate SVD (Singular value decomposition) of graph's adjacency matrix
ua,sa,va = np.linalg.svd(AdjDense)

print(ua.shape)
# u=np.around(u,decimals=3)
# print(u)

print(sa.shape)
# s=np.around(s)
# print(s)

print(va.shape)
# v=np.around(v,decimals=3)
# print(v)
import matplotlib
import numpy as np

fig = plt.figure(figsize=(10,10))
colors=['green','hotpink','yellow', 'cyan','red','purple']
svd = fig.add_subplot(1,1,1)

svd.scatter([ua[:, 0]], [ua[:, 1]],c=np.array(list(partition.values())),s=[50,50],cmap=matplotlib.colors.ListedColormap(colors))
svd.title.set_text("U-nodes:SVD Decomposition of adjacency matrix")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1837494965767217, 'fraction_numerical': 0.03433346757954088, 'mean_word_length': 6.559360730593608, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '451165', 'license': 'MIT', 'n_tokens_mistral': 4199, 'n_tokens_neox': 3990, 'n_words': 678, 'path': 'Zachary-incidence-mat-exp.ipynb', 'repo_name': 'supriya-pandhre/incidence-mat-exp', 'size': '9932'}