<commit_before>"""
Various methods for binning real-valued data.
"""

from boltons.iterutils import pairwise

import numpy as np

__all__ = ['binned']

def binned(ts, bins=2, style='maxent'):
    """
    Discretize a real-valued list.

    Parameters
    ----------
    ts : ndarray
        The real-valued array to bin
    bins : int
        The number of bins to map the data into.
    style : str, {'maxent', 'uniform'}
        The method of discretizing the data. Defaults to 'maxent'.

    Returns
    -------
    symb : ndarray
        The discretized time-series.

    Raises
    ------
    ValueError
        Raised if `style` is not a recognized method.
    """
    if style == 'maxent':
        return maxent_binning(ts, bins)
    elif style == 'uniform':
        return uniform_binning(ts, bins)
    else:
        msg = "The style {} is not understood.".format(style)
        raise ValueError(msg)

def uniform_binning(ts, bins):
    """
    Discretizes the time-series in to equal-width bins.

    Parameters
    ----------
    ts : ndarray
        The real-valued array to bin
    bins : int
        The number of bins to map the data into.

    Returns
    -------
    symb : ndarray
        The discretized time-series.
    """
    symb = np.asarray(bins*(ts - ts.min())/(ts.max() - ts.min() + 1e-12), dtype=int)
    return symb

def maxent_binning(ts, bins):
    """

    Parameters
    ----------
    ts : ndarray
        The real-valued array to bin
    bins : int
        The number of bins to map the data into.

    Returns
    -------
    symb : ndarray
        The discretized time-series.
    """
    symb = ts.copy()
    percentiles = np.percentile(symb, [100*i/bins for i in range(bins+1)])
    percentiles[-1] += 1e-12
    for i, (a, b) in enumerate(pairwise(percentiles)):
        symb[(a <= ts) & (ts < b)] = i
    return symb<commit_msg>ensure that binning results in int, not float, dtypes.<commit_after>"""
Various methods for binning real-valued data.
"""

from boltons.iterutils import pairwise

import numpy as np

__all__ = ['binned']

def binned(ts, bins=2, style='maxent'):
    """
    Discretize a real-valued list.

    Parameters
    ----------
    ts : ndarray
        The real-valued array to bin
    bins : int
        The number of bins to map the data into.
    style : str, {'maxent', 'uniform'}
        The method of discretizing the data. Defaults to 'maxent'.

    Returns
    -------
    symb : ndarray
        The discretized time-series.

    Raises
    ------
    ValueError
        Raised if `style` is not a recognized method.
    """
    if style == 'maxent':
        return maxent_binning(ts, bins)
    elif style == 'uniform':
        return uniform_binning(ts, bins)
    else:
        msg = "The style {} is not understood.".format(style)
        raise ValueError(msg)

def uniform_binning(ts, bins):
    """
    Discretizes the time-series in to equal-width bins.

    Parameters
    ----------
    ts : ndarray
        The real-valued array to bin
    bins : int
        The number of bins to map the data into.

    Returns
    -------
    symb : ndarray
        The discretized time-series.
    """
    symb = np.asarray(bins*(ts - ts.min())/(ts.max() - ts.min() + 1e-12), dtype=int)
    return symb

def maxent_binning(ts, bins):
    """

    Parameters
    ----------
    ts : ndarray
        The real-valued array to bin
    bins : int
        The number of bins to map the data into.

    Returns
    -------
    symb : ndarray
        The discretized time-series.
    """
    symb = ts.copy()
    percentiles = np.percentile(symb, [100*i/bins for i in range(bins+1)])
    percentiles[-1] += 1e-12
    for i, (a, b) in enumerate(pairwise(percentiles)):
        symb[(a <= ts) & (ts < b)] = i
    symb = symb.astype(int)
    return symb
{'fraction_non_alphanumeric': 0.13121220089403102, 'fraction_numerical': 0.006310807257428346, 'mean_word_length': 1.9488372093023256, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '2065378', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1170, 'n_words': 387}