<commit_before>import datetime

import numpy as np
import scipy.misc
import matplotlib.pyplot as plt

class bmm:

    def __init__(self, k, x, d):

        self.k = k
        self.d = d
        self.x = x
        self.xc = 1 - x

        self.n = self.x.shape[0]
        self.pi = np.array([1 / self.k for _ in range(self.k)])
        self.z = np.ndarray(shape=(self.k, self.n))
        self.mu = np.ndarray(shape=(self.k, self.d))

        # initialization of mu
        for k in range(self.k):
            for i in range(self.d):
                self.mu[k,i] = np.random.random() * 0.5 + 0.25

    def data_mean_init(self):

        mean = self.x.mean(0)

        for k in range(self.k):
            for i in range(self.d):
                self.mu[k, i] = mean[i] + np.random.random()

    def data_classes_mean_init(self, data_labels):

        labels = set(data_labels)

        assert self.k == len(labels), 'k must match the number of labels'

        for l in labels:
            matches = np.in1d(data_labels, l)
            mean = self.x[matches].mean(0)
            self.mu[l] = mean

    def fit(self):

        start = datetime.datetime.now()

        iterations = 0

        previous_log_likelihood = -np.inf
        log_likelihood = previous_log_likelihood + 1

        while iterations == 0 or (
                abs(previous_log_likelihood - log_likelihood) > 1e-5 \
                and previous_log_likelihood < log_likelihood):

            print('\riteration {} (elapsed {})'
                  .format(iterations, datetime.datetime.now() - start), end='')

            log_support = self._log_support()
            previous_log_likelihood = log_likelihood
            log_likelihood = self.log_likelihood(log_support)

            self.expectation_step(log_support)
            self.maximization_step()

            iterations += 1

        end = datetime.datetime.now()

        elapsed = end - start

        print('\r > converged in {} iterations in {}'
              .format(iterations, elapsed))


    def plot_mu(self):

        rows = self.k // 5 + 1
        columns = min(self.k, 5)

        for k in range(self.k):
            plt.subplot(rows, columns, k + 1)
            plt.imshow(scipy.misc.toimage(self.mu[k].reshape(28, 28),
                                          cmin=0.0, cmax=1.0))

    def data_mean_init(self):<commit_msg>(bmm) previous_llk -> prev_llk<commit_after>import datetime

import numpy as np
import scipy.misc
import matplotlib.pyplot as plt

class bmm:

    def __init__(self, k, x, d):

        self.k = k
        self.d = d
        self.x = x
        self.xc = 1 - x

        self.n = self.x.shape[0]
        self.pi = np.array([1 / self.k for _ in range(self.k)])
        self.z = np.ndarray(shape=(self.k, self.n))
        self.mu = np.ndarray(shape=(self.k, self.d))

        # initialization of mu
        for k in range(self.k):
            for i in range(self.d):
                self.mu[k,i] = np.random.random() * 0.5 + 0.25

    def data_mean_init(self):

        mean = self.x.mean(0)

        for k in range(self.k):
            for i in range(self.d):
                self.mu[k, i] = mean[i] + np.random.random()

    def data_classes_mean_init(self, data_labels):

        labels = set(data_labels)

        assert self.k == len(labels), 'k must match the number of labels'

        for l in labels:
            matches = np.in1d(data_labels, l)
            mean = self.x[matches].mean(0)
            self.mu[l] = mean

    def fit(self):

        start = datetime.datetime.now()

        iterations = 0

        prev_log_likelihood = -np.inf
        log_likelihood = prev_log_likelihood + 1

        while iterations == 0 or (
                abs(prev_log_likelihood - log_likelihood) > 1e-5 \
                and prev_log_likelihood < log_likelihood):

            print('\riteration {} (elapsed {})'
                  .format(iterations, datetime.datetime.now() - start), end='')

            log_support = self._log_support()
            prev_log_likelihood = log_likelihood
            log_likelihood = self.log_likelihood(log_support)

            self.expectation_step(log_support)
            self.maximization_step()

            iterations += 1

        end = datetime.datetime.now()

        elapsed = end - start

        print('\r > converged in {} iterations in {}'
              .format(iterations, elapsed))


    def plot_mu(self):

        rows = self.k // 5 + 1
        columns = min(self.k, 5)

        for k in range(self.k):
            plt.subplot(rows, columns, k + 1)
            plt.imshow(scipy.misc.toimage(self.mu[k].reshape(28, 28),
                                          cmin=0.0, cmax=1.0))

    def data_mean_init(self):
{'fraction_non_alphanumeric': 0.11814524666525514, 'fraction_numerical': 0.012280330298539064, 'mean_word_length': 1.7196315486470928, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4746864', 'n_tokens_mistral': 1586, 'n_tokens_neox': 1485, 'n_words': 343}