<jupyter_start><jupyter_text>Infinite relational modelClustering of binary data. For example, suppose the row is the user and the column is the purchase status of each product. Use Poisson Infinite relational model when you want to cluster products with users who have the same tendency from the user's purchase history.<jupyter_code>import numpy as np
from scipy import stats

import seaborn as sns
import matplotlib.pyplot as plt

from irmlearn import IRM<jupyter_output><empty_output><jupyter_text>Prepare data<jupyter_code>def prepare_data():
    np.random.seed(0)
    h_beta = [
        (5, 10),
        (2, 20),
        (17, 4),
        (20, 1),
        (10, 2),
        (2, 16),
        (22, 4),
        (8, 29),
        (5, 31),
        (11, 17),
        (41, 3),
        (6, 32),
    ]

    thetas = []
    for h in h_beta:
        theta = np.random.beta(*h, (10, 10))
        thetas.append(theta)

    temp = []
    for i in range(0, 12, 3):
        temp.append(
            np.hstack([thetas[i], thetas[i+1], thetas[i+2]])
        )
    theta_ = np.vstack(temp)

    X = stats.bernoulli.rvs(p=theta_, size=theta_.shape)
    X_ = X.copy()
    row_index = X.shape[0]
    col_index = X.shape[1]

    X = X[np.random.permutation(row_index)]
    X = X[:, np.random.permutation(col_index)]

    return X, X_
X, X_ = prepare_data()<jupyter_output><empty_output><jupyter_text>Data is generated with a different probability distribution for each block.<jupyter_code>plt.figure(figsize=(10, 7))
sns.heatmap(X_, cmap="Greens")
plt.axis('off')
plt.show()<jupyter_output><empty_output><jupyter_text>Shuffle rows and columns to make train data.<jupyter_code>plt.figure(figsize=(10, 7))
sns.heatmap(X, cmap="Greens")
plt.axis('off')
plt.show()<jupyter_output><empty_output><jupyter_text>Traininig<jupyter_code>alpha = 1.5
a = 0.1
b = 0.1
max_iter = 300

model = IRM(alpha, a, b, max_iter, verbose=True, use_best_iter=True)
model.fit(X)
plt.plot(model.history_)
row_index = np.argsort(model.sample_labels_)
col_index = np.argsort(model.feature_labels_)
X__ = X[row_index, :][:, col_index]

_, hlist = np.unique(np.sort(model.sample_labels_), return_counts=True)
_, vlist = np.unique(np.sort(model.feature_labels_), return_counts=True)

hlist = np.cumsum(hlist)
vlist = np.cumsum(vlist)

plt.figure(figsize=(10, 7))
sns.heatmap(X__, cmap="Greens")
for y in hlist[:-1]:
    plt.axhline(y=y, linewidth=2, color="r")    
for x in vlist[:-1]:
    plt.axvline(x=x, linewidth=2, color="r")
plt.axis("off")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14443567482241515, 'fraction_numerical': 0.028413575374901343, 'mean_word_length': 3.7830188679245285, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '608301', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 1042, 'n_tokens_neox': 972, 'n_words': 217, 'path': 'demo/irm_clustering.ipynb', 'repo_name': 'ground0state/irmlearn', 'size': '2534'}