`L in dZddlZddlmZmZddlmZmZddlZ ddl m Z ddl mZddlmZmZmZmZddlmZdd lmZdd lmZmZmZdd lmZdd lmZdd l m!Z!m"Z"m#Z#gdZ$edeedddgdeedddgdddddZ%dZ&dZ'd$dZ(d%dZ)GddeeeeZ*Gd d!e*Z+Gd"d#e*Z,y)&aQRandom projection transformers. Random projections are a simple and computationally efficient way to reduce the dimensionality of the data by trading a controlled amount of accuracy (as additional variance) for faster processing times and smaller model sizes. The dimensions and distribution of random projections matrices are controlled so as to preserve the pairwise distances between any two samples of the dataset. The main theoretical result behind the efficiency of random projection is the `Johnson-Lindenstrauss lemma (quoting Wikipedia) `_: In mathematics, the Johnson-Lindenstrauss lemma is a result concerning low-distortion embeddings of points from high-dimensional into low-dimensional Euclidean space. The lemma states that a small set of points in a high-dimensional space can be embedded into a space of much lower dimension in such a way that distances between the points are nearly preserved. The map used for the embedding is at least Lipschitz, and can even be taken to be an orthogonal projection. N)ABCMetaabstractmethod)IntegralReal)linalg) BaseEstimatorClassNamePrefixFeaturesOutMixinTransformerMixin _fit_context)DataDimensionalityWarning)check_random_state)Interval StrOptionsvalidate_params)safe_sparse_dot)sample_without_replacement) check_arraycheck_is_fitted validate_data)GaussianRandomProjectionSparseRandomProjectionjohnson_lindenstrauss_min_dimz array-likeleftclosedneither n_samplesepsTprefer_skip_nested_validation皙?)r ctj|}tj|}tj|dkstj|dk\rtd|ztj|dkrtd|z|dzdz |dzdz z }dtj|z|z j tj S) a Find a 'safe' number of components to randomly project to. The distortion introduced by a random projection `p` only changes the distance between two points by a factor (1 +- eps) in a euclidean space with good probability. The projection `p` is an eps-embedding as defined by: .. code-block:: text (1 - eps) ||u - v||^2 < ||p(u) - p(v)||^2 < (1 + eps) ||u - v||^2 Where u and v are any rows taken from a dataset of shape (n_samples, n_features), eps is in ]0, 1[ and p is a projection by a random Gaussian N(0, 1) matrix of shape (n_components, n_features) (or a sparse Achlioptas matrix). The minimum number of components to guarantee the eps-embedding is given by: .. code-block:: text n_components >= 4 log(n_samples) / (eps^2 / 2 - eps^3 / 3) Note that the number of dimensions is independent of the original number of features but instead depends on the size of the dataset: the larger the dataset, the higher is the minimal dimensionality of an eps-embedding. Read more in the :ref:`User Guide `. Parameters ---------- n_samples : int or array-like of int Number of samples that should be an integer greater than 0. If an array is given, it will compute a safe number of components array-wise. eps : float or array-like of shape (n_components,), dtype=float, default=0.1 Maximum distortion rate in the range (0, 1) as defined by the Johnson-Lindenstrauss lemma. If an array is given, it will compute a safe number of components array-wise. Returns ------- n_components : int or ndarray of int The minimal number of components to guarantee with good probability an eps-embedding with n_samples. References ---------- .. [1] https://en.wikipedia.org/wiki/Johnson%E2%80%93Lindenstrauss_lemma .. [2] `Sanjoy Dasgupta and Anupam Gupta, 1999, "An elementary proof of the Johnson-Lindenstrauss Lemma." `_ Examples -------- >>> from sklearn.random_projection import johnson_lindenstrauss_min_dim >>> johnson_lindenstrauss_min_dim(1e6, eps=0.5) np.int64(663) >>> johnson_lindenstrauss_min_dim(1e6, eps=[0.5, 0.1, 0.01]) array([ 663, 11841, 1112658]) >>> johnson_lindenstrauss_min_dim([1e4, 1e5, 1e6], eps=0.1) array([ 7894, 9868, 11841]) rz1The JL bound is defined for eps in ]0, 1[, got %rrz?The JL bound is defined for n_samples greater than zero, got %r)npasarrayany ValueErrorlogastypeint64)rr denominators _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/random_projection.pyrr8sZ **S/C 9%I vvcSjRVVC1H-LsRSS vvi1n M   6A:#q&1*-K y! !K / 7 7 AAct|dk(rdtj|z }|S|dks|dkDrtd|z|S)z.Factorize density check according to Li et al.autorrz)Expected density in range ]0, 1], got: %r)r)sqrtr,)density n_featuress r1_check_densityr8sI&bggj)) N A1DwNOO Nr2cP|dkrtd|z|dkrtd|zy)z9Factorize argument checking for random matrix generation.rz.n_components must be strictly positive, got %dz,n_features must be strictly positive, got %dN)r,) n_componentsr7s r1_check_input_sizer;s>q <| K  QG*TUUr2ct||t|}|jddtj|z ||f}|S)abGenerate a dense Gaussian random matrix. The components of the random matrix are drawn from N(0, 1.0 / n_components). Read more in the :ref:`User Guide `. Parameters ---------- n_components : int, Dimensionality of the target projection space. n_features : int, Dimensionality of the original source space. random_state : int, RandomState instance or None, default=None Controls the pseudo random number generator used to generate the matrix at fit time. Pass an int for reproducible output across multiple function calls. See :term:`Glossary `. Returns ------- components : ndarray of shape (n_components, n_features) The generated Gaussian random matrix. See Also -------- GaussianRandomProjection r%?)locscalesize)r;rnormalr)r5)r:r7 random_staterng componentss r1_gaussian_random_matrixrEsN@lJ/ \ *C sRWW\22, 9SJ r2ct||t||}t|}|dk(r6|jdd||fdzdz }dt j |z |zSg}d}|g}t |D]I} |j||} t|| |} |j| || z }|j|Kt j|}|jddt j|dzdz } tj| ||f||f}t j d|z t j |z |zS)aGeneralized Achlioptas random sparse matrix for random projection. Setting density to 1 / 3 will yield the original matrix by Dimitris Achlioptas while setting a lower value will yield the generalization by Ping Li et al. If we note :math:`s = 1 / density`, the components of the random matrix are drawn from: - -sqrt(s) / sqrt(n_components) with probability 1 / 2s - 0 with probability 1 - 1 / s - +sqrt(s) / sqrt(n_components) with probability 1 / 2s Read more in the :ref:`User Guide `. Parameters ---------- n_components : int, Dimensionality of the target projection space. n_features : int, Dimensionality of the original source space. density : float or 'auto', default='auto' Ratio of non-zero component in the random projection matrix in the range `(0, 1]` If density = 'auto', the value is set to the minimum density as recommended by Ping Li et al.: 1 / sqrt(n_features). Use density = 1 / 3.0 if you want to reproduce the results from Achlioptas, 2001. random_state : int, RandomState instance or None, default=None Controls the pseudo random number generator used to generate the matrix at fit time. Pass an int for reproducible output across multiple function calls. See :term:`Glossary `. Returns ------- components : {ndarray, sparse matrix} of shape (n_components, n_features) The generated Gaussian random matrix. Sparse matrix will be of CSR format. See Also -------- SparseRandomProjection References ---------- .. [1] Ping Li, T. Hastie and K. W. Church, 2006, "Very Sparse Random Projections". https://web.stanford.edu/~hastie/Papers/Ping/KDD06_rp.pdf .. [2] D. Achlioptas, 2001, "Database-friendly random projections", https://cgi.di.uoa.gr/~optas/papers/jl.pdf rg?r&rrB)r@)shape) r;r8rbinomialr)r5rangerappend concatenater@sp csr_matrix) r:r7r6rBrCrDindicesoffsetindptr_ n_nonzero_i indices_idatas r1_sparse_random_matrixrVsTzlJ/Wj1G \ *C!|\\!S<*DEIAM 277<((:55|$ "A,,z7;K2KcI NN9 % k !F MM& ! "..)||As)9|:Q>B]] 7F #L*+E wwq7{#bggl&;;jHHr2ceZdZUdZeedddedhgeedddgd gd gd Ze e d <e dd ddddZ e dZ dZedddZdZfdZxZS)BaseRandomProjectionz~Base class for random projections. Warning: This class should not be used directly. Use derived classes instead. rNrrr4rrbooleanrBr:r compute_inverse_componentsrB_parameter_constraintsr#Fr r[rBc<||_||_||_||_yNrZ)selfr:r r[rBs r1__init__zBaseRandomProjection.__init__Fs#)*D'(r2cy)aGenerate the random projection matrix. Parameters ---------- n_components : int, Dimensionality of the target projection space. n_features : int, Dimensionality of the original source space. Returns ------- components : {ndarray, sparse matrix} of shape (n_components, n_features) The generated random matrix. Sparse matrix will be of CSR format. N)r`r:r7s r1_make_random_matrixz(BaseRandomProjection._make_random_matrixTsr2c|j}tj|r|j}t j |dS)z9Compute the pseudo-inverse of the (densified) components.F) check_finite) components_rMissparsetoarrayrpinv)r`rDs r1_compute_inverse_componentsz0BaseRandomProjection._compute_inverse_componentsgs8%% ;;z "#++-J{{:E::r2Tr!ct||ddgtjtjg}|j\}}|j dk(rt ||j|_|jdkr%td|j||jfz|j|kDrqtd|j||j|fz|j |kDr+tjd |d |j d t|j |_|j|j|j|jd |_|j"r|j%|_|j |_|S)aGenerate a sparse random projection matrix. Parameters ---------- X : {ndarray, sparse matrix} of shape (n_samples, n_features) Training set: only the shape is used to find optimal random matrix dimensions based on the theory referenced in the afore mentioned papers. y : Ignored Not used, present here for API consistency by convention. Returns ------- self : object BaseRandomProjection class instance. csrcsc) accept_sparsedtyper4rrzIeps=%f and n_samples=%d lead to a target dimension of %d which is invalidzseps=%f and n_samples=%d lead to a target dimension of %d which is larger than the original space with n_features=%dz[The number of components is higher than the number of features: n_features < n_components (z < z8).The dimensionality of the problem will not be reduced.F)copy)rr)float64float32rHr:rr n_components_r,warningswarnr rdr.rprgr[rkinverse_components__n_features_out)r`Xyrr7s r1fitzBaseRandomProjection.fitns&  !E5>"**bjj9Q !" :    &!>#"D !!Q& *-1XXy$BTBT,UV ##j0 $xxD,>,> KL  :- "4#4#46. "&!2!2D  33     &u& %   * *'+'G'G'ID $ $00 r2ct|t|tjtjgd}|j r||j jzS|j}||jzS)aProject data back to its original space. Returns an array X_original whose transform would be X. Note that even if X is sparse, X_original is dense: this may use a lot of RAM. If `compute_inverse_components` is False, the inverse of the components is computed during each call to `inverse_transform` which can be costly. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_components) Data to be transformed back. Returns ------- X_original : ndarray of shape (n_samples, n_features) Reconstructed data. )rmrn)rpro) rrr)rrrsr[rwTrk)r`ryinverse_componentss r1inverse_transformz&BaseRandomProjection.inverse_transformsh&  "**bjj!9 X  * *t//111 1!==?%''''r2clt|}ddg|j_d|j_|S)NrrrsT)super__sklearn_tags__transformer_tagspreserves_dtype input_tagssparse)r`tags __class__s r1rz%BaseRandomProjection.__sklearn_tags__s4w')1:I0F-!% r2r4r_)__name__ __module__ __qualname____doc__rrrrr\dict__annotations__rrardrkr r{rr __classcell__rs@r1rXrX3s Xq$v 6 x  q$y9:'0k'($D ) #( ) )  $;5A6AF(:r2rX) metaclassc<eZdZdZ d ddddfd ZdZdZxZS) ra Reduce dimensionality through Gaussian random projection. The components of the random matrix are drawn from N(0, 1 / n_components). Read more in the :ref:`User Guide `. .. versionadded:: 0.13 Parameters ---------- n_components : int or 'auto', default='auto' Dimensionality of the target projection space. n_components can be automatically adjusted according to the number of samples in the dataset and the bound given by the Johnson-Lindenstrauss lemma. In that case the quality of the embedding is controlled by the ``eps`` parameter. It should be noted that Johnson-Lindenstrauss lemma can yield very conservative estimated of the required number of components as it makes no assumption on the structure of the dataset. eps : float, default=0.1 Parameter to control the quality of the embedding according to the Johnson-Lindenstrauss lemma when `n_components` is set to 'auto'. The value should be strictly positive. Smaller values lead to better embedding and higher number of dimensions (n_components) in the target projection space. compute_inverse_components : bool, default=False Learn the inverse transform by computing the pseudo-inverse of the components during fit. Note that computing the pseudo-inverse does not scale well to large matrices. random_state : int, RandomState instance or None, default=None Controls the pseudo random number generator used to generate the projection matrix at fit time. Pass an int for reproducible output across multiple function calls. See :term:`Glossary `. Attributes ---------- n_components_ : int Concrete number of components computed when n_components="auto". components_ : ndarray of shape (n_components, n_features) Random matrix used for the projection. inverse_components_ : ndarray of shape (n_features, n_components) Pseudo-inverse of the components, only computed if `compute_inverse_components` is True. .. versionadded:: 1.1 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 -------- SparseRandomProjection : Reduce dimensionality through sparse random projection. Examples -------- >>> import numpy as np >>> from sklearn.random_projection import GaussianRandomProjection >>> rng = np.random.RandomState(42) >>> X = rng.rand(25, 3000) >>> transformer = GaussianRandomProjection(random_state=rng) >>> X_new = transformer.fit_transform(X) >>> X_new.shape (25, 2759) r#FNr]c,t|||||yNrZ)rra)r`r:r r[rBrs r1raz!GaussianRandomProjection.__init__*s# %'A%  r2cHt|j}t|||S)aGenerate the random projection matrix. Parameters ---------- n_components : int, Dimensionality of the target projection space. n_features : int, Dimensionality of the original source space. Returns ------- components : ndarray of shape (n_components, n_features) The generated random matrix. rG)rrBrEr`r:r7rBs r1rdz,GaussianRandomProjection._make_random_matrix9s( *$*;*;< & *<  r2ct|t||ddgdtjtjg}||j j zS)awProject the data by using matrix product with the random matrix. Parameters ---------- X : {ndarray, sparse matrix} of shape (n_samples, n_features) The input data to project into a smaller dimensional space. Returns ------- X_new : ndarray of shape (n_samples, n_components) Projected array. rmrnFroresetrp)rrr)rrrsrgr}r`rys r1 transformz"GaussianRandomProjection.transformNsO     %.::rzz*  4##%%%%r2r)rrrrrardrrrs@r1rrs.Qj   #(   *&r2rceZdZUdZiej eedddedhgdgdZe e d < ddd d d d d fd Z dZ dZ xZS)raTReduce dimensionality through sparse random projection. Sparse random matrix is an alternative to dense random projection matrix that guarantees similar embedding quality while being much more memory efficient and allowing faster computation of the projected data. If we note `s = 1 / density` the components of the random matrix are drawn from: .. code-block:: text -sqrt(s) / sqrt(n_components) with probability 1 / 2s 0 with probability 1 - 1 / s +sqrt(s) / sqrt(n_components) with probability 1 / 2s Read more in the :ref:`User Guide `. .. versionadded:: 0.13 Parameters ---------- n_components : int or 'auto', default='auto' Dimensionality of the target projection space. n_components can be automatically adjusted according to the number of samples in the dataset and the bound given by the Johnson-Lindenstrauss lemma. In that case the quality of the embedding is controlled by the ``eps`` parameter. It should be noted that Johnson-Lindenstrauss lemma can yield very conservative estimated of the required number of components as it makes no assumption on the structure of the dataset. density : float or 'auto', default='auto' Ratio in the range (0, 1] of non-zero component in the random projection matrix. If density = 'auto', the value is set to the minimum density as recommended by Ping Li et al.: 1 / sqrt(n_features). Use density = 1 / 3.0 if you want to reproduce the results from Achlioptas, 2001. eps : float, default=0.1 Parameter to control the quality of the embedding according to the Johnson-Lindenstrauss lemma when n_components is set to 'auto'. This value should be strictly positive. Smaller values lead to better embedding and higher number of dimensions (n_components) in the target projection space. dense_output : bool, default=False If True, ensure that the output of the random projection is a dense numpy array even if the input and random projection matrix are both sparse. In practice, if the number of components is small the number of zero components in the projected data will be very small and it will be more CPU and memory efficient to use a dense representation. If False, the projected data uses a sparse representation if the input is sparse. compute_inverse_components : bool, default=False Learn the inverse transform by computing the pseudo-inverse of the components during fit. Note that the pseudo-inverse is always a dense array, even if the training data was sparse. This means that it might be necessary to call `inverse_transform` on a small batch of samples at a time to avoid exhausting the available memory on the host. Moreover, computing the pseudo-inverse does not scale well to large matrices. random_state : int, RandomState instance or None, default=None Controls the pseudo random number generator used to generate the projection matrix at fit time. Pass an int for reproducible output across multiple function calls. See :term:`Glossary `. Attributes ---------- n_components_ : int Concrete number of components computed when n_components="auto". components_ : sparse matrix of shape (n_components, n_features) Random matrix used for the projection. Sparse matrix will be of CSR format. inverse_components_ : ndarray of shape (n_features, n_components) Pseudo-inverse of the components, only computed if `compute_inverse_components` is True. .. versionadded:: 1.1 density_ : float in range 0.0 - 1.0 Concrete density computed from when density = "auto". 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 -------- GaussianRandomProjection : Reduce dimensionality through Gaussian random projection. References ---------- .. [1] Ping Li, T. Hastie and K. W. Church, 2006, "Very Sparse Random Projections". https://web.stanford.edu/~hastie/Papers/Ping/KDD06_rp.pdf .. [2] D. Achlioptas, 2001, "Database-friendly random projections", https://cgi.di.uoa.gr/~optas/papers/jl.pdf Examples -------- >>> import numpy as np >>> from sklearn.random_projection import SparseRandomProjection >>> rng = np.random.RandomState(42) >>> X = rng.rand(25, 3000) >>> transformer = SparseRandomProjection(random_state=rng) >>> X_new = transformer.fit_transform(X) >>> X_new.shape (25, 2759) >>> # very few components are non-zero >>> np.mean(transformer.components_ != 0) np.float64(0.0182) r%r=rightrr4rY)r6 dense_outputr\r#FN)r6r rr[rBcHt|||||||_||_yr)rrarr6)r`r:r6r rr[rBrs r1razSparseRandomProjection.__init__s4 %'A%  ) r2ct|j}t|j||_t |||j|S)aGenerate the random projection matrix Parameters ---------- n_components : int Dimensionality of the target projection space. n_features : int Dimensionality of the original source space. Returns ------- components : sparse matrix of shape (n_components, n_features) The generated random matrix in CSR format. )r6rB)rrBr8r6density_rVrs r1rdz*SparseRandomProjection._make_random_matrix s@"*$*;*;< &t||Z@ $ *dmm,  r2ct|t||ddgdtjtjg}t ||j j|jS)aProject the data by using matrix product with the random matrix. Parameters ---------- X : {ndarray, sparse matrix} of shape (n_samples, n_features) The input data to project into a smaller dimensional space. Returns ------- X_new : {ndarray, sparse matrix} of shape (n_samples, n_components) Projected array. It is a sparse matrix only when the input is sparse and `dense_output = False`. rmrnFr)r) rrr)rrrsrrgr}rrs r1rz SparseRandomProjection.transform!sY     %.::rzz*  q$"2"2"4"44CTCTUUr2r)rrrrrXr\rrrrrrardrrrs@r1rrgsvFP$  5 5$T3GRS" $D #(( .Vr2rr_)r4N)-rruabcrrnumbersrrnumpyr) scipy.sparserrMscipyrbaser r r r exceptionsr utilsrutils._param_validationrrr utils.extmathr utils.randomrutils.validationrrr__all__rr8r;rErVrXrrrcr2r1rs6'" 2%JJ*4II "HT1d6$JKhtQ)DE#' 58SBSBlV%P_ID`#%5}PW`FN&3N&bQV1QVr2