ó
‡ˆ\c           @   s›   d  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
 e e f d „  ƒ  YZ d S(   s   Isomap for manifold learningiÿÿÿÿNi   (   t   BaseEstimatort   TransformerMixin(   t   NearestNeighborst   kneighbors_graph(   t   check_array(   t   graph_shortest_path(   t	   KernelPCA(   t   KernelCenterert   Isomapc        	   B   sb   e  Z d  Z d d d d d d d d d „ Z d „  Z d „  Z d d „ Z d d	 „ Z d
 „  Z	 RS(   sš
  Isomap Embedding

    Non-linear dimensionality reduction through Isometric Mapping

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

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

    n_components : integer
        number of coordinates for the manifold

    eigen_solver : ['auto'|'arpack'|'dense']
        'auto' : Attempt to choose the most efficient solver
        for the given problem.

        'arpack' : Use Arnoldi decomposition to find the eigenvalues
        and eigenvectors.

        'dense' : Use a direct solver (i.e. LAPACK)
        for the eigenvalue decomposition.

    tol : float
        Convergence tolerance passed to arpack or lobpcg.
        not used if eigen_solver == 'dense'.

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

    path_method : string ['auto'|'FW'|'D']
        Method to use in finding shortest path.

        'auto' : attempt to choose the best algorithm automatically.

        'FW' : Floyd-Warshall algorithm.

        'D' : Dijkstra's algorithm.

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

    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.

    kernel_pca_ : object
        `KernelPCA` object used to implement the embedding.

    training_data_ : array-like, shape (n_samples, n_features)
        Stores the training data.

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

    dist_matrix_ : array-like, shape (n_samples, n_samples)
        Stores the geodesic distance matrix of training data.

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

    References
    ----------

    .. [1] Tenenbaum, J.B.; De Silva, V.; & Langford, J.C. A global geometric
           framework for nonlinear dimensionality reduction. Science 290 (5500)
    i   i   t   autoi    c	   	      C   sL   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   n_neighborst   n_componentst   eigen_solvert   tolt   max_itert   path_methodt   neighbors_algorithmt   n_jobs(	   t   selfR
   R   R   R   R   R   R   R   (    (    s6   lib/python2.7/site-packages/sklearn/manifold/isomap.pyt   __init__h   s    							c         C   s	  t  | d d ƒ} t d |  j d |  j d |  j ƒ |  _ |  j j | ƒ |  j j |  _ t	 d |  j
 d d d	 |  j d
 |  j d |  j d |  j ƒ |  _ t |  j |  j d d d |  j ƒ} t | d |  j d t ƒ|  _ |  j d } | d 9} |  j j | ƒ |  _ d  S(   Nt   accept_sparset   csrR
   t	   algorithmR   R   t   kernelt   precomputedR   R   R   t   modet   distancet   methodt   directedi   g      à¿(   R   R   R
   R   R   t   nbrs_t   fitt   _fit_Xt   training_data_R   R   R   R   R   t   kernel_pca_R   R   R   t   Falset   dist_matrix_t   fit_transformt
   embedding_(   R   t   Xt   kngt   G(    (    s6   lib/python2.7/site-packages/sklearn/manifold/isomap.pyt   _fit_transformt   s&    				
c         C   se   d |  j  d } t ƒ  j | ƒ } |  j j } t j t j | d ƒ t j | d ƒ ƒ | j d S(   s  Compute the reconstruction error for the embedding.

        Returns
        -------
        reconstruction_error : float

        Notes
        -------
        The cost function of an isomap embedding is

        ``E = frobenius_norm[K(D) - K(D_fit)] / n_samples``

        Where D is the matrix of distances for the input data X,
        D_fit is the matrix of distances for the output embedding X_fit,
        and K is the isomap kernel:

        ``K(D) = -0.5 * (I - 1/n_samples) * D^2 * (I - 1/n_samples)``
        g      à¿i   i    (	   R#   R   R$   R!   t   lambdas_t   npt   sqrtt   sumt   shape(   R   R(   t   G_centert   evals(    (    s6   lib/python2.7/site-packages/sklearn/manifold/isomap.pyt   reconstruction_errorŒ   s    c         C   s   |  j  | ƒ |  S(   s¤  Compute the embedding vectors for data X

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

        y : Ignored

        Returns
        -------
        self : returns an instance of self.
        (   R)   (   R   R&   t   y(    (    s6   lib/python2.7/site-packages/sklearn/manifold/isomap.pyR   ¤   s    c         C   s   |  j  | ƒ |  j S(   s…  Fit the model from data in X and transform X.

        Parameters
        ----------
        X : {array-like, sparse matrix, BallTree, KDTree}
            Training vector, where n_samples in the number of samples
            and n_features is the number of features.

        y : Ignored

        Returns
        -------
        X_new : array-like, shape (n_samples, n_components)
        (   R)   R%   (   R   R&   R2   (    (    s6   lib/python2.7/site-packages/sklearn/manifold/isomap.pyR$   ·   s    c         C   sË   t  | ƒ } |  j j | d t ƒ\ } } t j | j d |  j j d f ƒ } xT t | j d ƒ D]? } t j	 |  j
 | | | | d d … d f d ƒ | | <qd W| d C} | d 9} |  j j | ƒ S(   s¸  Transform X.

        This is implemented by linking the points X into the graph of geodesic
        distances of the training data. First the `n_neighbors` nearest
        neighbors of X are found in the training data, and from these the
        shortest geodesic distances from each point in X to each point in
        the training data are computed in order to construct the kernel.
        The embedding of X is the projection of this kernel onto the
        embedding vectors of the training set.

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

        Returns
        -------
        X_new : array-like, shape (n_samples, n_components)
        t   return_distancei    Ni   g      à¿(   R   R   t
   kneighborst   TrueR+   t   zerosR.   R    t   ranget   minR#   t   NoneR!   t	   transform(   R   R&   t	   distancest   indicest   G_Xt   i(    (    s6   lib/python2.7/site-packages/sklearn/manifold/isomap.pyR:   É   s    &)

N(
   t   __name__t
   __module__t   __doc__R9   R   R)   R1   R   R$   R:   (    (    (    s6   lib/python2.7/site-packages/sklearn/manifold/isomap.pyR      s   W		
		(   RA   t   numpyR+   t   baseR    R   t	   neighborsR   R   t   utilsR   t   utils.graphR   t   decompositionR   t   preprocessingR   R   (    (    (    s6   lib/python2.7/site-packages/sklearn/manifold/isomap.pyt   <module>   s   