`L idZddlZddlmZddlmZddlmZddl m Z dd l m Z m Z d Zd Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZGdde Zy)zGaussian Mixture Model.N)linalg) check_array) StrOptions) row_norms) BaseMixture _check_shapecht|tjtjgd}t ||fdt tj |dst tj|dr6tdtj|tj|fz|jtjk(rdnd}tjtjdtj|z d| s!td tj|z|S) a)Check the user provided 'weights'. Parameters ---------- weights : array-like of shape (n_components,) The proportions of components of each mixture. n_components : int Number of components. Returns ------- weights : array, shape (n_components,) Fdtype ensure_2dweights?z]The parameter 'weights' should be in the range [0, 1], but got max value %.5f, min value %.5fư>g:0yE>)atolzIThe parameter 'weights' should be normalized, but got sum(weights) = %.5f)rnpfloat64float32r anylessgreater ValueErrorminmaxr allcloseabssum)r n_componentsrs g/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/mixture/_gaussian_mixture.py_check_weightsr"s'"**bjj)AUSGt|||||jdS)aEstimate the spherical variance values. Parameters ---------- responsibilities : array-like of shape (n_samples, n_components) X : array-like of shape (n_samples, n_features) nk : array-like of shape (n_components,) means : array-like of shape (n_components, n_features) reg_covar : float Returns ------- variances : array, shape (n_components,) The variance values of each components. r)rWmean)rGrHrIr%rJs r!(_estimate_gaussian_covariances_sphericalrZs#( /tQE9 M R RST UUr#cB|jddtj|jjzz}tj |j ||ddtjfz } ttttd||||||}|||fS)aXEstimate the Gaussian distribution parameters. Parameters ---------- X : array-like of shape (n_samples, n_features) The input data array. resp : array-like of shape (n_samples, n_components) The responsibilities for each data sample in X. reg_covar : float The regularization added to the diagonal of the covariance matrices. covariance_type : {'full', 'tied', 'diag', 'spherical'} The type of precision matrices. Returns ------- nk : array-like of shape (n_components,) The numbers of data samples in the current components. means : array-like of shape (n_components, n_features) The centers of the current components. covariances : array-like The covariance matrix of the current components. The shape depends of the covariance_type. raxis Nr:) rrfinfor epsrEr/rVrNrTrWrZ)rHrGrJr,rIr%rKs r!_estimate_gaussian_parametersras: q B$**!5!9!99 9B FF4661 1bjj= 1 1E333=    QE9 6K uk !!r#chd}|j}|tjk(r|dz }|dk(r|j\}}}tj|||f|}t |D]W\}} t j| d} t j| tj||dj||<Y|S|dk(r`|j\}} t j|d} t j| tj||dj}|Stjtj|dr t|d tj|z }|S#t j$r t|wxYw#t j$r t|wxYw) a Compute the Cholesky decomposition of the precisions. Parameters ---------- covariances : array-like The covariance matrix of the current components. The shape depends of the covariance_type. covariance_type : {'full', 'tied', 'diag', 'spherical'} The type of precision matrices. Returns ------- precisions_cholesky : array-like The cholesky decomposition of sample precisions of the current components. The shape depends of the covariance_type. zFitting the mixture model failed because some components have ill-defined empirical covariance (for instance caused by singleton or collapsed samples). Try to decrease the number of components, increase reg_covar, or scale the input data.zX The numerical accuracy can also be improved by passing float64 data instead of float32.r8rATlowerr;rr)r rrrBrC enumeratercholesky LinAlgErrorrsolve_triangulareyer/rr)sqrt) rKr, estimate_precision_error_messager r r&_precisions_cholrLrScov_chols r!_compute_precision_choleskyro*s& 7%   E  ( ( ( & &1&7&7# j!((L*j#IQVW&{3 MAz C!??:TB"(!8!8"&&59"a A   *  F "#)) : ?{$?H!11 bffZu5T !   66"-- S1 2=> > 44 %%% C !ABB C!! ?=> > ?s(E0F0FF1cRtjtj|S)z)Reverse the rows and columns of an array.)rflipudfliplr)arrays r! _flipudlrrtds 99RYYu% &&r#c <|dk(rLtj|Dcgc]+}ttjt|d-c}}|S|dk(r+ttjt|d}|Stj |}|Scc}w)aCompute the Cholesky decomposition of precisions using precisions themselves. As implemented in :func:`_compute_precision_cholesky`, the `precisions_cholesky_` is an upper-triangular matrix for each Gaussian component, which can be expressed as the $UU^T$ factorization of the precision matrix for each Gaussian component, where $U$ is an upper-triangular matrix. In order to use the Cholesky decomposition to get $UU^T$, the precision matrix $\Lambda$ needs to be permutated such that its rows and columns are reversed, which can be done by applying a similarity transformation with an exchange matrix $J$, where the 1 elements reside on the anti-diagonal and all other elements are 0. In particular, the Cholesky decomposition of the transformed precision matrix is $J\Lambda J=LL^T$, where $L$ is a lower-triangular matrix. Because $\Lambda=UU^T$ and $J=J^{-1}=J^T$, the `precisions_cholesky_` for each Gaussian component can be expressed as $JLJ$. Refer to #26415 for details. Parameters ---------- precisions : array-like The precision matrix of the current components. The shape depends on the covariance_type. covariance_type : {'full', 'tied', 'diag', 'spherical'} The type of precision matrices. Returns ------- precisions_cholesky : array-like The cholesky decomposition of sample precisions of the current components. The shape depends on the covariance_type. r8Trcr;)rrsrtrrfrj)r4r,r+precisions_choleskys r!+_compute_precision_cholesky_from_precisionsrwisD&  hh", &//)I*>dKL    F "' OOIj1 >  !ggj1  s0Bc |dk(rY|j\}}}tjtj|j |ddddd|dzfd}|S|dk(r=tjtjtj |}|S|dk(r,tjtj|d}|S|tj|z}|S)aCompute the log-det of the cholesky decomposition of matrices. Parameters ---------- matrix_chol : array-like Cholesky decompositions of the matrices. 'full' : shape of (n_components, n_features, n_features) 'tied' : shape of (n_features, n_features) 'diag' : shape of (n_components, n_features) 'spherical' : shape of (n_components,) covariance_type : {'full', 'tied', 'diag', 'spherical'} n_features : int Number of features. Returns ------- log_det_precision_chol : array-like of shape (n_components,) The determinant of the precision matrix for each component. r8Nrr\r;r<)rBrrlogreshaper<) matrix_cholr,r&r rl log_det_chols r!_compute_log_det_choleskyr~s,& (.. avv FF;&&|R8 ?  :q255y 1 8 8 AAHL MPW WWr#ceZdZUdZiej ehdgddgddgddgdZeed< ddd d d dd ddddd ddd fd Z dZ fdZ dZ dZ dZdZdZdZdZdZdZdZxZS)GaussianMixtureaGaussian Mixture. Representation of a Gaussian mixture model probability distribution. This class allows to estimate the parameters of a Gaussian mixture distribution. Read more in the :ref:`User Guide `. .. versionadded:: 0.18 Parameters ---------- n_components : int, default=1 The number of mixture components. covariance_type : {'full', 'tied', 'diag', 'spherical'}, default='full' String describing the type of covariance parameters to use. Must be one of: - 'full': each component has its own general covariance matrix. - 'tied': all components share the same general covariance matrix. - 'diag': each component has its own diagonal covariance matrix. - 'spherical': each component has its own single variance. For an example of using `covariance_type`, refer to :ref:`sphx_glr_auto_examples_mixture_plot_gmm_selection.py`. tol : float, default=1e-3 The convergence threshold. EM iterations will stop when the lower bound average gain is below this threshold. reg_covar : float, default=1e-6 Non-negative regularization added to the diagonal of covariance. Allows to assure that the covariance matrices are all positive. max_iter : int, default=100 The number of EM iterations to perform. n_init : int, default=1 The number of initializations to perform. The best results are kept. init_params : {'kmeans', 'k-means++', 'random', 'random_from_data'}, default='kmeans' The method used to initialize the weights, the means and the precisions. String must be one of: - 'kmeans' : responsibilities are initialized using kmeans. - 'k-means++' : use the k-means++ method to initialize. - 'random' : responsibilities are initialized randomly. - 'random_from_data' : initial means are randomly selected data points. .. versionchanged:: v1.1 `init_params` now accepts 'random_from_data' and 'k-means++' as initialization methods. weights_init : array-like of shape (n_components, ), default=None The user-provided initial weights. If it is None, weights are initialized using the `init_params` method. means_init : array-like of shape (n_components, n_features), default=None The user-provided initial means, If it is None, means are initialized using the `init_params` method. precisions_init : array-like, default=None The user-provided initial precisions (inverse of the covariance matrices). If it is None, precisions are initialized using the 'init_params' method. The shape depends on 'covariance_type':: (n_components,) if 'spherical', (n_features, n_features) if 'tied', (n_components, n_features) if 'diag', (n_components, n_features, n_features) if 'full' random_state : int, RandomState instance or None, default=None Controls the random seed given to the method chosen to initialize the parameters (see `init_params`). In addition, it controls the generation of random samples from the fitted distribution (see the method `sample`). Pass an int for reproducible output across multiple function calls. See :term:`Glossary `. warm_start : bool, default=False If 'warm_start' is True, the solution of the last fitting is used as initialization for the next call of fit(). This can speed up convergence when fit is called several times on similar problems. In that case, 'n_init' is ignored and only a single initialization occurs upon the first call. See :term:`the Glossary `. verbose : int, default=0 Enable verbose output. If 1 then it prints the current initialization and each iteration step. If greater than 1 then it prints also the log probability and the time needed for each step. verbose_interval : int, default=10 Number of iteration done before the next print. Attributes ---------- weights_ : array-like of shape (n_components,) The weights of each mixture components. means_ : array-like of shape (n_components, n_features) The mean of each mixture component. covariances_ : array-like The covariance of each mixture component. The shape depends on `covariance_type`:: (n_components,) if 'spherical', (n_features, n_features) if 'tied', (n_components, n_features) if 'diag', (n_components, n_features, n_features) if 'full' For an example of using covariances, refer to :ref:`sphx_glr_auto_examples_mixture_plot_gmm_covariances.py`. precisions_ : array-like The precision matrices for each component in the mixture. A precision matrix is the inverse of a covariance matrix. A covariance matrix is symmetric positive definite so the mixture of Gaussian can be equivalently parameterized by the precision matrices. Storing the precision matrices instead of the covariance matrices makes it more efficient to compute the log-likelihood of new samples at test time. The shape depends on `covariance_type`:: (n_components,) if 'spherical', (n_features, n_features) if 'tied', (n_components, n_features) if 'diag', (n_components, n_features, n_features) if 'full' precisions_cholesky_ : array-like The cholesky decomposition of the precision matrices of each mixture component. A precision matrix is the inverse of a covariance matrix. A covariance matrix is symmetric positive definite so the mixture of Gaussian can be equivalently parameterized by the precision matrices. Storing the precision matrices instead of the covariance matrices makes it more efficient to compute the log-likelihood of new samples at test time. The shape depends on `covariance_type`:: (n_components,) if 'spherical', (n_features, n_features) if 'tied', (n_components, n_features) if 'diag', (n_components, n_features, n_features) if 'full' converged_ : bool True when convergence of the best fit of EM was reached, False otherwise. n_iter_ : int Number of step used by the best fit of EM to reach the convergence. lower_bound_ : float Lower bound value on the log-likelihood (of the training data with respect to the model) of the best fit of EM. lower_bounds_ : array-like of shape (`n_iter_`,) The list of lower bound values on the log-likelihood from each iteration of the best fit of EM. 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 -------- BayesianGaussianMixture : Gaussian mixture model fit with a variational inference. Examples -------- >>> import numpy as np >>> from sklearn.mixture import GaussianMixture >>> X = np.array([[1, 2], [1, 4], [1, 0], [10, 2], [10, 4], [10, 0]]) >>> gm = GaussianMixture(n_components=2, random_state=0).fit(X) >>> gm.means_ array([[10., 2.], [ 1., 2.]]) >>> gm.predict([[0, 0], [12, 3]]) array([1, 0]) For a comparison of Gaussian Mixture with other clustering algorithms, see :ref:`sphx_glr_auto_examples_cluster_plot_cluster_comparison.py` >r<r8r;r=z array-likeN)r, weights_init means_initprecisions_init_parameter_constraintsrr8gMbP?rdkmeansFrr^) r,tolrJmax_itern_init init_paramsrrr random_state warm_startverboseverbose_intervalc pt|||||||| | | | ||_||_| |_| |_y)N) r rrJrrrrrrr)super__init__r,rrr)selfr r,rrJrrrrrrrrrr __class__s r!rzGaussianMixture.__init__sV$ %#%!-   /($.r#cd|j\}}|j%t|j|j|_|j&t |j|j||_|j 2t|j |j|j||_yy)z7Check the Gaussian mixture parameters are well defined.N) rBrr"r rr'rr?r,)rrHrlr&s r!_check_parametersz!GaussianMixture._check_parameterss :    ( .t/@/@$BSBS TD  ?? &*!2!2JDO    +#4$$$$!! $D  ,r#c|jduxs|jduxs|jdu}|rt|||y|j |dyN)rrrr_initialize_parameters _initialize)rrHr compute_resprs r!rz&GaussianMixture._initialize_parameterss`    % ,$& ,##t+   G *1l ;   Q %r#c|j\}}d\}}}|7t|||j|j\}}}|j||z}|j|n |j|_|j |n |j |_|j#||_ t||j|_ yt|j|j|_ y)zInitialization of the Gaussian mixture parameters. Parameters ---------- X : array-like of shape (n_samples, n_features) resp : array-like of shape (n_samples, n_components) )NNNN) rBrarJr,rweights_rmeans_r covariances_roprecisions_cholesky_rw)rrHrGrrlrr%rKs r!rzGaussianMixture._initializesww 1&6#  *G4)=)=+ 'GUK  (9$#'#4#4#<$BSBS #6eDOO    ' +D (CT11)D %)T$$d&:&:)D %r#c8t|tj||j|j\|_|_|_|xj |j jzc_t|j|j|_ y)a*M step. Parameters ---------- X : array-like of shape (n_samples, n_features) log_resp : array-like of shape (n_samples, n_components) Logarithm of the posterior probabilities (or responsibilities) of the point of each sample in X. N) rarexprJr,rrrrror)rrHlog_resps r!_m_stepzGaussianMixture._m_step3st9V rvvh1E1E9 5 t{D$5 **,, $?   t33% !r#cZt||j|j|jSr)rrrr,rrHs r!_estimate_log_probz"GaussianMixture._estimate_log_probFs(* t{{D55t7K7K  r#c@tj|jSr)rrzrrs r!_estimate_log_weightsz%GaussianMixture._estimate_log_weightsKsvvdmm$$r#c|Sr)rrl log_prob_norms r!_compute_lower_boundz$GaussianMixture._compute_lower_boundNsr#c^|j|j|j|jfSr)rrrrrs r!_get_parameterszGaussianMixture._get_parametersQs, MM KK     % %   r#cN|\|_|_|_|_|jj\}}|jj }|j dk(rotj|j|_ t|jD]2\}}tj||j|j|<4y|j dk(r:tj|j|jj|_ y|jdz|_ y)Nr8r;r) rrrrrBr r,r empty_like precisions_rerEr/)rparamsrlr&r rLrs r!_set_parameterszGaussianMixture._set_parametersYs   M K    % )) :))//   6 )!}}T-F-FGD  )$*C*C D E 9&(ffY &D  # E ! !V +!vv))4+D+D+F+F D  $88!;D r#c|jj\}}|jdk(r|j|z|dzzdz }nU|jdk(r|j|z}n6|jdk(r ||dzzdz }n|jdk(r |j}||jz}t |z|jzdz S)z2Return the number of free parameters in the model.r8rrr<r;r=)rrBr,r int)rrlr& cov_params mean_paramss r! _n_parameterszGaussianMixture._n_parametersqs )) :   6 )**Z7:>JSPJ  ! !V +**Z7J  ! !V +#zA~6` for more details regarding the formulation of the BIC used. For an example of GMM selection using `bic` information criterion, refer to :ref:`sphx_glr_auto_examples_mixture_plot_gmm_selection.py`. Parameters ---------- X : array of shape (n_samples, n_dimensions) The input samples. Returns ------- bic : float The lower the better. r)scorerBrrrzrs r!biczGaussianMixture.bicsR&DJJqM!AGGAJ.1C1C1E GGAJI 2   r#crd|j|z|jdzd|jzzS)aAkaike information criterion for the current model on the input X. You can refer to this :ref:`mathematical section ` for more details regarding the formulation of the AIC used. Parameters ---------- X : array of shape (n_samples, n_dimensions) The input samples. Returns ------- aic : float The lower the better. rrr)rrBrrs r!aiczGaussianMixture.aics7 DJJqM!AGGAJ.T5G5G5I1IIIr#)r)__name__ __module__ __qualname____doc__r rrdict__annotations__rrrrrrrrrrrrr __classcell__)rs@r!rrsAF$  , ,$&'LMN%t,#T*($/ $D"/ !"/H( &> & % <0 E .Jr#r)rnumpyrscipyrutilsrutils._param_validationr utils.extmathr_baser r r"r'r-r2r6r?rNrTrWrZrarortrwr~rrrr#r!rs 0%, !H.P  7 /l:8+2V.%"P7t' /h%P;X|cJkcJr#