def cosine_similarity(F_a, F_b):
    """
    Calculate `cosine similarity
    <http://en.wikipedia.org/wiki/Cosine_similarity>`_ for sparse feature
    vectors.

    Parameters
    ----------
    F_a : :class:`.Feature`
    F_b : :class:`.Feature`

    Returns
    -------
    similarity : float
        Cosine similarity.
    """

    shared = list(F_a.unique & F_b.unique)
    A = [dict(F_a.norm)[i] for i in shared]
    B = [dict(F_b.norm)[i] for i in shared]
    dot = sum(map(lambda a, b: a*b, A, B))
    mag_A = sqrt(sum(map(lambda a: a**2, A)))
    mag_B = sqrt(sum(map(lambda a: a**2, B)))

    return dot / (mag_A + mag_B)