`L iMw:dZddlmZmZddlZddlmZmZm Z m Z ddl m Z m Z mZddlmZddlmZmZmZmZmZdd lmZdd lmZmZdd lmZdd lmZm Z m!Z!dd l"m#Z#ddl$m%Z%m&Z&m'Z'd(dZ(d)dZ) d*dZ*dddddddddd dZ+e!degeedddgeedddgeedddge hdgeedddgeedddge hd geedddgeedddgd!gdegd" d#$dddddddddd d%Z,Gd&d'eeeeZ-y)+zLocally Linear Embedding)IntegralRealN)eighqrsolvesvd) csr_matrixeye lil_matrix)eigsh) BaseEstimatorClassNamePrefixFeaturesOutMixinTransformerMixin _fit_context_UnstableArchMixin)NearestNeighbors) check_arraycheck_random_state)_init_arpack_v0)Interval StrOptionsvalidate_params) stable_cumsum) FLOAT_DTYPEScheck_is_fitted validate_dataMbP?ct|t}t|t}t|t}|j\}}|jd|k(sJt j ||f|j }t j||j }t|D]\}} || } | ||z } t j| | j} t j| } | dkDr|| z}n|}| jdd|dzxx|z cc<t| |d}|t j|z ||ddf<|S)aCompute barycenter weights of X from Y along the first axis We estimate the weights to assign to each point in Y[indices] to recover the point X[i]. The barycenter weights sum to 1. Parameters ---------- X : array-like, shape (n_samples, n_dim) Y : array-like, shape (n_samples, n_dim) indices : array-like, shape (n_samples, n_dim) Indices of the points in Y used to compute the barycenter reg : float, default=1e-3 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. dtyperNpos)assume_a)rrintshapenpemptyr!ones enumeratedotTtraceflatrsum)XYindicesreg n_samples n_neighborsBviindACGr-Rws f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/manifold/_locally_linear.pybarycenter_weightsr@s*6 A\*AA\*A'-G$]]I{ 771: "" " )[)9A  177+AG$  3 cF !H FF1accN  19e AA !+/!"a'" !Q 'bffQi-!Q$   Hc\t|dz|j|}|j}|j}|j |dddddf}t ||||}t jd||zdz|}t|j|j|f||fS) a-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, default=1e-3 Amount of regularization when solving the least-squares problem. Only relevant if mode='barycenter'. If None, use the default. n_jobs : int or None, 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 ` 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 r"r5n_jobsF)return_distanceNr3rr&) rfit_fit_Xn_samples_fit_ kneighborsr@r'aranger ravel) r0r5r3rDknnr4r9dataindptrs r?barycenter_kneighbors_graphrQRsB {Qv F J J1 MC A""I ..E. 21ab5 9C aCS 1D YYq)k1A5{ CF tzz|SYY[&9)YAW XXrAr"ư>dcX|dk(r|jddkDr ||zdkrd}nd}|dk(rTt|jd|} t|||zd|||\}} | d d |d ft j ||d fS|dk(r{t|d r|j}t||||zd z fd \}} t jt j|} | d d | ft j |fSt d|z#t$r} t d | z| d } ~ wwxYw)a0 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 : int Number of eigenvalues/vectors to return k_skip : int, default=1 Number of low eigenvalues to skip. eigen_solver : {'auto', 'arpack', 'dense'}, default='arpack' 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, default=1e-6 Tolerance for 'arpack' method. Not used if eigen_solver=='dense'. max_iter : int, default=100 Maximum number of iterations for 'arpack' method. Not used if eigen_solver=='dense' random_state : int, RandomState instance, default=None Determines the random number generator when ``solver`` == 'arpack'. Pass an int for reproducible results across multiple function calls. See :term:`Glossary `. autor arpackdenseg)sigmatolmaxiterv0a Error in determining null-space with ARPACK. Error message: '%s'. Note that eigen_solver='arpack' can fail when the weight matrix is singular or otherwise ill-behaved. In that case, eigen_solver='dense' is recommended. See online documentation for more information.Ntoarrayr"T)subset_by_index overwrite_azUnrecognized eigen_solver '%s') r&rr RuntimeError ValueErrorr'r/hasattrr^rargsortabs) Mkk_skip eigen_solverr[max_iter random_stater] eigen_values eigen_vectorseindexs r? null_spacerp|sTTv 771: F R#L"Lx QWWQZ 6 */1v:Sc8+ 'L-QZ("&&fg1F*GGG  1i  A&* F Q7T' # m 266,/0QX&|(<<<9LHII' 69: :    sD D)D$$D)rUstandard-C6?-q=) r3rir[rjmethod hessian_tol modified_tolrkrDc t|dz| } | j|| j}|j\} }||kDr t d|| k\rt d| |fz|dk7}|rt nt j}|dk(rt| ||| }|r3t|jd|ji|z }|j|z}n|j|z|jz |z j}|jdd|jd dzxxdz cc<n/|d k(r||dzzd z}|||zkr t d | j||dzd }|ddddf}t j|d|z|zft j }d|ddd f<|| | ft j }||kD}t#| D]i}|||}||j%d z}|rt'|d d }n8t j(||j}t+|ddddddf}|ddd|f|dddd|zf<d|z}t#|D]3}|dd||dzf|dd||fz|dd|||z|z f<|||z z }5t-|\}}|dd|dzdf}|j/d } d| t j0t3| |k<|| z}t j4||||\}!}"||!|"fxxt j(||jz cc<ln|dk(r||kr t d| j||dzd }|ddddf}t j| ||f}#t7||}$t j| |$g}%||kD}|r;t#| D]'}|||||z }&t'|&d\|#|<|%|<}')|%d z}%nft#| D]X}|||||z }&t j(|&|&j}(t+|(\})}*|)ddd|%|<|*dddddf|#|<Zd|%j/dz}t j(|#j9d d dt j:|}+|+ddd|$fxx|%|dddfzzcc<|+dd|$dfxx|dddfzcc<t j| |f},t#| D]!}t j(|#||+||,|<#|,|,j/ddddfz},|%dd|dfj/d|%ddd|fj/dz }-t j<|-}.t j| t>}/tA|%d}0|0ddddf|0ddddfz dz }1t#| D]#}t jB|1|dddf|.|/|<%|/||$z z }/|| | ft j }t#| D]}|/|}2|#|dd||2z df}3t jDjG|3j/d t jH|2z }4t jJ|2|4t j(|3jt j:|z }5t jDjG|5}6|6| kr|5d z}5n|5|6z}5|3d t jLt j(|3|5|5zz d|4z |,|dddfzz}7t j4||||\}!}"||!|"fxxt j(|7|7jz cc<|7j/d}8||||fxx|8zcc<||||gfxx|8zcc<|||fxx|2z cc<n|dk(r~| j||dzd }|ddddf}|| | ft j }||kD}t#| D].}|||}9|9|9j%d z}9|rt'|9dd }:n8t j(|9|9j}t+|ddddddf}:t j||dzf}|:ddd|f|ddddf<dt jH|z |ddd f<t j(||j};t j4||||\}!}"||!|"fxx|;zcc<|||||fxxt j:|z cc<1|rjO}tQ|d|||| S)Nr"rCz>output dimension must be less than or equal to input dimensionzFExpected n_neighbors < n_samples, but n_samples = %d, n_neighbors = %drYrq)r5r3rDformatrhessianr z^for method='hessian', n_neighbors must be greater than [n_components * (n_components + 3) / 2]Fr5rEr ) full_matricesmodifiedz1modified LLE requires n_neighbors >= n_componentsTrltsag?rG)rhrir[rjrk))rrHrIr&rbr r'zerosrQr rxr,r^r.rKr(float64rangemeanrr+rrr/whereremeshgridmin transposer)medianr%r searchsortedlinalgnormsqrtfulloutertocsrrp)Snbrs_xnbrs_yVnevevalsX_nbrs_C_nbrsevivitmpw_regrhoetas_range evals_cumsum eta_ranges_iVialpha_ihnorm_hWiWi_sum1Xir7GiGiTs< r?_locally_linear_embeddingrs   a GDHHQK AggGAtd L  a T+   w&H,4j"((  ' ks6  QWW.QXX.2AaAq133"++-A FF$aggaj1n$ % * % 9  \A- .! 3 ,+ +:  OO ;?E$ ae$ XX{A $4r$9:"** M1a4 #QF"** =$q 0A9Q<B "''!* B!,Q/VVB%HQK4R4(*+A} },<*=Bq!a,&&& 'L A<( &23Aq1q5yL/Aa<FWDX2X1a!l*Q.../\A%% &b6DAq!\A%''(AaA01Abhhs1v +, - FA[[1y|DNFF ffn 133 / 7 0: :   %PQ QOO ;?E$ ae$ HHak2 3$ $!S"$ 1X D9Q<1Q4/$'d$C!!eAh D aKE1X #9Q<1Q4/1v,Rtt9a!TrT'{!  #UYYq\!ffQ[[Aq)277;+?@ AttG AtG ,,  AstG AtG $ ![)*q ,AvvadCF+E!H , 1ag&&A|}$%))!,uQ  5E/F/J/J1/MMiin ((1C($UA.  BC(<3B3+??!C q BA1dd7);SAGAJ B;$$ $QF"** =q$ A!*C1as*,,-BiinnRVVAY/"''#,>G W%rttRWW[5I(JJAYY^^A&F $QV a"((266"a=!444G uQPQSWZGX7XXB [[1y|DNFF ffn BDD!1 1 ffQiG a1o ' )  ilQC G + adGsNGI$ L 6 OO ;?E$ ae$ #QF"** =$q HA9Q<B "''!* B$/2VVB%HQK4R4(; q(89:B!]l]*+Bq!"uIRWW[11Bq!tHFF2rtt$E[[1y|DNFF ffn  &  ilIaL( )RWW;-G G )) H, GGI  ! ! rAz array-likeleftclosed>rUrYrX>r~ryr}rqrk r0r5rr3rir[rjrtrurvrkrDTprefer_skip_nested_validationc 0t|||||||||| | |  S)aPerform a Locally Linear Embedding analysis on the data. Read more in the :ref:`User Guide `. 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 to consider for each point. n_components : int Number of coordinates for the manifold. reg : float, default=1e-3 Regularization constant, multiplies the trace of the local covariance matrix of the distances. eigen_solver : {'auto', 'arpack', 'dense'}, default='auto' 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, default=1e-6 Tolerance for 'arpack' method Not used if eigen_solver=='dense'. max_iter : int, default=100 Maximum number of iterations for the arpack solver. method : {'standard', 'hessian', 'modified', 'ltsa'}, default='standard' 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, default=1e-4 Tolerance for Hessian eigenmapping method. Only used if method == 'hessian'. modified_tol : float, default=1e-12 Tolerance for modified LLE method. Only used if method == 'modified'. random_state : int, RandomState instance, default=None Determines the random number generator when ``solver`` == 'arpack'. Pass an int for reproducible results across multiple function calls. See :term:`Glossary `. n_jobs : int or None, 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 ` for more details. Returns ------- Y : ndarray of 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. `_ .. [4] Zhang, Z. & Zha, H. Principal manifolds and nonlinear dimensionality reduction via tangent space alignment. Journal of Shanghai Univ. 8:406 (2004) Examples -------- >>> from sklearn.datasets import load_digits >>> from sklearn.manifold import locally_linear_embedding >>> X, _ = load_digits(return_X_y=True) >>> X.shape (1797, 64) >>> embedding, _ = locally_linear_embedding(X[:100],n_neighbors=5, n_components=2) >>> embedding.shape (100, 2) r)rrs r?locally_linear_embeddingrs6T % ! ! !!  rAc~eZdZUdZeedddgeedddgeedddgehdgeedddgeedddgehdgeedddgeedddgehd gd gdegd Ze e d <d dddddddddddd dZ dZ e dddZe dddZdZy)LocallyLinearEmbeddingaLocally Linear Embedding. Read more in the :ref:`User Guide `. Parameters ---------- n_neighbors : int, default=5 Number of neighbors to consider for each point. n_components : int, default=2 Number of coordinates for the manifold. reg : float, default=1e-3 Regularization constant, multiplies the trace of the local covariance matrix of the distances. eigen_solver : {'auto', 'arpack', 'dense'}, default='auto' The solver used to compute the eigenvectors. The available options are: - `'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. - `'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. .. warning:: ARPACK can be unstable for some problems. It is best to try several random seeds in order to check results. tol : float, default=1e-6 Tolerance for 'arpack' method Not used if eigen_solver=='dense'. max_iter : int, default=100 Maximum number of iterations for the arpack solver. Not used if eigen_solver=='dense'. method : {'standard', 'hessian', 'modified', 'ltsa'}, default='standard' - `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, default=1e-4 Tolerance for Hessian eigenmapping method. Only used if ``method == 'hessian'``. modified_tol : float, default=1e-12 Tolerance for modified LLE method. Only used if ``method == 'modified'``. neighbors_algorithm : {'auto', 'brute', 'kd_tree', 'ball_tree'}, default='auto' Algorithm to use for nearest neighbors search, passed to :class:`~sklearn.neighbors.NearestNeighbors` instance. random_state : int, RandomState instance, default=None Determines the random number generator when ``eigen_solver`` == 'arpack'. Pass an int for reproducible results across multiple function calls. See :term:`Glossary `. 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. Attributes ---------- embedding_ : array-like, shape [n_samples, n_components] Stores the embedding vectors reconstruction_error_ : float Reconstruction error associated with `embedding_` 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 nbrs_ : NearestNeighbors object Stores nearest neighbors instance, including BallTree or KDtree if applicable. See Also -------- SpectralEmbedding : Spectral embedding for non-linear dimensionality reduction. TSNE : Distributed Stochastic Neighbor Embedding. 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. `_ .. [4] Zhang, Z. & Zha, H. Principal manifolds and nonlinear dimensionality reduction via tangent space alignment. Journal of Shanghai Univ. 8:406 (2004) 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) r"Nrrr>rUrYrX>r~ryr}rq>rUbrutekd_tree ball_treerk) r5rr3rir[rjrtrurvneighbors_algorithmrkrD_parameter_constraintsr rrUrRrSrqrrrsc ||_||_||_||_||_||_||_||_| |_| |_ | |_ | |_ yN) r5rr3rir[rjrtrurvrkrrD) selfr5rr3rir[rjrtrurvrrkrDs r?__init__zLocallyLinearEmbedding.__init__s_ '((   &((#6  rAcJt|j|j|j|_t |j }t||t}|jj|t|j|j|j|j|j|j|j|j |j"||j$|j \|_|_|j&j*d|_y)N)r5 algorithmrDr ) r0r5rrir[rjrtrurvrkr3rDr")rr5rrDnbrs_rrkrfloatrHrrrir[rjrtrurvr3 embedding_reconstruction_error_r&_n_features_out)rr0rks r?_fit_transformz%LocallyLinearEmbedding._fit_transforms%((..;;  *$*;*;< $ / q6Ojj((****]];;((**%;; 7 33 $44Q7rATrc(|j||S)ayCompute the embedding vectors for data X. Parameters ---------- X : array-like of shape (n_samples, n_features) Training set. y : Ignored Not used, present here for API consistency by convention. Returns ------- self : object Fitted `LocallyLinearEmbedding` class instance. )rrr0ys r?rHzLocallyLinearEmbedding.fit*s" A rAc<|j||jS)aCompute the embedding vectors for data X and transform X. Parameters ---------- X : array-like of shape (n_samples, n_features) Training set. y : Ignored Not used, present here for API consistency by convention. Returns ------- X_new : array-like, shape (n_samples, n_components) Returns the instance itself. )rrrs r? fit_transformz$LocallyLinearEmbedding.fit_transform>s" ArAct|t||d}|jj||jd}t ||jj ||j}tj|jd|jf}t|jdD]8}tj|j||j||||<:|S)a Transform new points into embedding space. Parameters ---------- X : array-like of shape (n_samples, n_features) Training set. Returns ------- X_new : ndarray of shape (n_samples, n_components) Returns the instance itself. Notes ----- Because of scaling performed by this method, it is discouraged to use it together with methods that are not scale-invariant (like SVMs). F)resetrzrFr)rrrrKr5r@rIr3r'r(r&rrr+rr,)rr0r9weightsX_newr8s r? transformz LocallyLinearEmbedding.transformRs&  $ /jj## 4++U$ %Q (9(93DHHM!''!*d&7&789qwwqz" EAvvdooc!f577DE!H E rAr)__name__ __module__ __qualname____doc__rrrrrdict__annotations__rrrrHrrrAr?rrZs* BJ!1d6BC!(AtFCDq$v67#$?@Aq$v67h4?@IJK q$v>?!$4?@ *+T UV'(" $D $  ":8456&56&rAr)r)rN)r"rXrRrSN).rnumbersrrnumpyr' scipy.linalgrrrr scipy.sparser r r scipy.sparse.linalgr baserrrrrrrutilsrr utils._arpackrutils._param_validationrrr utils.extmathrutils.validationrrrr@rQrprrrrrAr?rs #--44%)3+KK)KK3 l'YVQUIJb    up, - 1d6BC!(AtFCDq$v67#$?@Aq$v67h4?@IJK q$v>?!$4?@'(" #',    F#"FRU# UrA