!pip install dimcli networkx pyvis -U --quiet
|████████████████████████████████| 307kB 8.6MB/s |████████████████████████████████| 51kB 4.3MB/s
import networkx as nx
from pyvis import network as net
import pyvis
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import scipy
from dimcli.core.extras import NetworkViz
lrst = pd.DataFrame({'class': ['Math', 'Math', 'Math', 'Math','English', 'English', 'English', 'PE', 'PE', 'PE', 'PE', 'History', 'History', 'History', 'History', 'History'],
'name': ['Carole ', 'Wood', 'Terrence ', 'Bowen','Anne', 'Singleton', 'Yvette','Edwards', 'Terrence ', 'Foster', 'Austin ','Montgomery', 'Lorena', 'Yvette', 'Kirk', 'Alexander']})
lrst.head(10)
class | name | |
---|---|---|
0 | Math | Carole |
1 | Math | Wood |
2 | Math | Terrence |
3 | Math | Bowen |
4 | English | Anne |
5 | English | Singleton |
6 | English | Yvette |
7 | PE | Edwards |
8 | PE | Terrence |
9 | PE | Foster |
df = nx.from_pandas_edgelist(lrst, source = 'class', target = 'name')
data = nx.adjacency_matrix(df)
print(data.todense())
[[0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0] [0 0 0 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1] [0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0]]
nodes = list(df.nodes())
documents = lrst['class'].unique().tolist()
names = lrst['name'].unique().tolist()
row_idx = []
for name in names:
temp = np.where(np.array(nodes) == name)
row_idx.append(temp[0][0])
col_idx = []
for document in documents:
temp = np.where(np.array(nodes) == document)
col_idx.append(temp[0][0])
column_names = lrst['class'].unique().tolist()
doc_matrix = pd.DataFrame(data[np.ix_(row_idx, col_idx)].todense(), columns = column_names)
doc_matrix.insert(0, 'name', pd.Series(names))
doc_matrix
name | Math | English | PE | History | |
---|---|---|---|---|---|
0 | Carole | 1 | 0 | 0 | 0 |
1 | Wood | 1 | 0 | 0 | 0 |
2 | Terrence | 1 | 0 | 1 | 0 |
3 | Bowen | 1 | 0 | 0 | 0 |
4 | Anne | 0 | 1 | 0 | 0 |
5 | Singleton | 0 | 1 | 0 | 0 |
6 | Yvette | 0 | 1 | 0 | 1 |
7 | Edwards | 0 | 0 | 1 | 0 |
8 | Foster | 0 | 0 | 1 | 0 |
9 | Austin | 0 | 0 | 1 | 0 |
10 | Montgomery | 0 | 0 | 0 | 1 |
11 | Lorena | 0 | 0 | 0 | 1 |
12 | Kirk | 0 | 0 | 0 | 1 |
13 | Alexander | 0 | 0 | 0 | 1 |
G = NetworkViz(notebook=True)
G.from_nx(df)
G.show("example.html")