
    -i)K                         S r SSKrSSKJrJr  SSKJrJr  SSKJr  SSK	r
SSKJr  SSKJr  SS	KJrJrJr  SS
KJr  SSKJr  SSKJr  SSKJrJr  SSKJrJr  S r " S S\\\S9rg)zBase class for mixture models.    N)ABCMetaabstractmethod)IntegralReal)time)	logsumexp   )cluster)BaseEstimatorDensityMixin_fit_context)kmeans_plusplus)ConvergenceWarning)check_random_state)Interval
StrOptions)check_is_fittedvalidate_datac                     [         R                  " U 5      n U R                  U:w  a!  [        SU< SU< SU R                  < 35      eg)zyValidate the shape of the input parameter 'param'.

Parameters
----------
param : array

param_shape : tuple

name : str
zThe parameter 'z' should have the shape of z
, but got N)nparrayshape
ValueError)paramparam_shapenames      H/var/www/html/venv/lib/python3.13/site-packages/sklearn/mixture/_base.py_check_shaper      s?     HHUOE{{k![%++/
 	
 "    c                      \ rS rSr% Sr\" \SSSS9/\" \SSSS9/\" \SSSS9/\" \SSSS9/\" \SSSS9/\" 1 S	k5      /S
/S/S/\" \SSSS9/S.
r	\
\S'   S r\S 5       rS r\S 5       rS(S jr\" SS9S(S j5       rS r\S 5       r\S 5       r\S 5       rS rS(S jrS rS rS)S jrS  r\S! 5       r\S" 5       rS# r S$ r!S% r"S& r#S'r$g)*BaseMixture*   zBase class for mixture models.

This abstract class specifies an interface for all mixture classes and
provides basic common methods for mixture models.
   Nleft)closedg        r   >   kmeansrandomrandom_from_data	k-means++random_statebooleanverbose
n_componentstol	reg_covarmax_itern_initinit_paramsr*   
warm_startr,   verbose_interval_parameter_constraintsc                 |    Xl         X l        X0l        X@l        XPl        X`l        Xpl        Xl        Xl        Xl	        g Nr-   )selfr.   r/   r0   r1   r2   r3   r*   r4   r,   r5   s              r   __init__BaseMixture.__init__@   s:     )" &($ 0r   c                     g)zwCheck initial parameters of the derived class.

Parameters
----------
X : array-like of shape  (n_samples, n_features)
N r9   Xs     r   _check_parametersBaseMixture._check_parametersX        	r   c                    UR                   u  p4U R                  S:X  a  [        R                  " X0R                  4UR
                  S9n[        R                  " U R                  SUS9R                  U5      R                  nSU[        R                  " U5      U4'   GO\U R                  S:X  a^  [        R                  " UR                  X0R                  4S9UR
                  S9nXUR                  SS9SS2[        R                  4   -  nOU R                  S	:X  ai  [        R                  " X0R                  4UR
                  S9nUR                  X0R                  S
S9nSXW[        R                  " U R                  5      4'   OuU R                  S:X  ae  [        R                  " X0R                  4UR
                  S9n[!        UU R                  US9u  pGSXW[        R                  " U R                  5      4'   U R#                  UW5        g)a  Initialize the model parameters.

Parameters
----------
X : array-like of shape  (n_samples, n_features)

random_state : RandomState
    A random number generator instance that controls the random seed
    used for the method chosen to initialize the parameters.
r&   dtyper#   )
n_clustersr2   r*   r'   sizeaxisNr(   F)rH   replacer)   )r*   )r   r3   r   zerosr.   rE   r
   KMeansfitlabels_arangeasarrayuniformsumnewaxischoicer   _initialize)r9   r?   r*   	n_samples_resplabelindicess           r   _initialize_parameters"BaseMixture._initialize_parametersb   s    ww	x'88Y(9(9:!''JD#00 Q  12D9%u,-)::$$96G6G*H$IQRQXQXD HH!H$Q

]33D!3388Y(9(9:!''JD")) 1 15 * G ;<D"))D$5$5667,88Y(9(9:!''JD(!!)JA
 ;<D"))D$5$5667D!r   c                     g)zInitialize the model parameters of the derived class.

Parameters
----------
X : array-like of shape  (n_samples, n_features)

resp : array-like of shape (n_samples, n_components)
Nr=   )r9   r?   rY   s      r   rV   BaseMixture._initialize   s     	r   c                 (    U R                  X5        U $ )aX  Estimate model parameters with the EM algorithm.

The method fits the model ``n_init`` times and sets the parameters with
which the model has the largest likelihood or lower bound. Within each
trial, the method iterates between E-step and M-step for ``max_iter``
times until the change of likelihood or lower bound is less than
``tol``, otherwise, a ``ConvergenceWarning`` is raised.
If ``warm_start`` is ``True``, then ``n_init`` is ignored and a single
initialization is performed upon the first call. Upon consecutive
calls, training starts where it left off.

Parameters
----------
X : array-like of shape (n_samples, n_features)
    List of n_features-dimensional data points. Each row
    corresponds to a single data point.

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

Returns
-------
self : object
    The fitted mixture.
)fit_predictr9   r?   ys      r   rN   BaseMixture.fit   s    6 	r   T)prefer_skip_nested_validationc                    [        X[        R                  [        R                  /SS9nUR                  S   U R
                  :  a(  [        SU R
                   SUR                  S    35      eU R                  U5        U R                  =(       a    [        U S5      (       + nU(       a  U R                  OSn[        R                  * n/ nSU l        [        U R                  5      nUR                  u  p[        U5       GHb  n
U R!                  U
5        U(       a  U R#                  X5        U(       a  [        R                  * OU R$                  n/ nU R&                  S:X  a  U R)                  5       nSnMw  Sn[        SU R&                  S-   5       H  nUnU R+                  U5      u  nnU R-                  UU5        U R/                  UU5      nUR1                  U5        UU-
  nU R3                  UU5        [5        U5      U R6                  :  d  M  S	n  O   U R9                  X5        X:  d  U[        R                  * :X  d  GMF  UnU R)                  5       nWnUnXl        GMe     U R                  (       d+  U R&                  S:  a  [:        R<                  " S
[>        5        U RA                  W5        WU l!        XPl        X`l"        U R+                  U5      u  n	nURG                  SS9$ )a>  Estimate model parameters using X and predict the labels for X.

The method fits the model n_init times and sets the parameters with
which the model has the largest likelihood or lower bound. Within each
trial, the method iterates between E-step and M-step for `max_iter`
times until the change of likelihood or lower bound is less than
`tol`, otherwise, a :class:`~sklearn.exceptions.ConvergenceWarning` is
raised. After fitting, it predicts the most probable label for the
input data points.

.. versionadded:: 0.20

Parameters
----------
X : array-like of shape (n_samples, n_features)
    List of n_features-dimensional data points. Each row
    corresponds to a single data point.

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

Returns
-------
labels : array, shape (n_samples,)
    Component labels.
r	   )rE   ensure_min_samplesr   z:Expected n_samples >= n_components but got n_components = z, n_samples = 
converged_r#   FTzBest performing initialization did not converge. Try different init parameters, or increase max_iter, tol, or check for degenerate data.rI   )$r   r   float64float32r   r.   r   r@   r4   hasattrr2   infrh   r   r*   range_print_verbose_msg_init_begr\   lower_bound_r1   _get_parameters_e_step_m_step_compute_lower_boundappend_print_verbose_msg_iter_endabsr/   _print_verbose_msg_init_endwarningswarnr   _set_parametersn_iter_lower_bounds_argmax)r9   r?   rc   do_initr2   max_lower_boundbest_lower_boundsr*   rW   rX   initlower_boundcurrent_lower_boundsbest_paramsbest_n_iter	convergedn_iterprev_lower_boundlog_prob_normlog_respchanges                        r   ra   BaseMixture.fit_predict   s   8 $"**bjj)AVWX771:)))**.*;*;)< = wwqzl, 
 	q! F74+FG 'Q66')$*;*;<ww	&MD,,T2++A<%,266'$2C2CK#% }}!"224!	#At}}q'89F'2$.2ll1o+M8LLH-"&";";Hm"TK(//<(+;;F44VVD6{TXX-$(	 : 00H0Ow4N&1O"&"6"6"8K"(K(<%&/OI "R 4==1#4MM9 # 	[)"+.
 ll1o8A&&r   c                 X    U R                  U5      u  p#[        R                  " U5      U4$ )a`  E step.

Parameters
----------
X : array-like of shape (n_samples, n_features)

Returns
-------
log_prob_norm : float
    Mean of the logarithms of the probabilities of each sample in X

log_responsibility : array, shape (n_samples, n_components)
    Logarithm of the posterior probabilities (or responsibilities) of
    the point of each sample in X.
)_estimate_log_prob_respr   mean)r9   r?   r   r   s       r   rq   BaseMixture._e_step(  s+      #'">">q"Aww}%x//r   c                     g)zM 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.
Nr=   )r9   r?   r   s      r   rr   BaseMixture._m_step;  s     	r   c                     g r8   r=   r9   s    r   rp   BaseMixture._get_parametersI      r   c                     g r8   r=   )r9   paramss     r   rz   BaseMixture._set_parametersM  r   r   c                 ^    [        U 5        [        XSS9n[        U R                  U5      SS9$ )aK  Compute the log-likelihood of each sample.

Parameters
----------
X : array-like of shape (n_samples, n_features)
    List of n_features-dimensional data points. Each row
    corresponds to a single data point.

Returns
-------
log_prob : array, shape (n_samples,)
    Log-likelihood of each sample in `X` under the current model.
Fresetr#   rI   )r   r   r   _estimate_weighted_log_probr>   s     r   score_samplesBaseMixture.score_samplesQ  s0     	$/99!<1EEr   c                 @    U R                  U5      R                  5       $ )a  Compute the per-sample average log-likelihood of the given data X.

Parameters
----------
X : array-like of shape (n_samples, n_dimensions)
    List of n_features-dimensional data points. Each row
    corresponds to a single data point.

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

Returns
-------
log_likelihood : float
    Log-likelihood of `X` under the Gaussian mixture model.
)r   r   rb   s      r   scoreBaseMixture.scored  s    " !!!$))++r   c                 h    [        U 5        [        XSS9nU R                  U5      R                  SS9$ )a4  Predict the labels for the data samples in X using trained model.

Parameters
----------
X : array-like of shape (n_samples, n_features)
    List of n_features-dimensional data points. Each row
    corresponds to a single data point.

Returns
-------
labels : array, shape (n_samples,)
    Component labels.
Fr   r#   rI   )r   r   r   r}   r>   s     r   predictBaseMixture.predictw  s7     	$///299q9AAr   c                 ~    [        U 5        [        XSS9nU R                  U5      u  p#[        R                  " U5      $ )aV  Evaluate the components' density for each sample.

Parameters
----------
X : array-like of shape (n_samples, n_features)
    List of n_features-dimensional data points. Each row
    corresponds to a single data point.

Returns
-------
resp : array, shape (n_samples, n_components)
    Density of each Gaussian component for each sample in X.
Fr   )r   r   r   r   exp)r9   r?   rX   r   s       r   predict_probaBaseMixture.predict_proba  s9     	$/2215vvhr   c                 r   [        U 5        US:  a  [        SU R                  -  5      eU R                  R                  u  p#[        U R                  5      nUR                  XR                  5      nU R                  S:X  ag  [        R                  " [        U R                  U R                  U5       VVVs/ s H!  u  pgnUR                  Xg[        U5      5      PM#     snnn5      n	OU R                  S:X  ac  [        R                  " [        U R                  U5       VVs/ s H*  u  phUR                  X`R                  [        U5      5      PM,     snn5      n	Ou[        R                  " [        U R                  U R                  U5       VVVs/ s H0  u  pgnUUR!                  X4S9[        R"                  " U5      -  -   PM2     snnn5      n	[        R$                  " ['        U5       V
Vs/ s H  u  p[        R(                  " X[        S9PM      snn
5      nX4$ s  snnnf s  snnf s  snnnf s  snn
f )a!  Generate random samples from the fitted Gaussian distribution.

Parameters
----------
n_samples : int, default=1
    Number of samples to generate.

Returns
-------
X : array, shape (n_samples, n_features)
    Randomly generated sample.

y : array, shape (nsamples,)
    Component labels.
r#   zNInvalid value for 'n_samples': %d . The sampling requires at least one sample.fulltiedrG   rD   )r   r   r.   means_r   r   r*   multinomialweights_covariance_typer   vstackzipcovariances_multivariate_normalintstandard_normalsqrtconcatenate	enumerater   )r9   rW   rX   
n_featuresrngn_samples_compr   
covariancesampler?   jrc   s               r   r   BaseMixture.sample  s     	q=$'+'8'8: 
 )) !2!23MMB6)		 7:T%6%67726 ++Dc&kJ7A !!V+		 +.dkk>*J*J ++D2C2CS[Q*JA 		
 7:T%6%67	726 ))/C)Dggj)**7		A NN<En<UV<UyqRWWVc*<UV
 v= Ws   7(H"1H&
7H,/%H3
c                 F    U R                  U5      U R                  5       -   $ )zEstimate the weighted log-probabilities, log P(X | Z) + log weights.

Parameters
----------
X : array-like of shape (n_samples, n_features)

Returns
-------
weighted_log_prob : array, shape (n_samples, n_component)
)_estimate_log_prob_estimate_log_weightsr>   s     r   r   'BaseMixture._estimate_weighted_log_prob  s#     &&q)D,F,F,HHHr   c                     g)zEstimate log-weights in EM algorithm, E[ log pi ] in VB algorithm.

Returns
-------
log_weight : array, shape (n_components, )
Nr=   r   s    r   r   !BaseMixture._estimate_log_weights  rB   r   c                     g)zEstimate the log-probabilities log P(X | Z).

Compute the log-probabilities per each component for each sample.

Parameters
----------
X : array-like of shape (n_samples, n_features)

Returns
-------
log_prob : array, shape (n_samples, n_component)
Nr=   r>   s     r   r   BaseMixture._estimate_log_prob  s     	r   c                     U R                  U5      n[        USS9n[        R                  " SS9   X#SS2[        R                  4   -
  nSSS5        X44$ ! , (       d  f       UW4$ = f)a  Estimate log probabilities and responsibilities for each sample.

Compute the log probabilities, weighted log probabilities per
component and responsibilities for each sample in X with respect to
the current state of the model.

Parameters
----------
X : array-like of shape (n_samples, n_features)

Returns
-------
log_prob_norm : array, shape (n_samples,)
    log p(X)

log_responsibilities : array, shape (n_samples, n_components)
    logarithm of the responsibilities
r#   rI   ignore)underN)r   r   r   errstaterT   )r9   r?   weighted_log_probr   r   s        r   r   #BaseMixture._estimate_log_prob_resp  si    & !<<Q?!"3!<[[x((BJJ+GGH ) && )( h&&s   A
A&c                     U R                   S:X  a  [        SU-  5        gU R                   S:  a/  [        SU-  5        [        5       U l        U R                  U l        gg)(Print verbose message on initialization.r#   zInitialization %dr	   N)r,   printr   _init_prev_time_iter_prev_time)r9   r2   s     r   rn   'BaseMixture._print_verbose_msg_init_beg  sS    <<1%./\\Q%./#'6D #'#7#7D  r   c                     XR                   -  S:X  a^  U R                  S:X  a  [        SU-  5        gU R                  S:  a.  [        5       n[        SXU R                  -
  U4-  5        X0l        ggg)r   r   r#   z  Iteration %dr	   z0  Iteration %d	 time lapse %.5fs	 ll change %.5fN)r5   r,   r   r   r   )r9   r   diff_llcur_times       r   ru   'BaseMixture._print_verbose_msg_iter_end$  sv    )))Q.||q &/0"6H$*>*>>HI (0$ # /r   c           	          U(       a  SOSnU R                   S:X  a  [        SU S35        g
U R                   S:  a/  [        5       U R                  -
  n[        SU SUS S	US S35        g
g
)z.Print verbose message on the end of iteration.r   zdid not converger#   zInitialization .r	   z. time lapse z.5fzs	 lower bound N)r,   r   r   r   )r9   lbinit_has_convergedconverged_msgts        r   rw   'BaseMixture._print_verbose_msg_init_end1  sw    '9?Q<<1OM?!45\\Q---A!-aW Es81 r   )r   r   rh   r3   ro   r|   r1   r.   r2   r{   r*   r0   r/   r,   r5   r4   r8   )r#   )%__name__
__module____qualname____firstlineno____doc__r   r   r   r   r6   dict__annotations__r:   r   r@   r\   rV   rN   r   ra   rq   rr   rp   rz   r   r   r   r   r   r   r   r   r   rn   ru   rw   __static_attributes__r=   r   r   r!   r!   *   s    "(AtFCDsD89tS$v>?h4?@Haf=>LM
 (( k;%h4GH$D 10  +"Z 	 	< 5l' 6l'\0&      F&,&B$ &<|I    '480
r   r!   )	metaclass) r   rx   abcr   r   numbersr   r   r   numpyr   scipy.specialr    r
   baser   r   r   r   
exceptionsr   utilsr   utils._param_validationr   r   utils.validationr   r   r   r!   r=   r   r   <module>r      sK    $
  ' "   #  < < % + & : =
&Q, Qr   