Ë
    ÷Q(hÕI  ã                   óÖ   — d Z ddlZddlmZmZ ddlmZmZ ddlmZ ddl	Z
ddlmZ ddlmZ dd	lmZmZmZ dd
lmZ ddlmZ ddlmZ ddlmZmZ ddlmZmZ d„ Z G d„ deee¬«      Zy)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                 óˆ   — t        j                  | «      } | j                  |k7  rt        d|›d|›d| j                  ›�«      ‚y)z‘Validate 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      úS/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/sklearn/mixture/_base.pyÚ_check_shaper      s?   € ô �H‰H�U‹O€EØ‡{�{�kÒ!Ýâ’[ %§+¢+ð/ó
ð 	
ð "ó    c                   ó¼  — e Zd ZU dZ eeddd¬«      g eeddd¬«      g eeddd¬«      g eeddd¬«      g eeddd¬«      g eh d£«      gd	gd
gdg eeddd¬«      gdœ
Ze	e
d<   d„ Zed„ «       Zd„ Zed„ «       Zd&d„Z ed¬«      d&d„«       Zd„ Zed„ «       Zed„ «       Zed„ «       Zd„ Zd&d„Zd„ Zd„ Zd'd„Zd„ Zed „ «       Zed!„ «       Zd"„ Zd#„ Z d$„ Z!d%„ Z"y)(ÚBaseMixturez¥Base 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                 ó�   — || _         || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _	        y ©Nr,   )Úselfr-   r.   r/   r0   r1   r2   r)   r3   r+   r4   s              r   Ú__init__zBaseMixture.__init__@   sN   € ð )ˆÔØˆŒØ"ˆŒØ ˆŒØˆŒØ&ˆÔØ(ˆÔØ$ˆŒØˆŒØ 0ˆÕr   c                  ó   — y)z—Check initial parameters of the derived class.

        Parameters
        ----------
        X : array-like of shape  (n_samples, n_features)
        N© ©r8   ÚXs     r   Ú_check_parameterszBaseMixture._check_parametersX   ó   € ð 	r   c                 óÈ  — |j                   \  }}| j                  dk(  rxt        j                  || j                  f«      }t        j                  | j                  d|¬«      j                  |«      j                  }d|t        j                  |«      |f<   �n:| j                  dk(  rI|j                  || j                  f¬«      }||j                  d¬«      dd…t        j                  f   z  }nâ| j                  dk(  rdt        j                  || j                  f«      }|j                  || j                  d	¬
«      }d||t        j                  | j                  «      f<   no| j                  dk(  r`t        j                  || j                  f«      }t        || j                  |¬«      \  }}d||t        j                  | j                  «      f<   | j                  |«       y)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%   r"   )Ú
n_clustersr1   r)   r&   ©Úsize©ÚaxisNr'   F)rC   Úreplacer(   )r)   )r   r2   r   Úzerosr-   r
   ÚKMeansÚfitÚlabels_ÚarangeÚuniformÚsumÚnewaxisÚchoicer   Ú_initialize)r8   r=   r)   Ú	n_samplesÚ_ÚrespÚlabelÚindicess           r   Ú_initialize_parametersz"BaseMixture._initialize_parametersb   s¬  € ð —w‘w‰ˆ	�1à×Ñ˜xÒ'Ü—8‘8˜Y¨×(9Ñ(9Ð:Ó;ˆDä—‘Ø#×0Ñ0¸Èô÷ ‘�Q“ß‘ð ð 12ˆD”—‘˜9Ó% uÐ,Ó-Ø×Ñ Ò)Ø×'Ñ'¨i¸×9JÑ9JÐ-KÐ'ÓLˆDØ�D—H‘H !�HÓ$¢Q¬¯
©
 ]Ñ3Ñ3‰DØ×ÑÐ!3Ò3Ü—8‘8˜Y¨×(9Ñ(9Ð:Ó;ˆDØ"×)Ñ)Ø × 1Ñ 1¸5ð *ó ˆGð ;<ˆD�œ"Ÿ)™) D×$5Ñ$5Ó6Ð6Ò7Ø×Ñ Ò,Ü—8‘8˜Y¨×(9Ñ(9Ð:Ó;ˆDÜ(ØØ×!Ñ!Ø)ô‰JˆAˆwð
 ;<ˆD�œ"Ÿ)™) D×$5Ñ$5Ó6Ð6Ñ7à×Ñ˜˜DÕ!r   c                  ó   — y)zÜInitialize 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;   )r8   r=   rS   s      r   rP   zBaseMixture._initialize�   s   € ð 	r   c                 ó*   — | j                  ||«       | S )aø  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©r8   r=   Úys      r   rI   zBaseMixture.fit™   s   € ð6 	×Ñ˜˜AÔØˆr   T)Úprefer_skip_nested_validationc                 ób  — t        | |t        j                  t        j                  gd¬«      }|j                  d   | j
                  k  r(t        d| j
                  › d|j                  d   › �«      ‚| j                  |«       | j                  xr t        | d«       }|r| j                  nd}t        j                   }d| _        t        | j                  «      }|j                  \  }}t        |«      D �]>  }	| j!                  |	«       |r| j#                  ||«       |rt        j                   n| j$                  }
| j&                  dk(  r| j)                  «       }d}Œjd}t        d| j&                  dz   «      D ]o  }|
}| j+                  |«      \  }}| j-                  ||«       | j/                  ||«      }
|
|z
  }| j1                  ||«       t3        |«      | j4                  k  sŒmd	} n | j7                  |
|«       |
|kD  s|t        j                   k(  s�Œ$|
}| j)                  «       }}|| _        �ŒA | j                  s)| j&                  dkD  rt9        j:                  d
t<        «       | j?                  «       | _         || _        | j+                  |«      \  }}|jC                  d¬«      S )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	   )ÚdtypeÚensure_min_samplesr   z:Expected n_samples >= n_components but got n_components = z, n_samples = Ú
converged_r"   FTzˆBest performing initialization did not converge. Try different init parameters, or increase max_iter, tol, or check for degenerate data.rD   )"r   r   Úfloat64Úfloat32r   r-   r   r>   r3   Úhasattrr1   Úinfr`   r   r)   ÚrangeÚ_print_verbose_msg_init_begrV   Úlower_bound_r0   Ú_get_parametersÚ_e_stepÚ_m_stepÚ_compute_lower_boundÚ_print_verbose_msg_iter_endÚabsr.   Ú_print_verbose_msg_init_endÚwarningsÚwarnr   Ú_set_parametersÚn_iter_Úargmax)r8   r=   r[   Údo_initr1   Úmax_lower_boundr)   rQ   rR   ÚinitÚlower_boundÚbest_paramsÚbest_n_iterÚ	convergedÚn_iterÚprev_lower_boundÚlog_prob_normÚlog_respÚchanges                      r   rY   zBaseMixture.fit_predict·   s  € ô8 ˜$ ¬"¯*©*´b·j±jÐ)AÐVWÔXˆØ�7‰7�1‰:˜×)Ñ)Ò)Üð*Ø*.×*;Ñ*;Ð)<ð =Ø Ÿw™w q™z˜lð,óð ð
 	×Ñ˜qÔ!ð —‘ÒF¬7°4¸Ó+FÐGˆÙ '�—’¨QˆäŸ6™6˜'ˆØˆŒä)¨$×*;Ñ*;Ó<ˆà—w‘w‰ˆ	�1Ü˜&“Mó !	0ˆDØ×,Ñ,¨TÔ2áØ×+Ñ+¨A¨|Ô<á%,œ2Ÿ6™6™'°$×2CÑ2CˆKà�}‰} Ò!Ø"×2Ñ2Ó4�Ø‘à!�	Ü# A t§}¡}°qÑ'8Ó9ò �FØ'2Ð$à.2¯l©l¸1«oÑ+�M 8Ø—L‘L  HÔ-Ø"&×";Ñ";¸HÀmÓ"T�Kà(Ð+;Ñ;�FØ×4Ñ4°V¸VÔDä˜6“{ T§X¡XÓ-Ø$(˜	Ùðð ×0Ñ0°¸iÔHà Ò0°OÌÏÉÀwÔ4NØ&1�OØ"&×"6Ñ"6Ó"8�KØ"(�KØ&/�D–OðC!	0ðL �Š 4§=¡=°1Ò#4Ü�M‰Mð9ô #ôð 	×Ñ˜[Ô)Ø"ˆŒØ+ˆÔð
 —l‘l 1“o‰ˆˆ8à�‰ AˆÓ&Ð&r   c                 óX   — | j                  |«      \  }}t        j                  |«      |fS )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)r8   r=   r}   r~   s       r   ri   zBaseMixture._e_step!  s-   € ð  #'×">Ñ">¸qÓ"AÑˆ�xÜ�w‰w�}Ó% xÐ/Ð/r   c                  ó   — 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.
        Nr;   )r8   r=   r~   s      r   rj   zBaseMixture._m_step4  s   € ð 	r   c                  ó   — y r7   r;   ©r8   s    r   rh   zBaseMixture._get_parametersB  ó   € àr   c                  ó   — y r7   r;   )r8   Úparamss     r   rq   zBaseMixture._set_parametersF  r†   r   c                 ól   — t        | «       t        | |d¬«      }t        | j                  |«      d¬«      S )a›  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"   rD   )r   r   r   Ú_estimate_weighted_log_probr<   s     r   Úscore_sampleszBaseMixture.score_samplesJ  s2   € ô 	˜ÔÜ˜$ ¨Ô/ˆä˜×9Ñ9¸!Ó<À1ÔEÐEr   c                 ó@   — | j                  |«      j                  «       S )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‚   rZ   s      r   ÚscorezBaseMixture.score]  s   € ð" ×!Ñ! !Ó$×)Ñ)Ó+Ð+r   c                 óv   — t        | «       t        | |d¬«      }| j                  |«      j                  d¬«      S )a„  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"   rD   )r   r   rŒ   rs   r<   s     r   ÚpredictzBaseMixture.predictp  s9   € ô 	˜ÔÜ˜$ ¨Ô/ˆØ×/Ñ/°Ó2×9Ñ9¸qÐ9ÓAÐAr   c                 ó†   — t        | «       t        | |d¬«      }| j                  |«      \  }}t        j                  |«      S )a¦  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)r8   r=   rR   r~   s       r   Úpredict_probazBaseMixture.predict_proba‚  s=   € ô 	˜ÔÜ˜$ ¨Ô/ˆØ×2Ñ2°1Ó5‰ˆˆ8Ü�v‰v�hÓÐr   c                 ój  — t        | «       |dk  rt        d| j                  z  «      ‚| j                  j                  \  }}t        | j                  «      }|j                  || j                  «      }| j                  dk(  ret        j                  t        | j                  | j                  |«      D ���cg c]"  \  }}}|j                  ||t        |«      «      ‘Œ$ c}}}«      }	nå| j                  dk(  rat        j                  t        | j                  |«      D ��cg c]+  \  }}|j                  || j                  t        |«      «      ‘Œ- c}}«      }	nut        j                  t        | j                  | j                  |«      D ���cg c]3  \  }}}||j!                  ||f¬«      t        j"                  |«      z  z   ‘Œ5 c}}}«      }	t        j$                  t'        |«      D �
�cg c]!  \  }
}t        j(                  ||
t        ¬«      ‘Œ# c}}
«      }|	|fS c c}}}w c c}}w c c}}}w c c}}
w )ay  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ÚtiedrB   )r^   )r   r   r-   Úmeans_r   r   r)   ÚmultinomialÚweights_Úcovariance_typer   ÚvstackÚzipÚcovariances_Úmultivariate_normalÚintÚstandard_normalÚsqrtÚconcatenateÚ	enumerater–   )r8   rQ   rR   Ú
n_featuresÚrngÚn_samples_compr‚   Ú
covarianceÚsampler=   Újr[   s               r   r©   zBaseMixture.sample•  sý  € ô  	˜Ôà�qŠ=Üð$Ø'+×'8Ñ'8ñ:óð ð
 Ÿ™×)Ñ)‰ˆˆ:Ü  ×!2Ñ!2Ó3ˆØŸ™¨°D·M±MÓBˆà×Ñ 6Ò)Ü—	‘	ô 7:ØŸ™ T×%6Ñ%6¸ó7÷ð á2˜˜z¨6ð ×+Ñ+¨D°*¼cÀ&»kÕJôó‰Að ×!Ñ! VÒ+Ü—	‘	ô +.¨d¯k©k¸>Ó*J÷á&˜˜vð ×+Ñ+¨D°$×2CÑ2CÄSÈÃ[ÕQóó‰Aô —	‘	ô
 7:ØŸ™ T×%6Ñ%6¸ó7÷	ð ñ 3˜˜z¨6ð Ø×)Ñ)°¸
Ð/CÐ)ÓDÜ—g‘g˜jÓ)ñ*ó*ôó	ˆAô �N‰NÜ<EÀnÓ<U×V©y¨q°&ŒR�W‰W�V˜Q¤cÖ*ÓVó
ˆð �1ˆvˆùô=ùóùôùó Ws   Â6'HÄ0H"
Æ8H(Ç)&H/
c                 óF   — | j                  |«      | j                  «       z   S )a  Estimate 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Œ   z'BaseMixture._estimate_weighted_log_probÓ  s#   € ð ×&Ñ& qÓ)¨D×,FÑ,FÓ,HÑHÐHr   c                  ó   — y)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­   z!BaseMixture._estimate_log_weightsà  r?   r   c                  ó   — y)a9  Estimate 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¬   zBaseMixture._estimate_log_probê  s   € ð 	r   c                 óÖ   — | j                  |«      }t        |d¬«      }t        j                  d¬«      5  ||dd…t        j                  f   z
  }ddd«       ||fS # 1 sw Y   |fS xY w)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"   rD   Úignore)ÚunderN)rŒ   r   r   ÚerrstaterN   )r8   r=   Úweighted_log_probr}   r~   s        r   r�   z#BaseMixture._estimate_log_prob_respú  sr   € ð& !×<Ñ<¸QÓ?ÐÜ!Ð"3¸!Ô<ˆÜ�[‰[˜xÔ(ñ 	Hà(¨=º¼B¿J¹J¸Ñ+GÑGˆH÷	Hð ˜hÐ&Ð&÷	Hð ˜hÐ&Ð&ús   µAÁA(c                 ó¼   — | j                   dk(  rt        d|z  «       y| j                   dk\  r/t        d|z  «       t        «       | _        | j                  | _        yy)ú(Print verbose message on initialization.r"   zInitialization %dr	   N)r+   Úprintr   Ú_init_prev_timeÚ_iter_prev_time)r8   r1   s     r   rf   z'BaseMixture._print_verbose_msg_init_beg  sS   € à�<‰<˜1ÒÜÐ%¨Ñ.Õ/Ø�\‰\˜QÒÜÐ%¨Ñ.Ô/Ü#'£6ˆDÔ Ø#'×#7Ñ#7ˆDÕ ð r   c                 óä   — || j                   z  dk(  r^| j                  dk(  rt        d|z  «       y| j                  dk\  r0t        «       }t        d||| j                  z
  |fz  «       || _        yyy)r¶   r   r"   z  Iteration %dr	   z0  Iteration %d	 time lapse %.5fs	 ll change %.5fN)r4   r+   r·   r   r¹   )r8   r{   Údiff_llÚcur_times       r   rl   z'BaseMixture._print_verbose_msg_iter_end  s|   € à�D×)Ñ)Ñ)¨QÒ.Ø�|‰|˜qÒ ÜÐ&¨Ñ/Õ0Ø—‘ Ò"Ü›6�ÜØHØ˜x¨$×*>Ñ*>Ñ>ÀÐHñIôð (0�Õ$ð #ð /r   c           	      óÊ   — |rdnd}| j                   dk(  rt        d|› d�«       y
| j                   dk\  r/t        «       | j                  z
  }t        d|› d|d›d	|d›d�«       y
y
)z.Print verbose message on the end of iteration.rz   zdid not converger"   zInitialization ú.r	   z. time lapse z.5fzs	 lower bound N)r+   r·   r   r¸   )r8   ÚlbÚinit_has_convergedÚconverged_msgÚts        r   rn   z'BaseMixture._print_verbose_msg_init_end*  sw   € á'9™Ð?QˆØ�<‰<˜1ÒÜ�O M ?°!Ð4Õ5Ø�\‰\˜QÒÜ“˜×-Ñ-Ñ-ˆAÜØ! - °¸aÀ¸Wð EØ�s�8˜1ðõð r   r7   )r"   )#Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r5   ÚdictÚ__annotations__r9   r   r>   rV   rP   rI   r   rY   ri   rj   rh   rq   r�   r�   r‘   r”   r©   rŒ   r­   r¬   r�   rf   rl   rn   r;   r   r   r!   r!   *   s‘  … ññ " (¨A¨t¸FÔCÐDÙ˜˜s D°Ô8Ð9Ù˜t S¨$°vÔ>Ð?Ù˜h¨¨4¸Ô?Ð@Ù˜H a¨°fÔ=Ð>áÒLÓMð
ð (Ð(Ø �kØ�;Ù% h°°4ÀÔGÐHñ$Ð˜Dó ò1ð0 ñó ðò)"ðV ñ	ó ð	óñ< °Ô5òg'ó 6ðg'òR0ð& ñó ðð ñó ðð ñó ðòFó&,ò&Bò$ ó&<ò|Ið ñó ðð ñó ðò'ò48ò0ó
r   r!   )Ú	metaclass) rÆ   ro   Ú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   ðÙ $ó
 ß 'ß "Ý ã Ý #å ß <Ñ <Ý %Ý +Ý &ß :ß =ò
ô&J�, ¸ö Jr   