def contingency_matrix(labels_true, labels_pred, eps=None, max_n_classes=5000):
    """Build a contingency matrix describing the relationship between labels.

    Parameters
    ----------
    labels_true : int array, shape = [n_samples]
        Ground truth class labels to be used as a reference

    labels_pred : array, shape = [n_samples]
        Cluster labels to evaluate

    eps: None or float
        If a float, that value is added to all values in the contingency
        matrix. This helps to stop NaN propagation.
        If ``None``, nothing is adjusted.

    max_n_classes : int, optional (default=5000)
        Maximal number of classeses handled for contingency_matrix.
        This help to avoid Memory error with regression target
        for mutual_information.

    Returns
    -------
    contingency: array, shape=[n_classes_true, n_classes_pred]
        Matrix :math:`C` such that :math:`C_{i, j}` is the number of samples in
        true class :math:`i` and in predicted class :math:`j`. If
        ``eps is None``, the dtype of this array will be integer. If ``eps`` is
        given, the dtype will be float.
    """
    classes, class_idx = np.unique(labels_true, return_inverse=True)
    clusters, cluster_idx = np.unique(labels_pred, return_inverse=True)
    n_classes = classes.shape[0]
    n_clusters = clusters.shape[0]
    if n_classes > max_n_classes:
        raise ValueError("Too many classes for a clustering metric. If you "
                         "want to increase the limit, pass parameter "
                         "max_n_classes to the scoring function")
    if n_clusters > max_n_classes:
        raise ValueError("Too many clusters for a clustering metric. If you "
                         "want to increase the limit, pass parameter "
                         "max_n_classes to the scoring function")
    # Using coo_matrix to accelerate simple histogram calculation,
    # i.e. bins are consecutive integers
    # Currently, coo_matrix is faster than histogram2d for simple cases
    contingency = coo_matrix((np.ones(class_idx.shape[0]),
                              (class_idx, cluster_idx)),
                             shape=(n_classes, n_clusters),
                             dtype=np.int).toarray()
    if eps is not None:
        # don't use += as contingency is integer
        contingency = contingency + eps
    return contingency


# clustering measures
dataset/ETHPy150Open scikit-learn/scikit-learn/sklearn/metrics/cluster/supervised.py/contingency_matrix/original