ó
‡ˆ\c        
   @   sC  d  Z  d d l Z d d l m Z m Z m Z m Z d d l m	 Z	 m
 Z
 d d l m Z d d l m Z 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 „ Z d e d „ Z d d d d e d „ Z d d d d d d d e e d „	 Z d e e e f d „  ƒ  YZ  d S(   s   Locally Linear EmbeddingiÿÿÿÿN(   t   eight   svdt   qrt   solve(   t   eyet
   csr_matrix(   t   eigshi   (   t   BaseEstimatort   TransformerMixint   _UnstableArchMixin(   t   check_random_statet   check_array(   t   stable_cumsum(   t   check_is_fitted(   t   FLOAT_DTYPES(   t   NearestNeighborsgü©ñÒMbP?c         C   s^  t  |  d t ƒ}  t  | d t d t ƒ} |  j d | j d } } t j | | f d |  j ƒ} t j | d |  j ƒ} xÜ t | j	 d d d ƒ ƒ D]¿ \ } } | j
 |  | }	 t j |	 |	 j
 ƒ }
 t j |
 ƒ } | d k rñ | | } n | } |
 j d d | j d d … c | 7<t |
 | d t ƒ} | t j | ƒ | | d d … f <q— W| S(   sb  Compute barycenter weights of X from Y along the first axis

    We estimate the weights to assign to each point in Y[i] to recover
    the point X[i]. The barycenter weights sum to 1.

    Parameters
    ----------
    X : array-like, shape (n_samples, n_dim)

    Z : array-like, shape (n_samples, n_neighbors, n_dim)

    reg : float, optional
        amount of regularization to add for the problem to be
        well-posed in the case of n_neighbors > n_dim

    Returns
    -------
    B : array-like, shape (n_samples, n_neighbors)

    Notes
    -----
    See developers note for more information.
    t   dtypet   allow_ndi    i   i   Nt   sym_pos(   R   R   t   Truet   shapet   npt   emptyR   t   onest	   enumeratet	   transposet   Tt   dott   tracet   flatR   t   sum(   t   Xt   Zt   regt	   n_samplest   n_neighborst   Bt   vt   it   At   Ct   GR   t   Rt   w(    (    s>   lib/python2.7/site-packages/sklearn/manifold/locally_linear.pyt   barycenter_weights   s     (''c   	      C   sÁ   t  | d d | ƒj |  ƒ } | j }  |  j d } | j |  d t ƒd d … d d … f } t |  |  | d | ƒ} t j d | | d | ƒ } t	 | j
 ƒ  | j
 ƒ  | f d | | f ƒS(   s4  Computes the barycenter weighted graph of k-Neighbors for points in X

    Parameters
    ----------
    X : {array-like, NearestNeighbors}
        Sample data, shape = (n_samples, n_features), in the form of a
        numpy array or a NearestNeighbors object.

    n_neighbors : int
        Number of neighbors for each sample.

    reg : float, optional
        Amount of regularization when solving the least-squares
        problem. Only relevant if mode='barycenter'. If None, use the
        default.

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

    Returns
    -------
    A : sparse matrix in CSR format, shape = [n_samples, n_samples]
        A[i, j] is assigned the weight of edge that connects i to j.

    See also
    --------
    sklearn.neighbors.kneighbors_graph
    sklearn.neighbors.radius_neighbors_graph
    i   t   n_jobsi    t   return_distanceNR!   R   (   R   t   fitt   _fit_XR   t
   kneighborst   FalseR,   R   t   arangeR   t   ravel(	   R   R#   R!   R-   t   knnR"   t   indt   datat   indptr(    (    s>   lib/python2.7/site-packages/sklearn/manifold/locally_linear.pyt   barycenter_kneighbors_graphC   s    !	+i   t   arpackgíµ ÷Æ°>id   c         C   s¢  | d k rA |  j  d d k r8 | | d k  r8 d } qA d } n  | d k rý t | ƒ } | j d d |  j  d ƒ } y5 t |  | | d	 d
 d | d | d | ƒ\ } }	 Wn# t k
 rÏ }
 t d |
 ƒ ‚ n X|	 d d … | d … f t j | | ƒ f S| d k rŽt |  d ƒ r'|  j	 ƒ  }  n  t
 |  d | | | d f d t ƒ\ } }	 t j t j | ƒ ƒ } |	 d d … | f t j | ƒ f St d | ƒ ‚ d S(   sŽ  
    Find the null space of a matrix M.

    Parameters
    ----------
    M : {array, matrix, sparse matrix, LinearOperator}
        Input covariance matrix: should be symmetric positive semi-definite

    k : integer
        Number of eigenvalues/vectors to return

    k_skip : integer, optional
        Number of low eigenvalues to skip.

    eigen_solver : string, {'auto', 'arpack', 'dense'}
        auto : algorithm will attempt to choose the best method for input data
        arpack : use arnoldi iteration in shift-invert mode.
                    For this method, M may be a dense matrix, sparse matrix,
                    or general linear operator.
                    Warning: ARPACK can be unstable for some problems.  It is
                    best to try several random seeds in order to check results.
        dense  : use standard dense matrix operations for the eigenvalue
                    decomposition.  For this method, M must be an array
                    or matrix type.  This method should be avoided for
                    large problems.

    tol : float, optional
        Tolerance for 'arpack' method.
        Not used if eigen_solver=='dense'.

    max_iter : int
        Maximum number of iterations for 'arpack' method.
        Not used if eigen_solver=='dense'

    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`. Used when ``solver`` == 'arpack'.

    t   autoi    iÈ   i
   R:   t   denseiÿÿÿÿi   t   sigmag        t   tolt   maxitert   v0sð   Error in determining null-space with ARPACK. Error message: '%s'. Note that method='arpack' can fail when the weight matrix is singular or otherwise ill-behaved.  method='dense' is recommended. See online documentation for more information.Nt   toarrayt   eigvalst   overwrite_as   Unrecognized eigen_solver '%s'(   R   R
   t   uniformR   t   RuntimeErrort
   ValueErrorR   R   t   hasattrRA   R    R   t   argsortt   abs(   t   Mt   kt   k_skipt   eigen_solverR>   t   max_itert   random_stateR@   t   eigen_valuest   eigen_vectorst   msgt   index(    (    s>   lib/python2.7/site-packages/sklearn/manifold/locally_linear.pyt
   null_spacen   s.    +#		-)#R;   t   standardg-Cëâ6?gê-™—q=c   ;      C   s  | d" k r t  d | ƒ ‚ n  | d# k r> t  d	 | ƒ ‚ n  t d
 | d d | ƒ } | j |  ƒ | j }  |  j \ } } | | k r— t  d ƒ ‚ n  | | k r¼ t  d | | f ƒ ‚ n  | d k r× t  d ƒ ‚ n  | d k } | d k rt | d
 | d | d | ƒ} | rEt d | j | j Œ | } | j | j	 ƒ  } qó| j | | j | j
 ƒ  } | j d d | j d d … c d 7<nf
| d k r„| | d d } | | | k rÊt  d ƒ ‚ n  | j |  d
 | d d t ƒ} | d d … d d … f } t j | d | | f d t j ƒ} d | d d … d f <t j | | f d t j ƒ} | | k } xþt | ƒ D]ð} |  | | } | | j d ƒ 8} | r¾t | d d ƒd } n> t j | | j ƒ } t | ƒ d d d … d d d … f } | d d … d | … f | d d … d d | … f <d | } xz t | ƒ D]l } | d d … | | d … f | d d … | | … f | d d … | | | | … f <| | | 7} qIWt | ƒ \ } } | d d … | d d … f } | j d ƒ } d | t j t | ƒ | k  ƒ <| | :} t j | | | | ƒ \ }  }! | |  |! f c t j | | j ƒ 7<qxW| rót | ƒ } qóno| d k r
| | k  r«t  d ƒ ‚ n  | j |  d
 | d d t ƒ} | d d … d d … f } t j | | | f ƒ }" t | | ƒ }# t j | |# g ƒ }$ | | k } | r‘xM t | ƒ D]? } |  | | |  | }% t |% d t ƒ\ |" | <|$ | <}& qAW|$ d C}$ nŽ x‹ t | ƒ D]} } |  | | |  | }% t j |% |% j ƒ }' t |' ƒ \ }( }) |( d d d … |$ | <|) d d … d d d … f |" | <qžWd |$ j d ƒ } t j |" j d d d ƒ t j  | ƒ ƒ }* |* d d … d |# … f c |$ | d d … d f :<|* d d … |# d … f c | d d … d f :<t j | | f ƒ }+ x2 t | ƒ D]$ } t j |" | |* | ƒ |+ | <qæW|+ |+ j d ƒ d d … d f :}+ |$ d d … | d … f j d ƒ |$ d d … d | … f j d ƒ }, t j" |, ƒ }- t j | d t# ƒ}. t$ |$ d ƒ }/ |/ d d … d d … f |/ d d … d d … f d }0 x= t | ƒ D]/ } t j% |0 | d d d … f |- ƒ |. | <qóW|. | |# 7}. t j | | f d t j ƒ} x¯t | ƒ D]¡} |. | }1 |" | d d … | |1 d … f }2 t j& j' |2 j d ƒ ƒ t j( |1 ƒ }3 t j) |1 |3 ƒ t j |2 j t j  | ƒ ƒ }4 t j& j' |4 ƒ }5 |5 |	 k  r	|4 d 9}4 n
 |4 |5 :}4 |2 d t j* t j |2 |4 ƒ |4 ƒ d |3 |+ | d d … d f }6 t j | | | | ƒ \ }  }! | |  |! f c t j |6 |6 j ƒ 7<|6 j d ƒ }7 | | | | f c |7 8<| | | | f c |7 8<| | | f c |1 7<q_W| rót | ƒ } qón×| d k ró| j |  d
 | d d t ƒ} | d d … d d … f } t j | | f ƒ } | | k } xlt | ƒ D][} |  | | }8 |8 |8 j d ƒ 8}8 | r×
t |8 d t ƒd }9 n> t j |8 |8 j ƒ } t | ƒ d d d … d d d … f }9 t j | | d f ƒ } |9 d d … d | … f | d d … d d … f <d t j( | ƒ | d d … d f <t j | | j ƒ }: t j | | | | ƒ \ }  }! | |  |! f c |: 8<| | | | | f c d 7<q‘
Wn  t+ | | d d d | d | d  | d! |
 ƒS($   s“  Perform a Locally Linear Embedding analysis on the data.

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

    Parameters
    ----------
    X : {array-like, NearestNeighbors}
        Sample data, shape = (n_samples, n_features), in the form of a
        numpy array or a NearestNeighbors object.

    n_neighbors : integer
        number of neighbors to consider for each point.

    n_components : integer
        number of coordinates for the manifold.

    reg : float
        regularization constant, multiplies the trace of the local covariance
        matrix of the distances.

    eigen_solver : string, {'auto', 'arpack', 'dense'}
        auto : algorithm will attempt to choose the best method for input data

        arpack : use arnoldi iteration in shift-invert mode.
                    For this method, M may be a dense matrix, sparse matrix,
                    or general linear operator.
                    Warning: ARPACK can be unstable for some problems.  It is
                    best to try several random seeds in order to check results.

        dense  : use standard dense matrix operations for the eigenvalue
                    decomposition.  For this method, M must be an array
                    or matrix type.  This method should be avoided for
                    large problems.

    tol : float, optional
        Tolerance for 'arpack' method
        Not used if eigen_solver=='dense'.

    max_iter : integer
        maximum number of iterations for the arpack solver.

    method : {'standard', 'hessian', 'modified', 'ltsa'}
        standard : use the standard locally linear embedding algorithm.
                   see reference [1]_
        hessian  : use the Hessian eigenmap method.  This method requires
                   n_neighbors > n_components * (1 + (n_components + 1) / 2.
                   see reference [2]_
        modified : use the modified locally linear embedding algorithm.
                   see reference [3]_
        ltsa     : use local tangent space alignment algorithm
                   see reference [4]_

    hessian_tol : float, optional
        Tolerance for Hessian eigenmapping method.
        Only used if method == 'hessian'

    modified_tol : float, optional
        Tolerance for modified LLE method.
        Only used if method == 'modified'

    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`. Used when ``solver`` == 'arpack'.

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

    Returns
    -------
    Y : array-like, shape [n_samples, n_components]
        Embedding vectors.

    squared_error : float
        Reconstruction error for the embedding vectors. Equivalent to
        ``norm(Y - W Y, 'fro')**2``, where W are the reconstruction weights.

    References
    ----------

    .. [1] `Roweis, S. & Saul, L. Nonlinear dimensionality reduction
        by locally linear embedding.  Science 290:2323 (2000).`
    .. [2] `Donoho, D. & Grimes, C. Hessian eigenmaps: Locally
        linear embedding techniques for high-dimensional data.
        Proc Natl Acad Sci U S A.  100:5591 (2003).`
    .. [3] `Zhang, Z. & Wang, J. MLLE: Modified Locally Linear
        Embedding Using Multiple Weights.`
        http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.70.382
    .. [4] `Zhang, Z. & Zha, H. Principal manifolds and nonlinear
        dimensionality reduction via tangent space alignment.
        Journal of Shanghai Univ.  8:406 (2004)`
    R;   R:   R<   s   unrecognized eigen_solver '%s'RU   t   hessiant   modifiedt   ltsas   unrecognized method '%s'R#   i   R-   s>   output dimension must be less than or equal to input dimensionsH   Expected n_neighbors <= n_samples,  but n_samples = %d, n_neighbors = %di    s   n_neighbors must be positiveR!   t   formatNi   s^   for method='hessian', n_neighbors must be greater than [n_components * (n_components + 3) / 2]R.   R   t   full_matricesiÿÿÿÿs1   modified LLE requires n_neighbors >= n_componentsgü©ñÒMbP?g      ð?RL   RM   R>   RN   RO   (   R;   R:   R<   (   RU   RV   RW   RX   (,   RF   R   R/   R0   R   R9   R   RY   R   t   tocsrRA   R   R1   R2   R   R   t   float64t   zerost   ranget   meanR   R   R    R   R   t   whereRI   t   meshgridR   t   minR   R   R   t   Nonet   mediant   intR   t   searchsortedt   linalgt   normt   sqrtt   fullt   outerRT   (;   R   R#   t   n_componentsR!   RM   R>   RN   t   methodt   hessian_tolt   modified_tolRO   R-   t   nbrst   Nt   d_int   M_sparset   WRJ   t   dpt	   neighborst   Yit   use_svdR&   t   Git   Ut   Cit   jRK   t   QR*   R+   t   St   nbrs_xt   nbrs_yt   Vt   nevt   evalst   X_nbrst   _t   C_nbrst   evit   vit   tmpt   w_regt   rhot   etat   s_ranget   evals_cumsumt	   eta_ranget   s_it   Vit   alpha_it   ht   norm_ht   Wit   Wi_sum1t   XiR%   t   GiGiT(    (    s>   lib/python2.7/site-packages/sklearn/manifold/locally_linear.pyt   locally_linear_embedding¼   s   d	*	&)6
; 
 )		'*62"#H:-
#(.
#" %	)2# %t   LocallyLinearEmbeddingc           B   se   e  Z d  Z d d d d d d d d d	 d d d d
 „ Z d „  Z d d „ Z d d „ Z d „  Z RS(   so  Locally Linear Embedding

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

    Parameters
    ----------
    n_neighbors : integer
        number of neighbors to consider for each point.

    n_components : integer
        number of coordinates for the manifold

    reg : float
        regularization constant, multiplies the trace of the local covariance
        matrix of the distances.

    eigen_solver : string, {'auto', 'arpack', 'dense'}
        auto : algorithm will attempt to choose the best method for input data

        arpack : use arnoldi iteration in shift-invert mode.
                    For this method, M may be a dense matrix, sparse matrix,
                    or general linear operator.
                    Warning: ARPACK can be unstable for some problems.  It is
                    best to try several random seeds in order to check results.

        dense  : use standard dense matrix operations for the eigenvalue
                    decomposition.  For this method, M must be an array
                    or matrix type.  This method should be avoided for
                    large problems.

    tol : float, optional
        Tolerance for 'arpack' method
        Not used if eigen_solver=='dense'.

    max_iter : integer
        maximum number of iterations for the arpack solver.
        Not used if eigen_solver=='dense'.

    method : string ('standard', 'hessian', 'modified' or 'ltsa')
        standard : use the standard locally linear embedding algorithm.  see
                   reference [1]
        hessian  : use the Hessian eigenmap method. This method requires
                   ``n_neighbors > n_components * (1 + (n_components + 1) / 2``
                   see reference [2]
        modified : use the modified locally linear embedding algorithm.
                   see reference [3]
        ltsa     : use local tangent space alignment algorithm
                   see reference [4]

    hessian_tol : float, optional
        Tolerance for Hessian eigenmapping method.
        Only used if ``method == 'hessian'``

    modified_tol : float, optional
        Tolerance for modified LLE method.
        Only used if ``method == 'modified'``

    neighbors_algorithm : string ['auto'|'brute'|'kd_tree'|'ball_tree']
        algorithm to use for nearest neighbors search,
        passed to neighbors.NearestNeighbors instance

    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`. Used when ``eigen_solver`` == 'arpack'.

    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
    ----------
    embedding_ : array-like, shape [n_samples, n_components]
        Stores the embedding vectors

    reconstruction_error_ : float
        Reconstruction error associated with `embedding_`

    nbrs_ : NearestNeighbors object
        Stores nearest neighbors instance, including BallTree or KDtree
        if applicable.

    Examples
    --------
    >>> from sklearn.datasets import load_digits
    >>> from sklearn.manifold import LocallyLinearEmbedding
    >>> X, _ = load_digits(return_X_y=True)
    >>> X.shape
    (1797, 64)
    >>> embedding = LocallyLinearEmbedding(n_components=2)
    >>> X_transformed = embedding.fit_transform(X[:100])
    >>> X_transformed.shape
    (100, 2)

    References
    ----------

    .. [1] `Roweis, S. & Saul, L. Nonlinear dimensionality reduction
        by locally linear embedding.  Science 290:2323 (2000).`
    .. [2] `Donoho, D. & Grimes, C. Hessian eigenmaps: Locally
        linear embedding techniques for high-dimensional data.
        Proc Natl Acad Sci U S A.  100:5591 (2003).`
    .. [3] `Zhang, Z. & Wang, J. MLLE: Modified Locally Linear
        Embedding Using Multiple Weights.`
        http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.70.382
    .. [4] `Zhang, Z. & Zha, H. Principal manifolds and nonlinear
        dimensionality reduction via tangent space alignment.
        Journal of Shanghai Univ.  8:406 (2004)`
    i   i   gü©ñÒMbP?R;   gíµ ÷Æ°>id   RU   g-Cëâ6?gê-™—q=c         C   sp   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ |	 |  _ | |  _	 |
 |  _
 | |  _ d  S(   N(   R#   Rl   R!   RM   R>   RN   Rm   Rn   Ro   RO   t   neighbors_algorithmR-   (   t   selfR#   Rl   R!   RM   R>   RN   Rm   Rn   Ro   R›   RO   R-   (    (    s>   lib/python2.7/site-packages/sklearn/manifold/locally_linear.pyt   __init__|  s    											c         C   sÎ   t  |  j d |  j d |  j ƒ|  _ t |  j ƒ } t | d t ƒ} |  j j	 | ƒ t
 |  j |  j |  j d |  j d |  j d |  j d |  j d |  j d	 |  j d
 | d |  j d |  j ƒ	\ |  _ |  _ d  S(   Nt	   algorithmR-   R   RM   R>   RN   Rm   Rn   Ro   RO   R!   (   R   R#   R›   R-   t   nbrs_R
   RO   R   t   floatR/   R™   Rl   RM   R>   RN   Rm   Rn   Ro   R!   t
   embedding_t   reconstruction_error_(   Rœ   R   RO   (    (    s>   lib/python2.7/site-packages/sklearn/manifold/locally_linear.pyt   _fit_transform  s    	c         C   s   |  j  | ƒ |  S(   s  Compute the embedding vectors for data X

        Parameters
        ----------
        X : array-like of shape [n_samples, n_features]
            training set.

        y : Ignored

        Returns
        -------
        self : returns an instance of self.
        (   R£   (   Rœ   R   t   y(    (    s>   lib/python2.7/site-packages/sklearn/manifold/locally_linear.pyR/     s    c         C   s   |  j  | ƒ |  j S(   s-  Compute the embedding vectors for data X and transform X.

        Parameters
        ----------
        X : array-like of shape [n_samples, n_features]
            training set.

        y : Ignored

        Returns
        -------
        X_new : array-like, shape (n_samples, n_components)
        (   R£   R¡   (   Rœ   R   R¤   (    (    s>   lib/python2.7/site-packages/sklearn/manifold/locally_linear.pyt   fit_transform®  s    c         C   sÅ   t  |  d ƒ t | ƒ } |  j j | d |  j d t ƒ} t | |  j j | d |  j ƒ} t	 j
 | j d |  j f ƒ } xC t | j d ƒ D]. } t	 j |  j | | j | | ƒ | | <q W| S(   sª  
        Transform new points into embedding space.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]

        Returns
        -------
        X_new : array, shape = [n_samples, n_components]

        Notes
        -----
        Because of scaling performed by this method, it is discouraged to use
        it together with methods that are not scale-invariant (like SVMs)
        RŸ   R#   R.   R!   i    (   R   R   RŸ   R1   R#   R2   R,   R0   R!   R   R   R   Rl   R^   R   R¡   R   (   Rœ   R   R6   t   weightst   X_newR&   (    (    s>   lib/python2.7/site-packages/sklearn/manifold/locally_linear.pyt	   transform¿  s    	,N(	   t   __name__t
   __module__t   __doc__Rc   R   R£   R/   R¥   R¨   (    (    (    s>   lib/python2.7/site-packages/sklearn/manifold/locally_linear.pyRš   	  s   q				(!   R«   t   numpyR   t   scipy.linalgR    R   R   R   t   scipy.sparseR   R   t   scipy.sparse.linalgR   t   baseR   R   R	   t   utilsR
   R   t   utils.extmathR   t   utils.validationR   R   Rv   R   R,   Rc   R9   RT   R™   Rš   (    (    (    s>   lib/python2.7/site-packages/sklearn/manifold/locally_linear.pyt   <module>   s(   "/+N	ÿ K	