ó
‡ˆ\c           @   sê   d  Z  d d l Z d d l Z d d l m Z m Z d d l m Z m	 Z	 d d l
 m Z d d l m Z d d l m Z d d	 l m Z d
 d l m Z e d d d d „ Z d d d d d d d d „ Z d e e f d „  ƒ  YZ d S(   s"   Algorithms for spectral clusteringiÿÿÿÿNi   (   t   BaseEstimatort   ClusterMixin(   t   check_random_statet   as_float_array(   t   check_array(   t   pairwise_kernels(   t   kneighbors_graph(   t   spectral_embeddingi   (   t   k_meansi   i   c      	   C   sª  d d l  m } d d l m } t | ƒ } t |  d | ƒ}  t j t ƒ j	 } |  j
 \ } }	 t j | ƒ }
 x¿ t |  j
 d ƒ D]ª } |  d d … | f t j j |  d d … | f ƒ |
 |  d d … | f <|  d | f d k r‚ d |  d d … | f t j |  d | f ƒ |  d d … | f <q‚ q‚ W|  t j |  d j d	 d ƒ ƒ d d … t j f }  d } t } x| | k  r| rt j |	 |	 f ƒ } |  | j | ƒ d d … f j | d d … d f <t j | ƒ } xy t d |	 ƒ D]h } | t j t j |  | d d … | d f ƒ ƒ 7} |  | j ƒ  d d … f j | d d … | f <qîWd
 } d } x$| sŒ| d 7} t j |  | ƒ } | j d	 d ƒ } | t j t | ƒ ƒ t j d | ƒ | f f d | |	 f ƒ} | j |  } y) t j j | ƒ \ } } } | d 7} Wn | k
 r+d GHPn Xd | | j ƒ  } t | | ƒ | k  sb| | k rkt } qi| } t j | j | j ƒ } qiWquW| s¦| d ƒ ‚ n  | S(   s[  Search for a partition matrix (clustering) which is closest to the
    eigenvector embedding.

    Parameters
    ----------
    vectors : array-like, shape: (n_samples, n_clusters)
        The embedding space of the samples.

    copy : boolean, optional, default: True
        Whether to copy vectors, or perform in-place normalization.

    max_svd_restarts : int, optional, default: 30
        Maximum number of attempts to restart SVD if convergence fails

    n_iter_max : int, optional, default: 30
        Maximum number of iterations to attempt in rotation and partition
        matrix search if machine precision convergence is not reached

    random_state : int, RandomState instance or None (default)
        Determines random number generation for rotation matrix initialization.
        Use an int to make the randomness deterministic.
        See :term:`Glossary <random_state>`.

    Returns
    -------
    labels : array of integers, shape: n_samples
        The labels of the clusters.

    References
    ----------

    - Multiclass spectral clustering, 2003
      Stella X. Yu, Jianbo Shi
      http://www1.icsi.berkeley.edu/~stellayu/publication/doc/2003kwayICCV.pdf

    Notes
    -----

    The eigenvector embedding is used to iteratively search for the
    closest discrete partition.  First, the eigenvector embedding is
    normalized to the space of partition matrices. An optimal discrete
    partition matrix closest to this normalized embedding multiplied by
    an initial rotation is calculated.  Fixing this discrete partition
    matrix, an optimal rotation matrix is calculated.  These two
    calculations are performed until convergence.  The discrete partition
    matrix is returned as the clustering solution.  Used in spectral
    clustering, this method tends to be faster and more robust to random
    initialization than k-means.

    iÿÿÿÿ(   t
   csc_matrix(   t   LinAlgErrort   copyi   Ni    i   t   axisg        t   shapes2   SVD did not converge, randomizing and trying againg       @s   SVD did not converge(   t   scipy.sparseR	   t   scipy.linalgR
   R   R   t   npt   finfot   floatt   epsR   t   sqrtt   ranget   linalgt   normt   signt   sumt   newaxist   Falset   zerost   randintt   Tt   abst   dott   argmint   argmaxt   onest   lent   aranget   svdt   True(   t   vectorsR   t   max_svd_restartst
   n_iter_maxt   random_stateR	   R
   R   t	   n_samplest   n_componentst	   norm_onest   it   svd_restartst   has_convergedt   rotationt   ct   jt   last_objective_valuet   n_itert
   t_discretet   labelst   vectors_discretet   t_svdt   Ut   St   Vht
   ncut_value(    (    s7   lib/python2.7/site-packages/sklearn/cluster/spectral.pyt
   discretize   s^    53H6233	
-	 i   i
   g        t   kmeansc         C   s¶   | d
 k r t  d | ƒ ‚ n  t | ƒ } | d k r= | n | } t |  d | d | d | d | d t ƒ} | d k r  t | | d | d	 | ƒ\ }	 }
 }	 n t | d | ƒ}
 |
 S(   sí  Apply clustering to a projection to the normalized laplacian.

    In practice Spectral Clustering is very useful when the structure of
    the individual clusters is highly non-convex or more generally when
    a measure of the center and spread of the cluster is not a suitable
    description of the complete cluster. For instance when clusters are
    nested circles on the 2D plan.

    If affinity is the adjacency matrix of a graph, this method can be
    used to find normalized graph cuts.

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

    Parameters
    -----------
    affinity : array-like or sparse matrix, shape: (n_samples, n_samples)
        The affinity matrix describing the relationship of the samples to
        embed. **Must be symmetric**.

        Possible examples:
          - adjacency matrix of a graph,
          - heat kernel of the pairwise distance matrix of the samples,
          - symmetric k-nearest neighbours connectivity matrix of the samples.

    n_clusters : integer, optional
        Number of clusters to extract.

    n_components : integer, optional, default is n_clusters
        Number of eigen vectors to use for the spectral embedding

    eigen_solver : {None, 'arpack', 'lobpcg', or 'amg'}
        The eigenvalue decomposition strategy to use. AMG requires pyamg
        to be installed. It can be faster on very large, sparse problems,
        but may also lead to instabilities

    random_state : int, RandomState instance or None (default)
        A pseudo random number generator used for the initialization of the
        lobpcg eigen vectors decomposition when eigen_solver == 'amg' and by
        the K-Means initialization. Use an int to make the randomness
        deterministic.
        See :term:`Glossary <random_state>`.

    n_init : int, optional, default: 10
        Number of time the k-means algorithm will be run with different
        centroid seeds. The final results will be the best output of
        n_init consecutive runs in terms of inertia.

    eigen_tol : float, optional, default: 0.0
        Stopping criterion for eigendecomposition of the Laplacian matrix
        when using arpack eigen_solver.

    assign_labels : {'kmeans', 'discretize'}, default: 'kmeans'
        The strategy to use to assign labels in the embedding
        space.  There are two ways to assign labels after the laplacian
        embedding.  k-means can be applied and is a popular choice. But it can
        also be sensitive to initialization. Discretization is another
        approach which is less sensitive to random initialization. See
        the 'Multiclass spectral clustering' paper referenced below for
        more details on the discretization approach.

    Returns
    -------
    labels : array of integers, shape: n_samples
        The labels of the clusters.

    References
    ----------

    - Normalized cuts and image segmentation, 2000
      Jianbo Shi, Jitendra Malik
      http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.160.2324

    - A Tutorial on Spectral Clustering, 2007
      Ulrike von Luxburg
      http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.165.9323

    - Multiclass spectral clustering, 2003
      Stella X. Yu, Jianbo Shi
      http://www1.icsi.berkeley.edu/~stellayu/publication/doc/2003kwayICCV.pdf

    Notes
    ------
    The graph should contain only one connect component, elsewhere
    the results make little sense.

    This algorithm solves the normalized cut for k=2: it is a
    normalized spectral clustering.
    R@   R?   sT   The 'assign_labels' parameter should be 'kmeans' or 'discretize', but '%s' was givenR-   t   eigen_solverR+   t	   eigen_tolt
   drop_firstt   n_init(   R@   R?   N(   t
   ValueErrorR   t   NoneR   R   R   R?   (   t   affinityt
   n_clustersR-   RA   R+   RD   RB   t   assign_labelst   mapst   _R8   (    (    s7   lib/python2.7/site-packages/sklearn/cluster/spectral.pyt   spectral_clusteringŸ   s    [t   SpectralClusteringc           B   sY   e  Z d  Z d d d d d d d d d d d d d d	 „ Z d d
 „ Z e d „  ƒ Z RS(   sp  Apply clustering to a projection to the normalized laplacian.

    In practice Spectral Clustering is very useful when the structure of
    the individual clusters is highly non-convex or more generally when
    a measure of the center and spread of the cluster is not a suitable
    description of the complete cluster. For instance when clusters are
    nested circles on the 2D plan.

    If affinity is the adjacency matrix of a graph, this method can be
    used to find normalized graph cuts.

    When calling ``fit``, an affinity matrix is constructed using either
    kernel function such the Gaussian (aka RBF) kernel of the euclidean
    distanced ``d(X, X)``::

            np.exp(-gamma * d(X,X) ** 2)

    or a k-nearest neighbors connectivity matrix.

    Alternatively, using ``precomputed``, a user-provided affinity
    matrix can be used.

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

    Parameters
    -----------
    n_clusters : integer, optional
        The dimension of the projection subspace.

    eigen_solver : {None, 'arpack', 'lobpcg', or 'amg'}
        The eigenvalue decomposition strategy to use. AMG requires pyamg
        to be installed. It can be faster on very large, sparse problems,
        but may also lead to instabilities

    random_state : int, RandomState instance or None (default)
        A pseudo random number generator used for the initialization of the
        lobpcg eigen vectors decomposition when eigen_solver == 'amg' and by
        the K-Means initialization. Use an int to make the randomness
        deterministic.
        See :term:`Glossary <random_state>`.

    n_init : int, optional, default: 10
        Number of time the k-means algorithm will be run with different
        centroid seeds. The final results will be the best output of
        n_init consecutive runs in terms of inertia.

    gamma : float, default=1.0
        Kernel coefficient for rbf, poly, sigmoid, laplacian and chi2 kernels.
        Ignored for ``affinity='nearest_neighbors'``.

    affinity : string, array-like or callable, default 'rbf'
        If a string, this may be one of 'nearest_neighbors', 'precomputed',
        'rbf' or one of the kernels supported by
        `sklearn.metrics.pairwise_kernels`.

        Only kernels that produce similarity scores (non-negative values that
        increase with similarity) should be used. This property is not checked
        by the clustering algorithm.

    n_neighbors : integer
        Number of neighbors to use when constructing the affinity matrix using
        the nearest neighbors method. Ignored for ``affinity='rbf'``.

    eigen_tol : float, optional, default: 0.0
        Stopping criterion for eigendecomposition of the Laplacian matrix
        when using arpack eigen_solver.

    assign_labels : {'kmeans', 'discretize'}, default: 'kmeans'
        The strategy to use to assign labels in the embedding
        space. There are two ways to assign labels after the laplacian
        embedding. k-means can be applied and is a popular choice. But it can
        also be sensitive to initialization. Discretization is another approach
        which is less sensitive to random initialization.

    degree : float, default=3
        Degree of the polynomial kernel. Ignored by other kernels.

    coef0 : float, default=1
        Zero coefficient for polynomial and sigmoid kernels.
        Ignored by other kernels.

    kernel_params : dictionary of string to any, optional
        Parameters (keyword arguments) and values for kernel passed as
        callable object. Ignored by other kernels.

    n_jobs : int or None, optional (default=None)
        The number of parallel jobs to run.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

    Attributes
    ----------
    affinity_matrix_ : array-like, shape (n_samples, n_samples)
        Affinity matrix used for clustering. Available only if after calling
        ``fit``.

    labels_ :
        Labels of each point

    Examples
    --------
    >>> from sklearn.cluster import SpectralClustering
    >>> import numpy as np
    >>> X = np.array([[1, 1], [2, 1], [1, 0],
    ...               [4, 7], [3, 5], [3, 6]])
    >>> clustering = SpectralClustering(n_clusters=2,
    ...         assign_labels="discretize",
    ...         random_state=0).fit(X)
    >>> clustering.labels_
    array([1, 1, 1, 0, 0, 0])
    >>> clustering # doctest: +NORMALIZE_WHITESPACE
    SpectralClustering(affinity='rbf', assign_labels='discretize', coef0=1,
              degree=3, eigen_solver=None, eigen_tol=0.0, gamma=1.0,
              kernel_params=None, n_clusters=2, n_init=10, n_jobs=None,
              n_neighbors=10, random_state=0)

    Notes
    -----
    If you have an affinity matrix, such as a distance matrix,
    for which 0 means identical elements, and high values means
    very dissimilar elements, it can be transformed in a
    similarity matrix that is well suited for the algorithm by
    applying the Gaussian (RBF, heat) kernel::

        np.exp(- dist_matrix ** 2 / (2. * delta ** 2))

    Where ``delta`` is a free parameter representing the width of the Gaussian
    kernel.

    Another alternative is to take a symmetric version of the k
    nearest neighbors connectivity matrix of the points.

    If the pyamg package is installed, it is used: this greatly
    speeds up computation.

    References
    ----------

    - Normalized cuts and image segmentation, 2000
      Jianbo Shi, Jitendra Malik
      http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.160.2324

    - A Tutorial on Spectral Clustering, 2007
      Ulrike von Luxburg
      http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.165.9323

    - Multiclass spectral clustering, 2003
      Stella X. Yu, Jianbo Shi
      http://www1.icsi.berkeley.edu/~stellayu/publication/doc/2003kwayICCV.pdf
    i   i
   g      ð?t   rbfg        R@   i   i   c         C   sy   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ |	 |  _ |
 |  _	 | |  _
 | |  _ | |  _ d  S(   N(   RH   RA   R+   RD   t   gammaRG   t   n_neighborsRB   RI   t   degreet   coef0t   kernel_paramst   n_jobs(   t   selfRH   RA   R+   RD   RO   RG   RP   RB   RI   RQ   RR   RS   RT   (    (    s7   lib/python2.7/site-packages/sklearn/cluster/spectral.pyt   __init__¬  s    												c         C   s˜  t  | d d d d g d t j d d ƒ} | j d | j d	 k rc |  j d
 k rc t j d ƒ n  |  j d k r­ t | d |  j d t	 d |  j
 ƒ} d | | j |  _ n“ |  j d
 k rÈ | |  _ nx |  j } | d k ræ i  } n  t |  j ƒ s|  j | d <|  j | d <|  j | d <n  t | d |  j d t	 | |  _ t |  j ƒ } t |  j d |  j d |  j d | d |  j d |  j d |  j ƒ|  _ |  S(   s‚  Creates an affinity matrix for X using the selected affinity,
        then applies spectral clustering to this affinity matrix.

        Parameters
        ----------
        X : array-like or sparse matrix, shape (n_samples, n_features)
            OR, if affinity==`precomputed`, a precomputed affinity
            matrix of shape (n_samples, n_samples)

        y : Ignored

        t   accept_sparset   csrt   csct   coot   dtypet   ensure_min_samplesi   i    i   t   precomputeds›   The spectral clustering API has changed. ``fit``now constructs an affinity matrix from data. To use a custom affinity matrix, set ``affinity=precomputed``.t   nearest_neighborsRP   t   include_selfRT   g      à?RO   RQ   RR   t   metrict   filter_paramsRH   RA   R+   RD   RB   RI   N(   R   R   t   float64R   RG   t   warningst   warnR   RP   R'   RT   R   t   affinity_matrix_RS   RF   t   callableRO   RQ   RR   R   R   R+   RL   RH   RA   RD   RB   RI   t   labels_(   RU   t   Xt   yt   connectivityt   paramsR+   (    (    s7   lib/python2.7/site-packages/sklearn/cluster/spectral.pyt   fit¾  s<    )						c         C   s   |  j  d k S(   NR]   (   RG   (   RU   (    (    s7   lib/python2.7/site-packages/sklearn/cluster/spectral.pyt	   _pairwiseð  s    N(   t   __name__t
   __module__t   __doc__RF   RV   Rl   t   propertyRm   (    (    (    s7   lib/python2.7/site-packages/sklearn/cluster/spectral.pyRM     s   —	2(   Rp   Rc   t   numpyR   t   baseR    R   t   utilsR   R   t   utils.validationR   t   metrics.pairwiseR   t	   neighborsR   t   manifoldR   t   k_means_R   R'   RF   R?   RL   RM   (    (    (    s7   lib/python2.7/site-packages/sklearn/cluster/spectral.pyt   <module>   s   	‰	r