<commit_before>from libact.base.interfaces import QueryStrategy
from libact.models import LogisticRegression
import numpy as np


class UncertaintySampling(QueryStrategy):

    def __init__(self, *args, **kwargs):
        """Currently only LogisticRegression is supported."""
        super(UncertaintySampling, self).__init__(*args, **kwargs)
        self.model = LogisticRegression()
        self.method = kwargs.pop('method', 'le')

    def update(self, entry_id, label):
        # TODO
        pass

    def make_query(self):
        """
        Three choices for method (default 'le'):
        'lc' (Least Confident), 'sm' (Smallest Margin), 'le' (Label Entropy)
        """
        dataset = self.dataset
        self.model.train(dataset)

        unlabeled_entry_ids, X_pool = zip(*dataset.get_unlabeled_entries())

        if self.method == 'lc':  # least confident
            # time complexity analysis:
            # self.model.predict_real(X_pool) -> O(NK)
            # np.max(..., axis=1) -> O(NK)
            # 1 - np.max(..., axis=1) -> O(NK)
            # np.argmax(...) -> O(N)
            # therefore, total time complexity is O(NK) + O(NK) + O(NK) + O(N) = O(NK)
            ask_id = np.argmax(1 - np.max(self.model.predict_real(X_pool), 1))

        elif self.method == 'sm':  # smallest margin<commit_msg>us document done<commit_after>from libact.base.interfaces import QueryStrategy
from libact.models import LogisticRegression
import numpy as np


class UncertaintySampling(QueryStrategy):
    """Uncertainty Sampling

    This class implements Uncertainty Sampling active learning algorithm [1]_.

    Parameters
    ----------
    model: {libact.model.LogisticRegression instance, 'LogisticRegression'}
        The based model used for training.
        Only Logistic regression are supported now.

    method: {'lc', 'sm', 'le'}
        lc stands for least confidence, it queries the instance whose posterior
        probability of being positive is nearest 0.5 (for binary classification);
        sm stands for smallest margin, it queries the instance whose posterior
        probability gap between the most and the second probable labels is minimal;
        le stands for the common entropy approach.

    Attributes
    ----------


    References
    ----------

    .. [1] <NAME>. "Active learning literature survey." University of
           Wisconsin, Madison 52.55-66 (2010): 11.
    """

    def __init__(self, *args, **kwargs):
        """Currently only LogisticRegression is supported."""
        super(UncertaintySampling, self).__init__(*args, **kwargs)
        self.model = LogisticRegression()
        self.method = kwargs.pop('method', 'le')

    def update(self, entry_id, label):
        # TODO
        pass

    def make_query(self):
        """
        Three choices for method (default 'le'):
        'lc' (Least Confident), 'sm' (Smallest Margin), 'le' (Label Entropy)
        """
        dataset = self.dataset
        self.model.train(dataset)

        unlabeled_entry_ids, X_pool = zip(*dataset.get_unlabeled_entries())

        if self.method == 'lc':  # least confident
            # time complexity analysis:
            # self.model.predict_real(X_pool) -> O(NK)
            # np.max(..., axis=1) -> O(NK)
            # 1 - np.max(..., axis=1) -> O(NK)
            # np.argmax(...) -> O(N)
            # therefore, total time complexity is O(NK) + O(NK) + O(NK) + O(N) = O(NK)
            ask_id = np.argmax(1 - np.max(self.model.predict_real(X_pool), 1))

        elif self.method == 'sm':  # smallest margin
{'fraction_non_alphanumeric': 0.12927650028042625, 'fraction_numerical': 0.007291082445316881, 'mean_word_length': 2.3971428571428572, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 12, '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}}
{'dir': 'git-commits-cleaned', 'id': '246217', 'n_tokens_mistral': 1077, 'n_tokens_neox': 1016, 'n_words': 295}