import math


def entropy(U, N, weight=None):
    """ Returns information entropy of the assignment

    Args:
        U: assignment {class_id: [members,],}
        N: number of objects
        weight: weight factor {object_id: weight},]
    """
    H = 0
    for class_id, members in U.items():
        size = len(members)
        if not size:
            continue
        p = size / N
        if weight is not None:
            w = sum(weight[m] for m in members) / size
        else:
            w = 1
        H -= p * math.log2(p) * w
    return H