ó
‡ˆ\c           @   s  d  Z  d d l m Z d d l m Z d d l Z d d l Z d d l m	 Z
 d d l m Z d d	 l m Z d d
 l m Z d „  Z d „  Z d „  Z d e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d d „ Z d d „ Z e d „ Z d „  Z  d S(   s’   Utilities to evaluate the clustering performance of models.

Functions named as *_score return a scalar value to maximize: the higher the
better.
iÿÿÿÿ(   t   division(   t   logN(   t   sparsei   (   t   expected_mutual_informationi   (   t   check_array(   t   combc         C   s   t  |  d d d ƒS(   Ni   t   exacti   (   R   (   t   n(    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyt   _comb2   s    c         C   s±   t  j |  ƒ }  t  j | ƒ } |  j d k rF t d |  j f ƒ ‚ n  | j d k rn t d | j f ƒ ‚ n  |  j | j k r§ t d |  j d | j d f ƒ ‚ n  |  | f S(   sö   Check that the labels arrays are 1D and of same dimension.

    Parameters
    ----------
    labels_true : int array, shape = [n_samples]
        The true labels

    labels_pred : int array, shape = [n_samples]
        The predicted labels
    i   s#   labels_true must be 1D: shape is %rs#   labels_pred must be 1D: shape is %rs>   labels_true and labels_pred must have same size, got %d and %di    (   t   npt   asarrayt   ndimt
   ValueErrort   shape(   t   labels_truet   labels_pred(    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyt   check_clusterings$   s    !c         C   s~   | d k r t  |  | ƒ S| d k r6 t j |  | ƒ S| d k rU t j |  | g ƒ S| d k rn t |  | ƒ St d ƒ ‚ d S(   s(   Return a particular mean of two numbers.t   mint	   geometrict
   arithmetict   maxsC   'average_method' must be 'min', 'geometric', 'arithmetic', or 'max'N(   R   R	   t   sqrtt   meanR   R   (   t   Ut   Vt   average_method(    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyt   _generalized_average@   s    c         C   sù   | d k	 r! | r! t d ƒ ‚ n  t j |  d t ƒ\ } } t j | d t ƒ\ } } | j d } | j d }	 t j t j | j d ƒ | | f f d | |	 f d t j	 ƒ}
 | rÐ |
 j
 ƒ  }
 |
 j ƒ  n% |
 j ƒ  }
 | d k	 rõ |
 | }
 n  |
 S(   so  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, optional.
        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.

    sparse : boolean, optional.
        If True, return a sparse CSR continency matrix. If ``eps is not None``,
        and ``sparse is True``, will throw ValueError.

        .. versionadded:: 0.18

    Returns
    -------
    contingency : {array-like, sparse}, 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.
        Will be a ``scipy.sparse.csr_matrix`` if ``sparse=True``.
    s!   Cannot set 'eps' when sparse=Truet   return_inversei    R   t   dtypeN(   t   NoneR   R	   t   uniquet   TrueR   t   spt
   coo_matrixt   onest   intt   tocsrt   sum_duplicatest   toarray(   R   R   t   epsR   t   classest	   class_idxt   clusterst   cluster_idxt	   n_classest
   n_clusterst   contingency(    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyt   contingency_matrixO   s"     c         C   s\  t  |  | ƒ \ }  } |  j d } t j |  ƒ j d } t j | ƒ j d } | | k oe d k n s¢ | | k o d k n s¢ | | k o | k n r¦ d St |  | d t ƒ} t d „  t j | j d d ƒ ƒ Dƒ ƒ } t d „  t j | j d d ƒ ƒ Dƒ ƒ } t d „  | j Dƒ ƒ } | | t	 | ƒ }	 | | d	 }
 | |	 |
 |	 S(
   sá	  Rand index adjusted for chance.

    The Rand Index computes a similarity measure between two clusterings
    by considering all pairs of samples and counting pairs that are
    assigned in the same or different clusters in the predicted and
    true clusterings.

    The raw RI score is then "adjusted for chance" into the ARI score
    using the following scheme::

        ARI = (RI - Expected_RI) / (max(RI) - Expected_RI)

    The adjusted Rand index is thus ensured to have a value close to
    0.0 for random labeling independently of the number of clusters and
    samples and exactly 1.0 when the clusterings are identical (up to
    a permutation).

    ARI is a symmetric measure::

        adjusted_rand_score(a, b) == adjusted_rand_score(b, a)

    Read more in the :ref:`User Guide <adjusted_rand_score>`.

    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

    Returns
    -------
    ari : float
       Similarity score between -1.0 and 1.0. Random labelings have an ARI
       close to 0.0. 1.0 stands for perfect match.

    Examples
    --------

    Perfectly matching labelings have a score of 1 even

      >>> from sklearn.metrics.cluster import adjusted_rand_score
      >>> adjusted_rand_score([0, 0, 1, 1], [0, 0, 1, 1])
      1.0
      >>> adjusted_rand_score([0, 0, 1, 1], [1, 1, 0, 0])
      1.0

    Labelings that assign all classes members to the same clusters
    are complete be not always pure, hence penalized::

      >>> adjusted_rand_score([0, 0, 1, 2], [0, 0, 1, 1])  # doctest: +ELLIPSIS
      0.57...

    ARI is symmetric, so labelings that have pure clusters with members
    coming from the same classes but unnecessary splits are penalized::

      >>> adjusted_rand_score([0, 0, 1, 1], [0, 0, 1, 2])  # doctest: +ELLIPSIS
      0.57...

    If classes members are completely split across different clusters, the
    assignment is totally incomplete, hence the ARI is very low::

      >>> adjusted_rand_score([0, 0, 0, 0], [0, 1, 2, 3])
      0.0

    References
    ----------

    .. [Hubert1985] `L. Hubert and P. Arabie, Comparing Partitions,
      Journal of Classification 1985`
      http://link.springer.com/article/10.1007%2FBF01908075

    .. [wk] https://en.wikipedia.org/wiki/Rand_index#Adjusted_Rand_index

    See also
    --------
    adjusted_mutual_info_score: Adjusted Mutual Information

    i    i   g      ð?R   c         s   s   |  ] } t  | ƒ Vq d  S(   N(   R   (   t   .0t   n_c(    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pys	   <genexpr>ê   s    t   axisc         s   s   |  ] } t  | ƒ Vq d  S(   N(   R   (   R0   t   n_k(    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pys	   <genexpr>ë   s    c         s   s   |  ] } t  | ƒ Vq d  S(   N(   R   (   R0   t   n_ij(    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pys	   <genexpr>ì   s    g       @(
   R   R   R	   R   R/   R   t   sumt   ravelt   dataR   (   R   R   t	   n_samplesR,   R-   R.   t
   sum_comb_ct
   sum_comb_kt   sum_combt	   prod_combt	   mean_comb(    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyt   adjusted_rand_scoreŠ   s    Q++c   	      C   sÕ   t  |  | ƒ \ }  } t |  ƒ d k r+ d St |  ƒ } t | ƒ } t |  | d t ƒ} t d d d | ƒ} | r} | | n d } | r“ | | n d } | | d k r² d } n d | | | | } | | | f S(	   së  Compute the homogeneity and completeness and V-Measure scores at once.

    Those metrics are based on normalized conditional entropy measures of
    the clustering labeling to evaluate given the knowledge of a Ground
    Truth class labels of the same samples.

    A clustering result satisfies homogeneity if all of its clusters
    contain only data points which are members of a single class.

    A clustering result satisfies completeness if all the data points
    that are members of a given class are elements of the same cluster.

    Both scores have positive values between 0.0 and 1.0, larger values
    being desirable.

    Those 3 metrics are independent of the absolute values of the labels:
    a permutation of the class or cluster label values won't change the
    score values in any way.

    V-Measure is furthermore symmetric: swapping ``labels_true`` and
    ``label_pred`` will give the same score. This does not hold for
    homogeneity and completeness. V-Measure is identical to
    :func:`normalized_mutual_info_score` with the arithmetic averaging
    method.

    Read more in the :ref:`User Guide <homogeneity_completeness>`.

    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

    Returns
    -------
    homogeneity : float
       score between 0.0 and 1.0. 1.0 stands for perfectly homogeneous labeling

    completeness : float
       score between 0.0 and 1.0. 1.0 stands for perfectly complete labeling

    v_measure : float
        harmonic mean of the first two

    See also
    --------
    homogeneity_score
    completeness_score
    v_measure_score
    i    g      ð?R   R.   g        g       @(   g      ð?g      ð?g      ð?N(   R   t   lent   entropyR/   R   t   mutual_info_scoreR   (	   R   R   t	   entropy_Ct	   entropy_KR.   t   MIt   homogeneityt   completenesst   v_measure_score(    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyt"   homogeneity_completeness_v_measureó   s    5	c         C   s   t  |  | ƒ d S(   s	  Homogeneity metric of a cluster labeling given a ground truth.

    A clustering result satisfies homogeneity if all of its clusters
    contain only data points which are members of a single class.

    This metric is independent of the absolute values of the labels:
    a permutation of the class or cluster label values won't change the
    score value in any way.

    This metric is not symmetric: switching ``label_true`` with ``label_pred``
    will return the :func:`completeness_score` which will be different in
    general.

    Read more in the :ref:`User Guide <homogeneity_completeness>`.

    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

    Returns
    -------
    homogeneity : float
       score between 0.0 and 1.0. 1.0 stands for perfectly homogeneous labeling

    References
    ----------

    .. [1] `Andrew Rosenberg and Julia Hirschberg, 2007. V-Measure: A
       conditional entropy-based external cluster evaluation measure
       <http://aclweb.org/anthology/D/D07/D07-1043.pdf>`_

    See also
    --------
    completeness_score
    v_measure_score

    Examples
    --------

    Perfect labelings are homogeneous::

      >>> from sklearn.metrics.cluster import homogeneity_score
      >>> homogeneity_score([0, 0, 1, 1], [1, 1, 0, 0])
      1.0

    Non-perfect labelings that further split classes into more clusters can be
    perfectly homogeneous::

      >>> print("%.6f" % homogeneity_score([0, 0, 1, 1], [0, 0, 1, 2]))
      ...                                                  # doctest: +ELLIPSIS
      1.000000
      >>> print("%.6f" % homogeneity_score([0, 0, 1, 1], [0, 1, 2, 3]))
      ...                                                  # doctest: +ELLIPSIS
      1.000000

    Clusters that include samples from different classes do not make for an
    homogeneous labeling::

      >>> print("%.6f" % homogeneity_score([0, 0, 1, 1], [0, 1, 0, 1]))
      ...                                                  # doctest: +ELLIPSIS
      0.0...
      >>> print("%.6f" % homogeneity_score([0, 0, 1, 1], [0, 0, 0, 0]))
      ...                                                  # doctest: +ELLIPSIS
      0.0...

    i    (   RH   (   R   R   (    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyt   homogeneity_score?  s    Gc         C   s   t  |  | ƒ d S(   s±  Completeness metric of a cluster labeling given a ground truth.

    A clustering result satisfies completeness if all the data points
    that are members of a given class are elements of the same cluster.

    This metric is independent of the absolute values of the labels:
    a permutation of the class or cluster label values won't change the
    score value in any way.

    This metric is not symmetric: switching ``label_true`` with ``label_pred``
    will return the :func:`homogeneity_score` which will be different in
    general.

    Read more in the :ref:`User Guide <homogeneity_completeness>`.

    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

    Returns
    -------
    completeness : float
       score between 0.0 and 1.0. 1.0 stands for perfectly complete labeling

    References
    ----------

    .. [1] `Andrew Rosenberg and Julia Hirschberg, 2007. V-Measure: A
       conditional entropy-based external cluster evaluation measure
       <http://aclweb.org/anthology/D/D07/D07-1043.pdf>`_

    See also
    --------
    homogeneity_score
    v_measure_score

    Examples
    --------

    Perfect labelings are complete::

      >>> from sklearn.metrics.cluster import completeness_score
      >>> completeness_score([0, 0, 1, 1], [1, 1, 0, 0])
      1.0

    Non-perfect labelings that assign all classes members to the same clusters
    are still complete::

      >>> print(completeness_score([0, 0, 1, 1], [0, 0, 0, 0]))
      1.0
      >>> print(completeness_score([0, 1, 2, 3], [0, 0, 1, 1]))
      0.999...

    If classes members are split across different clusters, the
    assignment cannot be complete::

      >>> print(completeness_score([0, 0, 1, 1], [0, 1, 0, 1]))
      0.0
      >>> print(completeness_score([0, 0, 0, 0], [0, 1, 2, 3]))
      0.0

    i   (   RH   (   R   R   (    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyt   completeness_score‰  s    Cc         C   s   t  |  | ƒ d S(   sG  V-measure cluster labeling given a ground truth.

    This score is identical to :func:`normalized_mutual_info_score` with
    the ``'arithmetic'`` option for averaging.

    The V-measure is the harmonic mean between homogeneity and completeness::

        v = 2 * (homogeneity * completeness) / (homogeneity + completeness)

    This metric is independent of the absolute values of the labels:
    a permutation of the class or cluster label values won't change the
    score value in any way.

    This metric is furthermore symmetric: switching ``label_true`` with
    ``label_pred`` will return the same score value. This can be useful to
    measure the agreement of two independent label assignments strategies
    on the same dataset when the real ground truth is not known.


    Read more in the :ref:`User Guide <homogeneity_completeness>`.

    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

    Returns
    -------
    v_measure : float
       score between 0.0 and 1.0. 1.0 stands for perfectly complete labeling

    References
    ----------

    .. [1] `Andrew Rosenberg and Julia Hirschberg, 2007. V-Measure: A
       conditional entropy-based external cluster evaluation measure
       <http://aclweb.org/anthology/D/D07/D07-1043.pdf>`_

    See also
    --------
    homogeneity_score
    completeness_score
    normalized_mutual_info_score

    Examples
    --------

    Perfect labelings are both homogeneous and complete, hence have score 1.0::

      >>> from sklearn.metrics.cluster import v_measure_score
      >>> v_measure_score([0, 0, 1, 1], [0, 0, 1, 1])
      1.0
      >>> v_measure_score([0, 0, 1, 1], [1, 1, 0, 0])
      1.0

    Labelings that assign all classes members to the same clusters
    are complete be not homogeneous, hence penalized::

      >>> print("%.6f" % v_measure_score([0, 0, 1, 2], [0, 0, 1, 1]))
      ...                                                  # doctest: +ELLIPSIS
      0.8...
      >>> print("%.6f" % v_measure_score([0, 1, 2, 3], [0, 0, 1, 1]))
      ...                                                  # doctest: +ELLIPSIS
      0.66...

    Labelings that have pure clusters with members coming from the same
    classes are homogeneous but un-necessary splits harms completeness
    and thus penalize V-measure as well::

      >>> print("%.6f" % v_measure_score([0, 0, 1, 1], [0, 0, 1, 2]))
      ...                                                  # doctest: +ELLIPSIS
      0.8...
      >>> print("%.6f" % v_measure_score([0, 0, 1, 1], [0, 1, 2, 3]))
      ...                                                  # doctest: +ELLIPSIS
      0.66...

    If classes members are completely split across different clusters,
    the assignment is totally incomplete, hence the V-Measure is null::

      >>> print("%.6f" % v_measure_score([0, 0, 0, 0], [0, 1, 2, 3]))
      ...                                                  # doctest: +ELLIPSIS
      0.0...

    Clusters that include samples from totally different classes totally
    destroy the homogeneity of the labeling, hence::

      >>> print("%.6f" % v_measure_score([0, 0, 1, 1], [0, 0, 0, 0]))
      ...                                                  # doctest: +ELLIPSIS
      0.0...

    i   (   RH   (   R   R   (    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyRG   Ï  s    _c         C   sÈ  | d k r9 t |  | ƒ \ }  } t |  | d t ƒ} n0 t | d d d d g d t t j t j g ƒ} t	 | t j
 ƒ r£ t j | ƒ \ } } | | | f } n@ t j | ƒ rÍ t j | ƒ \ } } } n t d t | ƒ ƒ ‚ | j ƒ  } t j | j d d	 ƒ ƒ } t j | j d d
 ƒ ƒ } t j | ƒ }	 | | }
 | j | ƒ j t j ƒ | j | ƒ j t j ƒ } t j | ƒ t | j ƒ  ƒ t | j ƒ  ƒ } |
 |	 t | ƒ |
 | } | j ƒ  S(   sE  Mutual Information between two clusterings.

    The Mutual Information is a measure of the similarity between two labels of
    the same data. Where :math:`|U_i|` is the number of the samples
    in cluster :math:`U_i` and :math:`|V_j|` is the number of the
    samples in cluster :math:`V_j`, the Mutual Information
    between clusterings :math:`U` and :math:`V` is given as:

    .. math::

        MI(U,V)=\sum_{i=1}^{|U|} \sum_{j=1}^{|V|} \frac{|U_i\cap V_j|}{N}
        \log\frac{N|U_i \cap V_j|}{|U_i||V_j|}

    This metric is independent of the absolute values of the labels:
    a permutation of the class or cluster label values won't change the
    score value in any way.

    This metric is furthermore symmetric: switching ``label_true`` with
    ``label_pred`` will return the same score value. This can be useful to
    measure the agreement of two independent label assignments strategies
    on the same dataset when the real ground truth is not known.

    Read more in the :ref:`User Guide <mutual_info_score>`.

    Parameters
    ----------
    labels_true : int array, shape = [n_samples]
        A clustering of the data into disjoint subsets.

    labels_pred : array, shape = [n_samples]
        A clustering of the data into disjoint subsets.

    contingency : {None, array, sparse matrix},                   shape = [n_classes_true, n_classes_pred]
        A contingency matrix given by the :func:`contingency_matrix` function.
        If value is ``None``, it will be computed, otherwise the given value is
        used, with ``labels_true`` and ``labels_pred`` ignored.

    Returns
    -------
    mi : float
       Mutual information, a non-negative value

    See also
    --------
    adjusted_mutual_info_score: Adjusted against chance Mutual Information
    normalized_mutual_info_score: Normalized Mutual Information
    R   t   accept_sparset   csrt   csct   cooR   s&   Unsupported type for 'contingency': %sR2   i   i    N(   R   R   R/   R   R   R#   R	   t   int32t   int64t
   isinstancet   ndarrayt   nonzeroR    t   issparset   findR   t   typeR5   R6   R   t   taket   astype(   R   R   R.   t   nzxt   nzyt   nz_valt   contingency_sumt   pit   pjt   log_contingency_nmt   contingency_nmt   outert	   log_outert   mi(    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyRA   1  s.    1	
40t   warnc         C   s•  | d k r% t  j d t ƒ d } n  t |  | ƒ \ }  } |  j d } t j |  ƒ } t j | ƒ } | j d | j d k oŠ d k n s¹ | j d | j d k o´ d k n r½ d St |  | d t ƒ} | j	 t j
 ƒ } t |  | d | ƒ} t | | ƒ } t |  ƒ t | ƒ }	 }
 t |	 |
 | ƒ } | | } | d k  rht | t j d	 ƒ j ƒ } n t | t j d	 ƒ j ƒ } | | | } | S(
   sz  Adjusted Mutual Information between two clusterings.

    Adjusted Mutual Information (AMI) is an adjustment of the Mutual
    Information (MI) score to account for chance. It accounts for the fact that
    the MI is generally higher for two clusterings with a larger number of
    clusters, regardless of whether there is actually more information shared.
    For two clusterings :math:`U` and :math:`V`, the AMI is given as::

        AMI(U, V) = [MI(U, V) - E(MI(U, V))] / [avg(H(U), H(V)) - E(MI(U, V))]

    This metric is independent of the absolute values of the labels:
    a permutation of the class or cluster label values won't change the
    score value in any way.

    This metric is furthermore symmetric: switching ``label_true`` with
    ``label_pred`` will return the same score value. This can be useful to
    measure the agreement of two independent label assignments strategies
    on the same dataset when the real ground truth is not known.

    Be mindful that this function is an order of magnitude slower than other
    metrics, such as the Adjusted Rand Index.

    Read more in the :ref:`User Guide <mutual_info_score>`.

    Parameters
    ----------
    labels_true : int array, shape = [n_samples]
        A clustering of the data into disjoint subsets.

    labels_pred : array, shape = [n_samples]
        A clustering of the data into disjoint subsets.

    average_method : string, optional (default: 'warn')
        How to compute the normalizer in the denominator. Possible options
        are 'min', 'geometric', 'arithmetic', and 'max'.
        If 'warn', 'max' will be used. The default will change to
        'arithmetic' in version 0.22.

        .. versionadded:: 0.20

    Returns
    -------
    ami: float (upperlimited by 1.0)
       The AMI returns a value of 1 when the two partitions are identical
       (ie perfectly matched). Random partitions (independent labellings) have
       an expected AMI around 0 on average hence can be negative.

    See also
    --------
    adjusted_rand_score: Adjusted Rand Index
    mutual_info_score: Mutual Information (not adjusted for chance)

    Examples
    --------

    Perfect labelings are both homogeneous and complete, hence have
    score 1.0::

      >>> from sklearn.metrics.cluster import adjusted_mutual_info_score
      >>> adjusted_mutual_info_score([0, 0, 1, 1], [0, 0, 1, 1])
      ... # doctest: +SKIP
      1.0
      >>> adjusted_mutual_info_score([0, 0, 1, 1], [1, 1, 0, 0])
      ... # doctest: +SKIP
      1.0

    If classes members are completely split across different clusters,
    the assignment is totally in-complete, hence the AMI is null::

      >>> adjusted_mutual_info_score([0, 0, 0, 0], [0, 1, 2, 3])
      ... # doctest: +SKIP
      0.0

    References
    ----------
    .. [1] `Vinh, Epps, and Bailey, (2010). Information Theoretic Measures for
       Clusterings Comparison: Variants, Properties, Normalization and
       Correction for Chance, JMLR
       <http://jmlr.csail.mit.edu/papers/volume11/vinh10a/vinh10a.pdf>`_

    .. [2] `Wikipedia entry for the Adjusted Mutual Information
       <https://en.wikipedia.org/wiki/Adjusted_Mutual_Information>`_

    Rd   s‘   The behavior of AMI will change in version 0.22. To match the behavior of 'v_measure_score', AMI will use average_method='arithmetic' by default.R   i    i   g      ð?R   R.   t   float64(   t   warningsRd   t   FutureWarningR   R   R	   R   R/   R   RX   Re   RA   R   R@   R   R   t   finfoR'   R   (   R   R   R   R8   R(   R*   R.   Rc   t   emit   h_truet   h_predt
   normalizert   denominatort   ami(    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyt   adjusted_mutual_info_score‚  s0    V		**	
c         C   s@  | d k r% t  j d t ƒ d } n  t |  | ƒ \ }  } t j |  ƒ } t j | ƒ } | j d | j d k o} d k n s¬ | j d | j d k o§ d k n r° d St |  | d t ƒ} | j	 t j
 ƒ } t |  | d | ƒ} t |  ƒ t | ƒ } } t | | | ƒ }	 t |	 t j d	 ƒ j ƒ }	 | |	 }
 |
 S(
   s.
  Normalized Mutual Information between two clusterings.

    Normalized Mutual Information (NMI) is a normalization of the Mutual
    Information (MI) score to scale the results between 0 (no mutual
    information) and 1 (perfect correlation). In this function, mutual
    information is normalized by some generalized mean of ``H(labels_true)``
    and ``H(labels_pred))``, defined by the `average_method`.

    This measure is not adjusted for chance. Therefore
    :func:`adjusted_mutual_info_score` might be preferred.

    This metric is independent of the absolute values of the labels:
    a permutation of the class or cluster label values won't change the
    score value in any way.

    This metric is furthermore symmetric: switching ``label_true`` with
    ``label_pred`` will return the same score value. This can be useful to
    measure the agreement of two independent label assignments strategies
    on the same dataset when the real ground truth is not known.

    Read more in the :ref:`User Guide <mutual_info_score>`.

    Parameters
    ----------
    labels_true : int array, shape = [n_samples]
        A clustering of the data into disjoint subsets.

    labels_pred : array, shape = [n_samples]
        A clustering of the data into disjoint subsets.

    average_method : string, optional (default: 'warn')
        How to compute the normalizer in the denominator. Possible options
        are 'min', 'geometric', 'arithmetic', and 'max'.
        If 'warn', 'geometric' will be used. The default will change to
        'arithmetic' in version 0.22.

        .. versionadded:: 0.20

    Returns
    -------
    nmi : float
       score between 0.0 and 1.0. 1.0 stands for perfectly complete labeling

    See also
    --------
    v_measure_score: V-Measure (NMI with arithmetic mean option.)
    adjusted_rand_score: Adjusted Rand Index
    adjusted_mutual_info_score: Adjusted Mutual Information (adjusted
        against chance)

    Examples
    --------

    Perfect labelings are both homogeneous and complete, hence have
    score 1.0::

      >>> from sklearn.metrics.cluster import normalized_mutual_info_score
      >>> normalized_mutual_info_score([0, 0, 1, 1], [0, 0, 1, 1])
      ... # doctest: +SKIP
      1.0
      >>> normalized_mutual_info_score([0, 0, 1, 1], [1, 1, 0, 0])
      ... # doctest: +SKIP
      1.0

    If classes members are completely split across different clusters,
    the assignment is totally in-complete, hence the NMI is null::

      >>> normalized_mutual_info_score([0, 0, 0, 0], [0, 1, 2, 3])i
      ... # doctest: +SKIP
      0.0

    Rd   s‘   The behavior of NMI will change in version 0.22. To match the behavior of 'v_measure_score', NMI will use average_method='arithmetic' by default.R   i    i   g      ð?R   R.   Re   (   Rf   Rd   Rg   R   R	   R   R   R/   R   RX   Re   RA   R@   R   R   Rh   R'   (   R   R   R   R(   R*   R.   Rc   Rj   Rk   Rl   t   nmi(    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyt   normalized_mutual_info_scoreþ  s&    J		**	
c         C   sô   t  |  | ƒ \ }  } |  j \ } t |  | d t ƒj t j ƒ } t j | j | j ƒ | } t j	 t j
 | j	 d d ƒ ƒ j ƒ  d ƒ | } t j	 t j
 | j	 d d ƒ ƒ j ƒ  d ƒ | } | d k rð t j | | ƒ t j | | ƒ Sd S(   sÄ  Measure the similarity of two clusterings of a set of points.

    The Fowlkes-Mallows index (FMI) is defined as the geometric mean between of
    the precision and recall::

        FMI = TP / sqrt((TP + FP) * (TP + FN))

    Where ``TP`` is the number of **True Positive** (i.e. the number of pair of
    points that belongs in the same clusters in both ``labels_true`` and
    ``labels_pred``), ``FP`` is the number of **False Positive** (i.e. the
    number of pair of points that belongs in the same clusters in
    ``labels_true`` and not in ``labels_pred``) and ``FN`` is the number of
    **False Negative** (i.e the number of pair of points that belongs in the
    same clusters in ``labels_pred`` and not in ``labels_True``).

    The score ranges from 0 to 1. A high value indicates a good similarity
    between two clusters.

    Read more in the :ref:`User Guide <fowlkes_mallows_scores>`.

    Parameters
    ----------
    labels_true : int array, shape = (``n_samples``,)
        A clustering of the data into disjoint subsets.

    labels_pred : array, shape = (``n_samples``, )
        A clustering of the data into disjoint subsets.

    sparse : bool
        Compute contingency matrix internally with sparse matrix.

    Returns
    -------
    score : float
       The resulting Fowlkes-Mallows score.

    Examples
    --------

    Perfect labelings are both homogeneous and complete, hence have
    score 1.0::

      >>> from sklearn.metrics.cluster import fowlkes_mallows_score
      >>> fowlkes_mallows_score([0, 0, 1, 1], [0, 0, 1, 1])
      1.0
      >>> fowlkes_mallows_score([0, 0, 1, 1], [1, 1, 0, 0])
      1.0

    If classes members are completely split across different clusters,
    the assignment is totally random, hence the FMI is null::

      >>> fowlkes_mallows_score([0, 0, 0, 0], [0, 1, 2, 3])
      0.0

    References
    ----------
    .. [1] `E. B. Fowkles and C. L. Mallows, 1983. "A method for comparing two
       hierarchical clusterings". Journal of the American Statistical
       Association
       <http://wildfire.stat.ucla.edu/pdflibrary/fowlkes.pdf>`_

    .. [2] `Wikipedia entry for the Fowlkes-Mallows Index
           <https://en.wikipedia.org/wiki/Fowlkes-Mallows_index>`_
    R   R2   i    i   i   g        (   R   R   R/   R   RX   R	   RP   t   dotR7   R5   R
   R6   R   (   R   R   R   R8   t   ct   tkt   pkt   qk(    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyt   fowlkes_mallows_scoree  s    A22c         C   s’   t  |  ƒ d k r d St j |  d t ƒd } t j | ƒ j t j ƒ } | | d k } t j | ƒ } t j | | t j | ƒ t | ƒ ƒ S(   s‰   Calculates the entropy for a labeling.

    Parameters
    ----------
    labels : int array, shape = [n_samples]
        The labels
    i    g      ð?R   i   (	   R?   R	   R   R   t   bincountRX   Re   R5   R   (   t   labelst	   label_idxR]   t   pi_sum(    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyR@   ±  s    (!   t   __doc__t
   __future__R    t   mathR   Rf   t   numpyR	   t   scipyR   R    t   expected_mutual_info_fastR   t   utils.validationR   t   utils.fixesR   R   R   R   R   t   FalseR/   R>   RH   RI   RJ   RG   RA   Ro   Rq   Rw   R@   (    (    (    sA   lib/python2.7/site-packages/sklearn/metrics/cluster/supervised.pyt   <module>   s,   			;	i	L	J	F	bR|fL