Пример #1
0
def filter_none_from_frequency_count(frequency_count):
    """
    Filter out None rows from a frequency count.

    A frequency count is a list of tuples or a list of lists, in which the first
    element is the property, and the second element is the count.

    Parameters
    ----------
    frequency_count: list of lists or tuples with length 2

    Returns
    -------
    new_frequency_count: list of lists or tuples with length 2

    Example
    -------
    >> a = [ [None, 4], [1, 2], [2, 3], [10, 1] ]
    >> UtilsContainer.filter_none_from_frequency_count(a)
    [ [1, 2], [2, 3], [10, 1] ]
    >> a = [ [None, 4], ['None', 2], ['c', 3], ['d', 1] ]
    >> UtilsContainer.filter_none_from_frequency_count(a)
    [ ['None', 2], ['c', 3], ['d', 1] ]
    """

    Assert.frequency_count(frequency_count)

    new_frequency_count = [x for x in frequency_count if x[0] is not None]
    return new_frequency_count
Пример #2
0
def frequency_count2occurrence_list(frequency_count):
    """Transform a frequency count into an occurrence list.

    A frequency count is a list of tuples or a list of lists, in which the first
    element is the property, and the second element is the count. An occurrence
    list is a list with each property <count> times in it.

    Parameters
    ----------
    frequency_count: list of lists or tuples with length 2

    Returns
    -------
    occurrence_list: np.array, dtype = <type(most general key in frequency_count)>

    Example
    -------
    >> a = [ [0, 4], [1, 2], [2, 3], [10, 1] ]
    >> UtilsContainer.frequency_count2occurrence_list(a)
    [ 0  0  0  0  1  1  2  2  2 10]
    >> a = [ ['a', 4], ['b', 2], ['c', 3], ['d', 1] ]
    >> UtilsContainer.frequency_count2occurrence_list(a)
    ['a' 'a' 'a' 'a' 'b' 'b' 'c' 'c' 'c' 'd']
    """

    Assert.frequency_count(frequency_count)

    occurrence_list = np.empty(0)
    for k, v in frequency_count:
        if k is not None:
            k_list = np.empty(v, dtype=type(k))
            k_list.fill(k)
            occurrence_list = np.append(occurrence_list, k_list)

    return occurrence_list