def gini(counts):
    """Calculate the Gini Impurity for a list of counts.

    There are a few different ways to do this, I thought this one was
    the most concise. See:
    https://en.wikipedia.org/wiki/Decision_tree_learning#Gini_impurity
    Smaller the purer
    """
    impurity = 1
    sum_count = sum([val for val in counts.values()])
    for lbl in counts:
        prob_of_lbl = counts[lbl] / float(sum_count)
        impurity -= prob_of_lbl ** 2
    return impurity