<jupyter_start><jupyter_text>BLSLDA Profiling<jupyter_code>from modules.helpers import plot_images
from functools import partial
from sklearn.metrics import (roc_auc_score, roc_curve)
import seaborn as sns

imshow = partial(plt.imshow, cmap='gray', interpolation='nearest', aspect='auto')
sns.set(style='white')<jupyter_output><empty_output><jupyter_text>Generate document-term matrix<jupyter_code>V = 100
K = 10
N = 100
D = 1000
alpha = np.repeat(1., K)
beta = np.repeat(1., V)
# generate phi
phi = np.random.RandomState(42).dirichlet(beta, size=K)
imshow(phi)
# generate theta
theta = np.random.RandomState(42).dirichlet(alpha, size=D)
imshow(theta)
# generate document-term matrix
doc_term_matrix = np.zeros((D, V), dtype=np.int64)
rng = np.random.RandomState(42)
for d in range(D):
    topic_histogram = rng.multinomial(N, theta[d])
    for k in range(K):
        doc_term_matrix[d] += rng.multinomial(topic_histogram[k], phi[k])
imshow(doc_term_matrix)<jupyter_output><empty_output><jupyter_text>Generate responses<jupyter_code># choose parameter values
mu = 0.
nu2 = 1.
eta = np.random.RandomState(4).normal(loc=mu, scale=nu2, size=K)
pd.Series(eta).plot(kind='bar')
# plot histogram of pre-responses
zeta = np.array([np.dot(eta, theta[d]) for d in range(D)])
pd.Series(zeta).hist(bins=50)
# set y
y = (zeta >= 0).astype(int)
# plot histogram of responses
pd.Series(y).hist()
plt.title('positive examples {} ({:.2f}%)'.format(y.sum(), y.sum() / D * 100))<jupyter_output><empty_output><jupyter_text>Estimate parameters<jupyter_code>import pstats, cProfile
from slda.topic_models import BLSLDA
_K = 10
_alpha = alpha
_beta = np.repeat(0.01, V)
_mu = mu
_nu2 = nu2
_b = 8
n_iter = 200
blslda = BLSLDA(_K, _alpha, _beta, _mu, _nu2, _b, n_iter, seed=42)
%%time
cProfile.runctx("blslda.fit(doc_term_matrix, y)", globals(), locals(), "Profile.prof")
s = pstats.Stats("Profile.prof")
s.strip_dirs().sort_stats("tottime").print_stats()<jupyter_output>Tue Aug 25 23:02:02 2015    Profile.prof

         20018581 function calls in 8.556 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    7.086    7.086    8.550    8.550 _topic_models.pyx:497(gibbs_sampler_blslda)
 20000000    1.295    0.000    1.295    0.000 _topic_models.pyx:64(searchsorted)
      200    0.131    0.001    0.131    0.001 _topic_models.pyx:151(loglikelihood_blslda)
        1    0.008    0.008    0.008    0.008 _topic_models.pyx:33(create_rands)
      200    0.005    0.000    0.012    0.000 linalg.py:296(solve)
     1015    0.004    0.000    0.004    0.000 {built-in method array}
        1    0.002    0.002    0.002    0.002 {method 'nonzero' of 'numpy.ndarray' objects}
        8    0.002    0.000    0.002    0.000 {method 'repeat' of 'numpy.ndarray' objects}
        1    0.002    0.002    0.002    0.002 _topic_models.pyx:48(create_topic_lookup)
      811    0.002    0.000    0.002    0.000 stringsou[...]<jupyter_text>other stuff<jupyter_code>%%time
blslda.fit(doc_term_matrix, y)
plot_images(plt, blslda.phi, (10, 10), (2, 5), figsize=(10, 5))
imshow(blslda.theta)
plt.plot(blslda.loglikelihoods)
burn_in = 100
eta_pred = blslda.eta[burn_in:].mean(axis=0)
print('mean log-likelihood {}'.format(blslda.loglikelihoods[burn_in:].mean()))
eta_pred<jupyter_output>mean log-likelihood -198916.3680838434
{'fraction_non_alphanumeric': 0.13803406035255453, 'fraction_numerical': 0.09023005676725426, 'mean_word_length': 4.095890410958904, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, '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': '383120', 'license': 'MIT', 'n_tokens_mistral': 1511, 'n_tokens_neox': 1323, 'n_words': 270, 'path': 'examples/blslda_profiling.ipynb', 'repo_name': 'jvahl/slda', 'size': '3347'}