o
    tBhI                     @   s   d Z ddlZddl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 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 dd ZG dd deeedZdS )zBase class for mixture models.    N)ABCMetaabstractmethod)time)	logsumexp   )cluster)kmeans_plusplus)BaseEstimator)DensityMixin)ConvergenceWarning)check_random_statecheck_scalar)check_is_fittedc                 C   s,   t | } | j|krtd||| jf dS )zValidate the shape of the input parameter 'param'.

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

    param_shape : tuple

    name : str
    z:The parameter '%s' should have the shape of %s, but got %sN)nparrayshape
ValueError)paramparam_shapename r   l/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/sklearn/mixture/_base.py_check_shape   s   


r   c                   @   s   e Zd ZdZdd Zdd Zedd Zdd	 Zed
d Z	d2ddZ
d2ddZdd Zedd Zedd Zedd Zdd Zd2ddZdd Zdd  Zd3d"d#Zd$d% Zed&d' Zed(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 ZdS )4BaseMixturezBase class for mixture models.

    This abstract class specifies an interface for all mixture classes and
    provides basic common methods for mixture models.
    c                 C   s@   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	d S N)
n_componentstol	reg_covarmax_itern_initinit_paramsrandom_state
warm_startverboseverbose_interval)selfr   r   r   r   r   r    r!   r"   r#   r$   r   r   r   __init__2   s   
zBaseMixture.__init__c                 C   sr   t | jdtjdd t | jdtjdd t | jdtjdd t | jdtjdd t | jd	tjdd | 	| d
S )zCheck values of the basic parameters.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
        r      )r   target_typemin_valr   g        r   r   r   r   N)
r   r   numbersIntegralr   Realr   r   r   _check_parametersr%   Xr   r   r   _check_initial_parametersJ   s"   z%BaseMixture._check_initial_parametersc                 C      dS )zCheck initial parameters of the derived class.

        Parameters
        ----------
        X : array-like of shape  (n_samples, n_features)
        Nr   r.   r   r   r   r-   i      zBaseMixture._check_parametersc                 C   s2  |j \}}| jdkr)t|| jf}tj| jd|d|j}d|t	||f< nh| jdkrG|j
|| jfd}||jddddtjf  }nJ| jdkrht|| jf}|j|| jd	d
}d||t	| jf< n)| jdkrt|| jf}t|| j|d\}}d||t	| jf< ntd| j | || dS )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.
        kmeansr'   )
n_clustersr   r!   randomsizeaxisNrandom_from_dataF)r7   replacez	k-means++)r!   z(Unimplemented initialization method '%s')r   r    r   zerosr   r   KMeansfitlabels_arangeuniformsumnewaxischoicer   r   _initialize)r%   r/   r!   	n_samples_resplabelindicesr   r   r   _initialize_parameterss   s>   


 


z"BaseMixture._initialize_parametersc                 C   r1   )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   )r%   r/   rH   r   r   r   rE      s   
zBaseMixture._initializeNc                 C   s   |  || | 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r%   r/   yr   r   r   r>      s   zBaseMixture.fitc                 C   s  | j |tjtjgdd}|jd | jk r"td| j d|jd  | | | jo.t	| d }|r5| j
nd}tj }d| _t| j}|j\}}t|D ]q}	| |	 |r[| || |ratj n| j}
| jdkrp|  }d}qLtd| jd D ]-}|
}| |\}}| || | ||}
|
| }| || t|| jk rd	| _ nqx| |
 |
|ks|tj kr|
}|  }|}qL| js| jdkrtd
|	d  t | | || _ || _| |\}}|j!d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'   FTzzInitialization %d did not converge. Try different init parameters, or increase max_iter, tol or check for degenerate data.r8   )"_validate_datar   float64float32r   r   r   r0   r"   hasattrr   infrQ   r   r!   range_print_verbose_msg_init_begrK   lower_bound_r   _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)r%   r/   rN   do_initr   max_lower_boundr!   rF   rG   initlower_boundbest_paramsbest_n_itern_iterprev_lower_boundlog_prob_normlog_respchanger   r   r   rL      sj   






zBaseMixture.fit_predictc                 C   s   |  |\}}t||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)r%   r/   rn   ro   r   r   r   r[   1  s   zBaseMixture._e_stepc                 C   r1   )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   )r%   r/   ro   r   r   r   r\   D  s   zBaseMixture._m_stepc                 C      d S r   r   r%   r   r   r   rZ   R     zBaseMixture._get_parametersc                 C   rs   r   r   )r%   paramsr   r   r   rc   V  ru   zBaseMixture._set_parametersc                 C   s(   t |  | j|dd}t| |d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'   r8   )r   rR   r   _estimate_weighted_log_probr.   r   r   r   score_samplesZ  s   zBaseMixture.score_samplesc                 C   s   |  | 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.
        )rz   rr   rM   r   r   r   scorem  s   zBaseMixture.scorec                 C   s(   t |  | j|dd}| |jd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.
        Frw   r'   r8   )r   rR   ry   re   r.   r   r   r   predict  s   zBaseMixture.predictc                 C   s.   t |  | j|dd}| |\}}t|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.
        Frw   )r   rR   rq   r   exp)r%   r/   rG   ro   r   r   r   predict_proba  s   
zBaseMixture.predict_probar'   c                    s   t  |dk rtdj jj\} tj|j}j	dkr9t
fddtjj|D }n*j	dkrPt
fddtj|D }nt
 fddtjj|D }t
d	d t|D }||fS )
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.fullc                    s$   g | ]\}}}  ||t|qS r   )multivariate_normalint.0rr   
covariancesample)rngr   r   
<listcomp>  s    z&BaseMixture.sample.<locals>.<listcomp>tiedc                    s$   g | ]\}}  |jt|qS r   )r   covariances_r   )r   rr   r   )r   r%   r   r   r     s    c                    s0   g | ]\}}}|j | fd t|  qS )r6   )standard_normalr   sqrtr   )
n_featuresr   r   r   r     s    c                 S   s    g | ]\}}t j||td qS ))rO   )r   r   r   )r   jr   r   r   r   r     s     )r   r   r   means_r   r   r!   multinomialweights_covariance_typer   vstackzipr   concatenate	enumerate)r%   rF   rG   n_samples_compr/   rN   r   )r   r   r%   r   r     sF   






zBaseMixture.samplec                 C   s   |  ||   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.   r   r   r   ry     s   z'BaseMixture._estimate_weighted_log_probc                 C   r1   )zEstimate log-weights in EM algorithm, E[ log pi ] in VB algorithm.

        Returns
        -------
        log_weight : array, shape (n_components, )
        Nr   rt   r   r   r   r     r2   z!BaseMixture._estimate_log_weightsc                 C   r1   )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.   r   r   r   r     s   zBaseMixture._estimate_log_probc                 C   sf   |  |}t|dd}tjdd ||ddtjf  }W d   ||fS 1 s*w   Y  ||fS )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'   r8   ignore)underN)ry   r   r   errstaterC   )r%   r/   weighted_log_probrn   ro   r   r   r   rq   
  s   

z#BaseMixture._estimate_log_prob_respc                 C   sH   | j dkrtd|  dS | j dkr"td|  t | _| j| _dS dS )(Print verbose message on initialization.r'   zInitialization %dr   N)r#   printr   _init_prev_time_iter_prev_time)r%   r   r   r   r   rX   $  s   

z'BaseMixture._print_verbose_msg_init_begc                 C   sb   || j  dkr-| jdkrtd|  dS | jdkr/t }td||| j |f  || _dS dS dS )r   r   r'   z  Iteration %dr   z0  Iteration %d	 time lapse %.5fs	 ll change %.5fN)r$   r#   r   r   r   )r%   rl   diff_llcur_timer   r   r   r^   -  s   


z'BaseMixture._print_verbose_msg_iter_endc                 C   sJ   | j dkrtd| j  dS | j dkr#td| jt | j |f  dS dS )z.Print verbose message on the end of iteration.r'   zInitialization converged: %sr   z7Initialization converged: %s	 time lapse %.5fs	 ll %.5fN)r#   r   rQ   r   r   )r%   llr   r   r   r`   :  s   

z'BaseMixture._print_verbose_msg_init_endr   )r'   )__name__
__module____qualname____doc__r&   r0   r   r-   rK   rE   r>   rL   r[   r\   rZ   rc   rz   r{   r|   r~   r   ry   r   r   rq   rX   r^   r`   r   r   r   r   r   +   s@    
	/


f




>
	
	r   )	metaclass)r   r*   ra   abcr   r   r   numpyr   scipy.specialr    r   r   baser	   r
   
exceptionsr   utilsr   r   utils.validationr   r   r   r   r   r   r   <module>   s     