def gini_impurity(counts):
    """Computes Gini impurity of a set of counts.

    If used by a decision tree learning algorithm, the goal is to minimize the Gini impurity inside
    each leaf.

    Parameters:
        counts (collections.Counter)

    Example:

        >>> counts = {
        ...     'sunny': 4,
        ...     'rainy': 2,
        ...     'green': 10
        ... }
        >>> gini_impurity(counts)
        0.53125...

    References:
        1. `A Simple Explanation of Gini Impurity <https://victorzhou.com/blog/gini-impurity/>`_

    """
    N = sum(counts.values())
    return sum(c / N * (1 - c / N) for c in counts.values())