`L iF=dZddlZddlmZmZddlZddlmZddl m Z m Z ddl m Z mZmZmZddlmZdd lmZdd lmZmZmZdd lmZdd lmZmZdd lmZddl m!Z!Gddeee Z"y)zIsomap for manifold learningN)IntegralReal)issparse)connected_components shortest_path) BaseEstimatorClassNamePrefixFeaturesOutMixinTransformerMixin _fit_context) KernelPCA)_VALID_METRICS)NearestNeighborskneighbors_graphradius_neighbors_graph)KernelCenterer)Interval StrOptions)_fix_connected_components)check_is_fittedceZdZUdZeeddddgeeddddgeedddgehdgeedddgeeddddgehd gehd gedgeedddgeee d hze ge dgd Z e e d <ddddddddddddd dZdZdZedddZedddZdZfdZxZS)IsomapaIsomap Embedding. Non-linear dimensionality reduction through Isometric Mapping Read more in the :ref:`User Guide `. 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 ` 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 `. .. versionadded:: 0.22 p : float, 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) Nleft)closedrboth>autodensearpack>DFWr>rbrutekd_tree ball_tree precomputed) n_neighborsradius n_components eigen_solvertolmax_iter path_methodneighbors_algorithmn_jobspmetric metric_params_parameter_constraintsrr minkowski r&r'r(r)r*r+r,r-r.r0r/r1c ||_||_||_||_||_||_||_||_| |_| |_ | |_ | |_ yNr5) selfr&r'r(r)r*r+r,r-r.r0r/r1s ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/manifold/_isomap.py__init__zIsomap.__init__s^ ' ((  &#6   *c |j%|jtd|jdt|j|j|j|j |j |j|j|_ |jj||jj|_ t|jdr|jj|_ t|jd|j |j"|j$|jj'd|_|jOt+|j|j|j |j |jd |j }nNt-|j|j|j |j |jd |j }t/|\}}|d kDr|j dk(rt1|rt3d |dt5j6d |ddt9d|jj:|||d |jj<d|jj>}tA||jBd|_"|jj:jFtHjJk(r@|jDjM|jj:jFd|_"|jDdz}|dz}|j(jO||_(|jPjRd |_*y)Nz 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)Xgraphn_connected_componentscomponent_labelsrCr0F)methoddirected)copy࿩)+r&r' ValueErrorrr-r0r/r1r.nbrs_fitn_features_in_hasattrr>r r(r)r*r+ set_output kernel_pca_rrrr RuntimeErrorwarningswarnr_fit_Xeffective_metric_effective_metric_params_rr, dist_matrix_dtypenpfloat32astype fit_transform embedding_shape_n_features_out)r8rEnbgrGlabelsGs r9_fit_transformzIsomap._fit_transforms    'DKK,C""&++/**  &((;;..;;ff,,;;   q"jj77 4::2 3%)ZZ%A%AD "$** **]];;   *y* )     '"   {{&&"00{{C) {{{{&&"00{{C*>c)B& !A %{{m+ "123;; MM012((  ,**##'=!'zz33 **55C*#d6F6FQVW ::   " "bjj 0 $ 1 1 8 8 !!''e!9!D    q  T **88;#44Q7r;c,d|jdzz}tj|}|jj}t j t j|dzt j|dzz |jdz 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)`` rLrr) r[rr`rT eigenvalues_r]sqrtsumrb)r8rfG_centerevalss r9reconstruction_errorzIsomap.reconstruction_error9sx( 4$$a' '!#11!4  --wwrvvhk*RVVE1H-==>KKr;F)prefer_skip_nested_validationc(|j||S)aCompute 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. )rgr8rEys r9rPz Isomap.fitRs, A r;c<|j||jS)aFit 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. )rgrarqs r9r`zIsomap.fit_transformks* Ar;ct||j!|jj|d\}}n |jj |d\}}|jj }|j d}t|dr.|jtjk(rtj}ntj}tj||f|}t|D]8}tj|j||||dddfzd||<:|dz}|dz}|j j#|S)aTransform 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)return_distancerr\rrL)rr&rO kneighborsradius_neighborsn_samples_fit_rbrRr\r]r^float64zerosrangeminr[rTrA) r8rE distancesindices n_samples_fit n_queriesr\G_Xis r9rAzIsomap.transforms+.     '!%!6!6q$!6!O Iw!%!rsQ" "!D &-RR*:3.^ ,.> ^r;