U
    3d;                     @   s   d Z ddlZddl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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 ddlmZmZ ddlm Z  G dd deeeZ!dS )zIsomap for manifold learning    N)IntegralReal)issparse)shortest_path)connected_components   )BaseEstimatorTransformerMixinClassNamePrefixFeaturesOutMixin)NearestNeighborskneighbors_graph)radius_neighbors_graph)check_is_fitted)	KernelPCA)KernelCenterer)_fix_connected_components)Interval
StrOptions)_VALID_METRICSc                   @   s*  e Zd ZU dZeedddddgeedddddgeeddddgedd	d
hgeeddddgeedddddgedddhgeddddhgedgeeddddgeee	dhB e
gedgdZeed< dddddddddddddddZdd Zdd Zd%ddZd&dd Z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 : int or None, default=5
        Number of neighbors to consider for each point. If `n_neighbors` is an int,
        then `radius` must be `None`.

    radius : float or None, default=None
        Limiting distance of neighbors to return. If `radius` is a float,
        then `n_neighbors` must be set to `None`.

        .. versionadded:: 1.1

    n_components : int, default=2
        Number of coordinates for the manifold.

    eigen_solver : {'auto', 'arpack', 'dense'}, default='auto'
        '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, default=0
        Convergence tolerance passed to arpack or lobpcg.
        not used if eigen_solver == 'dense'.

    max_iter : int, default=None
        Maximum number of iterations for the arpack solver.
        not used if eigen_solver == 'dense'.

    path_method : {'auto', 'FW', 'D'}, default='auto'
        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 : {'auto', 'brute', 'kd_tree', 'ball_tree'},                           default='auto'
        Algorithm to use for nearest neighbors search,
        passed to neighbors.NearestNeighbors instance.

    n_jobs : int or None, 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.

    metric : str, or callable, default="minkowski"
        The metric to use when calculating distance between instances in a
        feature array. If metric is a string or callable, it must be one of
        the options allowed by :func:`sklearn.metrics.pairwise_distances` for
        its metric parameter.
        If metric is "precomputed", X is assumed to be a distance matrix and
        must be square. X may be a :term:`Glossary <sparse graph>`.

        .. versionadded:: 0.22

    p : int, default=2
        Parameter for the Minkowski metric from
        sklearn.metrics.pairwise.pairwise_distances. When p = 1, this is
        equivalent to using manhattan_distance (l1), and euclidean_distance
        (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.

        .. versionadded:: 0.22

    metric_params : dict, default=None
        Additional keyword arguments for the metric function.

        .. versionadded:: 0.22

    Attributes
    ----------
    embedding_ : array-like, shape (n_samples, n_components)
        Stores the embedding vectors.

    kernel_pca_ : object
        :class:`~sklearn.decomposition.KernelPCA` object used to implement the
        embedding.

    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.

    n_features_in_ : int
        Number of features seen during :term:`fit`.

        .. versionadded:: 0.24

    feature_names_in_ : ndarray of shape (`n_features_in_`,)
        Names of features seen during :term:`fit`. Defined only when `X`
        has feature names that are all strings.

        .. versionadded:: 1.0

    See Also
    --------
    sklearn.decomposition.PCA : Principal component analysis that is a linear
        dimensionality reduction method.
    sklearn.decomposition.KernelPCA : Non-linear dimensionality reduction using
        kernels and PCA.
    MDS : Manifold learning using multidimensional scaling.
    TSNE : T-distributed Stochastic Neighbor Embedding.
    LocallyLinearEmbedding : Manifold learning using Locally Linear Embedding.
    SpectralEmbedding : Spectral embedding for non-linear dimensionality.

    References
    ----------

    .. [1] Tenenbaum, J.B.; De Silva, V.; & Langford, J.C. A global geometric
           framework for nonlinear dimensionality reduction. Science 290 (5500)

    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)
       Nleft)closedr   ZbothautoZarpackZdenseZFWDZbruteZkd_treeZ	ball_treeprecomputed)n_neighborsradiusn_componentseigen_solvertolmax_iterpath_methodneighbors_algorithmn_jobspmetricmetric_params_parameter_constraints   r   Z	minkowskir   r   r   r   r    r!   r"   r#   r$   r&   r%   r'   c                C   sL   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	|| _
|| _d S )Nr*   )selfr   r   r   r   r    r!   r"   r#   r$   r&   r%   r'    r,   </tmp/pip-unpacked-wheel-zrfo1fqw/sklearn/manifold/_isomap.py__init__   s    zIsomap.__init__c              	   C   s  | j d k	r&| jd k	r&td| j dt| j | j| j| j| j| j| jd| _	| j	
| | j	j| _t| j	drx| j	j| _t| jd| j| j| j| jd| _| j d k	rt| j	| j | j| j| jd| jd}n"t| j	| j| j| j| jd| jd	}t|\}}|d
krb| jdkr$t|r$td| dtjd| ddd tf | j	j|||d| j	jd| j	j}t|| j dd| _!| j	jj"t#j$kr| j!j%| j	jj"dd| _!| j!d }|d9 }| j&|| _'| j'j(d
 | _)d S )Nz<Both n_neighbors and radius are provided. Use Isomap(radius=z=, n_neighbors=None) if intended to use radius-based neighbors)r   r   	algorithmr&   r%   r'   r$   feature_names_in_r   )r   kernelr   r    r!   r$   Zdistance)r&   r%   r'   moder$   )r   r&   r%   r'   r2   r$   r   z=The number of connected components of the neighbors graph is z > 1. The graph cannot be completed with metric='precomputed', and Isomap cannot befitted. Increase the number of neighbors to avoid this issue, or precompute the full distance matrix instead of passing a sparse neighbors graph.zm > 1. Completing the graph to fit Isomap might be slow. Increase the number of neighbors to avoid this issue.r   )
stacklevel)Xgraphn_connected_componentsZcomponent_labelsr2   r&   F)methodZdirected)copy      )*r   r   
ValueErrorr   r#   r&   r%   r'   r$   nbrs_fitZn_features_in_hasattrr0   r   r   r   r    r!   kernel_pca_r   r   r   r   RuntimeErrorwarningswarnr   Z_fit_XZeffective_metric_Zeffective_metric_params_r   r"   dist_matrix_dtypenpfloat32Zastypefit_transform
embedding_shapeZ_n_features_out)r+   r4   Znbgr6   labelsGr,   r,   r-   _fit_transform   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
            Reconstruction error.

        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)``
        r9   r   r   )	rB   r   rF   r>   Zeigenvalues_rD   sqrtsumrH   )r+   rJ   ZG_centerZevalsr,   r,   r-   reconstruction_error4  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, sparse matrix, precomputed tree, or NearestNeighbors
            object.

        y : Ignored
            Not used, present for API consistency by convention.

        Returns
        -------
        self : object
            Returns a fitted instance of self.
        )_validate_paramsrK   r+   r4   yr,   r,   r-   r<   M  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` is the number of samples
            and `n_features` is the number of features.

        y : Ignored
            Not used, present for API consistency by convention.

        Returns
        -------
        X_new : array-like, shape (n_samples, n_components)
            X transformed in the new space.
        )rO   rK   rG   rP   r,   r,   r-   rF   c  s    
zIsomap.fit_transformc           	      C   s   t |  | jdk	r(| jj|dd\}}n| jj|dd\}}| jj}|jd }t|drl|jt	j
krlt	j
}nt	j}t	||f|}t|D ]2}t	| j||  || dddf  d||< q|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, sparse matrix}, shape (n_queries, n_features)
            If neighbors_algorithm='precomputed', X is assumed to be a
            distance matrix or a sparse graph of shape
            (n_queries, n_samples_fit).

        Returns
        -------
        X_new : array-like, shape (n_queries, n_components)
            X transformed in the new space.
        NT)Zreturn_distancer   rC   r   r9   )r   r   r;   Z
kneighborsZradius_neighborsZn_samples_fit_rH   r=   rC   rD   rE   float64zerosrangeminrB   r>   	transform)	r+   r4   Z	distancesindicesZn_samples_fitZ	n_queriesrC   ZG_Xir,   r,   r-   rV   x  s    

0zIsomap.transformc                 C   s   dt jt jgiS )NZpreserves_dtype)rD   rR   rE   )r+   r,   r,   r-   
_more_tags  s    zIsomap._more_tags)N)N)__name__
__module____qualname____doc__r   r   r   r   setr   callabledictr(   __annotations__r.   rK   rN   r<   rF   rV   rY   r,   r,   r,   r-   r      sD   
 c

3r   )"r]   r@   ZnumpyrD   Znumbersr   r   Zscipy.sparser   Zscipy.sparse.csgraphr   r   baser   r	   r
   Z	neighborsr   r   r   Zutils.validationr   decompositionr   Zpreprocessingr   Zutils.graphr   Zutils._param_validationr   r   Zmetrics.pairwiser   r   r,   r,   r,   r-   <module>   s    