Co-clustering vertices and hyperedges#

Spectral co-clustering (Dhillon 2001) clusters the rows and columns of the incidence matrix at the same time. On the synthetic news data it groups themes with the articles that use them; ordering the matrix by the co-clusters exposes the narratives as dense blocks.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

import hatvis as hv
from hatvis.plotting import plot_incidence

T = hv.datasets.make_news_transactions(60, random_state=0)
# [snippet:cocluster]
rl, cl = hv.spectral_coclustering(T, n_clusters=6, random_state=0)
rows = list(rl.sort_values(kind="stable").index)
cols = list(cl.sort_values(kind="stable").index)
# [/snippet:cocluster]
print(pd.crosstab(rl, T.node_data["group"]))
group             climate  energy  finance  impacts  policy  society
vertex bicluster
0                       4       0        0        0       0        0
1                       0       0        0        0       4        0
2                       0       0        4        4       0        0
3                       0       0        0        0       0        4
4                       0       4        0        0       0        0
fig, ax = plt.subplots(figsize=(12, 4.5))
plot_incidence(T, order=(rows, cols), ax=ax, labels=False, title="incidence matrix ordered by co-clusters")
ax.set_yticks(range(len(rows)), rows, fontsize=6)
b = np.cumsum(rl.value_counts().sort_index().to_numpy())[:-1]
for y in b:
    ax.axhline(y - 0.5, color=hv.plotting.PALETTE[1], lw=0.8)
plt.tight_layout()
incidence matrix ordered by co-clusters

Total running time of the script: (0 minutes 0.459 seconds)

Gallery generated by Sphinx-Gallery