B
    ¦	ˆ\\  ã               @   sr   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 G d
d„ deeƒZdS )zIsomap for manifold learningé    Né   )ÚBaseEstimatorÚTransformerMixin)ÚNearestNeighborsÚkneighbors_graph)Úcheck_array)Úgraph_shortest_path)Ú	KernelPCA)ÚKernelCentererc               @   sF   e Zd ZdZddd„Zd	d
„ Zdd„ Zddd„Zddd„Zdd„ Z	dS )ÚIsomapaš
  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)
    é   r   Úautor   Nc	       	      C   s4   || _ || _|| _|| _|| _|| _|| _|| _d S )N)Ún_neighborsÚn_componentsÚeigen_solverÚtolÚmax_iterÚpath_methodÚneighbors_algorithmÚn_jobs)	Úselfr   r   r   r   r   r   r   r   © r   ú6lib/python3.7/site-packages/sklearn/manifold/isomap.pyÚ__init__h   s    zIsomap.__init__c             C   s¤   t |dd}t| j| j| jd| _| j |¡ | jj| _t	| j
d| j| j| j| jd| _t| j| jd| jd}t|| jdd	| _| jd
 }|d9 }| j |¡| _d S )NZcsr)Zaccept_sparse)r   Ú	algorithmr   Zprecomputed)r   Zkernelr   r   r   r   Zdistance)Úmoder   F)ÚmethodZdirectedr   g      à¿)r   r   r   r   r   Únbrs_ÚfitZ_fit_XÚtraining_data_r	   r   r   r   r   Úkernel_pca_r   r   r   Údist_matrix_Úfit_transformÚ
embedding_)r   ÚXZkngÚGr   r   r   Ú_fit_transformt   s&    



zIsomap._fit_transformc             C   sN   d| j d  }tƒ  |¡}| jj}t t |d ¡t |d ¡ ¡|jd  S )a  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      à¿r   r   )	r!   r
   r"   r    Zlambdas_ÚnpZsqrtÚsumÚshape)r   r%   ZG_centerZevalsr   r   r   Úreconstruction_errorŒ   s    zIsomap.reconstruction_errorc             C   s   |   |¡ | S )a¤  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$   Úyr   r   r   r   ¤   s    
z
Isomap.fitc             C   s   |   |¡ | jS )a…  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$   r+   r   r   r   r"   ·   s    
zIsomap.fit_transformc             C   sš   t |ƒ}| jj|dd\}}t |jd | jjd f¡}xDt|jd ƒD ]2}t | j	||  || dd…df  d¡||< qHW |dC }|d9 }| j
 |¡S )a¸  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)Zreturn_distancer   Nr   g      à¿)r   r   Z
kneighborsr'   Zzerosr)   r   ÚrangeÚminr!   r    Ú	transform)r   r$   Z	distancesÚindicesZG_XÚir   r   r   r.   É   s    "zIsomap.transform)r   r   r   r   Nr   r   N)N)N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r&   r*   r   r"   r.   r   r   r   r   r      s   W  



r   )r4   Znumpyr'   Úbaser   r   Z	neighborsr   r   Zutilsr   Zutils.graphr   Zdecompositionr	   Zpreprocessingr
   r   r   r   r   r   Ú<module>   s   