
\c           @   s  d  Z  d d l Z d d l m Z d d l m Z m Z d d l m	 Z	 d d l
 m Z d d	 l m Z d d
 l m Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d e f d     YZ d S(   s   Gaussian Mixture Model.iN(   t   linalgi   (   t   BaseMixturet   _check_shapei   (   t   zip(   t   check_array(   t   check_is_fitted(   t	   row_normsc         C   s   t  |  d t j t j g d t }  t |  | f d  t t j |  d   sg t t j |  d   r t	 d t j
 |   t j |   f   n  t j t j d t j |    d  s t	 d t j |     n  |  S(   s'  Check the user provided 'weights'.

    Parameters
    ----------
    weights : array-like, shape (n_components,)
        The proportions of components of each mixture.

    n_components : int
        Number of components.

    Returns
    -------
    weights : array, shape (n_components,)
    t   dtypet	   ensure_2dt   weightsg        g      ?s]   The parameter 'weights' should be in the range [0, 1], but got max value %.5f, min value %.5fsI   The parameter 'weights' should be normalized, but got sum(weights) = %.5f(   R   t   npt   float64t   float32t   FalseR   t   anyt   lesst   greatert
   ValueErrort   mint   maxt   allcloset   abst   sum(   R	   t   n_components(    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _check_weights   s    	%(c         C   s>   t  |  d t j t j g d t }  t |  | | f d  |  S(   sY  Validate the provided 'means'.

    Parameters
    ----------
    means : array-like, shape (n_components, n_features)
        The centers of the current components.

    n_components : int
        Number of components.

    n_features : int
        Number of features.

    Returns
    -------
    means : array, (n_components, n_features)
    R   R   t   means(   R   R
   R   R   R   R   (   R   R   t
   n_features(    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _check_means6   s    $c         C   s2   t  j t  j |  d   r. t d |   n  d S(   s.   Check a precision vector is positive-definite.g        s!   '%s precision' should be positiveN(   R
   R   t
   less_equalR   (   t	   precisiont   covariance_type(    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _check_precision_positivityM   s    c         C   sJ   t  j |  |  j  o0 t  j t j |   d k  sF t d |   n  d S(   s<   Check a precision matrix is symmetric and positive-definite.g        s5   '%s precision' should be symmetric, positive-definiteN(   R
   R   t   Tt   allR    t   eigvalshR   (   R   R   (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _check_precision_matrixT   s    c         C   s"   x |  D] } t  | |  q Wd S(   sA   Check the precision matrices are symmetric and positive-definite.N(   R#   (   t
   precisionsR   t   prec(    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _check_precisions_full\   s    c      	   C   s   t  |  d t j t j g d t d | d k }  i | | | f d 6| | f d 6| | f d 6| f d 6} t |  | | d |  i t d 6t d 6t d 6t d 6} | | |  |  |  S(	   s  Validate user provided precisions.

    Parameters
    ----------
    precisions : array-like
        '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 : string

    n_components : int
        Number of components.

    n_features : int
        Number of features.

    Returns
    -------
    precisions : array
    R   R   t   allow_ndt   fullt   tiedt   diagt	   sphericals   %s precision(	   R   R
   R   R   R   R   R&   R#   R   (   R$   R   R   R   t   precisions_shapet   _check_precisions(    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyR-   b   s    

c   
      C   s   | j  \ } } t j | | | f  } x{ t |  D]m } | | | }	 t j |  d d  | f |	 j |	  | | | | <| | j d d | d  c | 7<q4 W| S(   s  Estimate the full covariance matrices.

    Parameters
    ----------
    resp : array-like, shape (n_samples, n_components)

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

    nk : array-like, shape (n_components,)

    means : array-like, shape (n_components, n_features)

    reg_covar : float

    Returns
    -------
    covariances : array, shape (n_components, n_features, n_features)
        The covariance matrix of the current components.
    Ni   (   t   shapeR
   t   emptyt   ranget   dotR    t   flat(
   t   respt   Xt   nkR   t	   reg_covarR   R   t   covariancest   kt   diff(    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt#   _estimate_gaussian_covariances_full   s    5(c         C   sr   t  j | j |  } t  j | | j |  } | | } | | j   :} | j d d t |  d  c | 7<| S(   s  Estimate the tied covariance matrix.

    Parameters
    ----------
    resp : array-like, shape (n_samples, n_components)

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

    nk : array-like, shape (n_components,)

    means : array-like, shape (n_components, n_features)

    reg_covar : float

    Returns
    -------
    covariance : array, shape (n_features, n_features)
        The tied covariance matrix of the components.
    Ni   (   R
   R1   R    R   R2   t   len(   R3   R4   R5   R   R6   t   avg_X2t
   avg_means2t
   covariance(    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt#   _estimate_gaussian_covariances_tied   s    
&c         C   s~   t  j |  j | |  | d d  t  j f } | d } | t  j |  j |  | d d  t  j f } | d | | | S(   s  Estimate the diagonal covariance vectors.

    Parameters
    ----------
    responsibilities : array-like, shape (n_samples, n_components)

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

    nk : array-like, shape (n_components,)

    means : array-like, shape (n_components, n_features)

    reg_covar : float

    Returns
    -------
    covariances : array, shape (n_components, n_features)
        The covariance vector of the current components.
    Ni   (   R
   R1   R    t   newaxis(   R3   R4   R5   R   R6   R<   R=   t   avg_X_means(    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt#   _estimate_gaussian_covariances_diag   s    0
0c         C   s   t  |  | | | |  j d  S(   s  Estimate the spherical variance values.

    Parameters
    ----------
    responsibilities : array-like, shape (n_samples, n_components)

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

    nk : array-like, shape (n_components,)

    means : array-like, shape (n_components, n_features)

    reg_covar : float

    Returns
    -------
    variances : array, shape (n_components,)
        The variance values of each components.
    i   (   RB   t   mean(   R3   R4   R5   R   R6   (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt(   _estimate_gaussian_covariances_spherical   s    c         C   s   | j  d d  d t j | j  j } t j | j |   | d d  t j f } i t d 6t	 d 6t
 d 6t d 6| | |  | | |  } | | | f S(	   sP  Estimate the Gaussian distribution parameters.

    Parameters
    ----------
    X : array-like, shape (n_samples, n_features)
        The input data array.

    resp : array-like, 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, shape (n_components,)
        The numbers of data samples in the current components.

    means : array-like, 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.
    t   axisi    i
   NR(   R)   R*   R+   (   R   R
   t   finfoR   t   epsR1   R    R@   R:   R?   RB   RD   (   R4   R3   R6   R   R5   R   R7   (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _estimate_gaussian_parameters   s    ),
c   
      C   s  d } | d k r |  j  \ } } } t j | | | f  } x<t |   D]p \ } } y t j | d t }	 Wn  t j k
 r t |   n Xt j	 |	 t j
 |  d t j | | <qI Wn | d k r>|  j  \ } } y t j |  d t }	 Wn  t j k
 rt |   n Xt j	 |	 t j
 |  d t j } n= t j t j |  d   rht |   n  d t j |   } | S(   s	  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.
    s   Fitting 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, or increase reg_covar.R(   t   lowerR)   g        g      ?(   R.   R
   R/   t	   enumerateR    t   choleskyt   Truet   LinAlgErrorR   t   solve_triangulart   eyeR    R   R   t   sqrt(
   R7   R   t    estimate_precision_error_messageR   R   t   _t   precisions_cholR8   R>   t   cov_chol(    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _compute_precision_cholesky!  s0    	c         C   s   | d k re |  j  \ } } } t j t j |  j | d  d d  d d | d  f  d  } np | d k r t j t j t j |     } n@ | d k r t j t j |   d d } n | t j |   } | S(   s  Compute 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, shape (n_components,)
        The determinant of the precision matrix for each component.
    R(   iNi   R)   R*   RE   (   R.   R
   R   t   logt   reshapeR*   (   t   matrix_cholR   R   R   RR   t   log_det_chol(    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _compute_log_det_choleskyU  s    5$!c         C   sT  |  j  \ } } | j  \ } } t | | |  } | d k r t j | | f  }	 xt t | |   D]b \ }
 \ } } t j |  |  t j | |  } t j t j |  d d |	 d d  |
 f <qg Wn`| d k rat j | | f  }	 x<t |  D]\ \ }
 } t j |  |  t j | |  } t j t j |  d d |	 d d  |
 f <q Wn | d k r| d } t j | d | d  d t j |  | | j	  t j |  d | j	  }	 nj | d	 k r0| d } t j | d d  | d t j |  | j	 |  t j
 t |  d
 t |  }	 n  d | t j d t j  |	 | S(   sw  Estimate the log Gaussian probability.

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

    means : array-like, shape (n_components, n_features)

    precisions_chol : array-like
        Cholesky decompositions of the precision 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'}

    Returns
    -------
    log_prob : array, shape (n_samples, n_components)
    R(   RE   i   NR)   R*   i   g       @R+   t   squaredg      (   R.   RZ   R
   R/   RJ   R   R1   R   t   squareR    t   outerR   RL   RV   t   pi(   R4   R   RS   R   t	   n_samplesR   R   RR   t   log_dett   log_probR8   t   mut	   prec_cholt   yR$   (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _estimate_log_gaussian_prob}  s.    ("5"5
2
2"t   GaussianMixturec           B   s   e  Z d  Z d d d d d d d d d d d e d d d	  Z d
   Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s-  Gaussian 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 <gmm>`.

    .. versionadded:: 0.18

    Parameters
    ----------
    n_components : int, defaults to 1.
        The number of mixture components.

    covariance_type : {'full' (default), 'tied', 'diag', 'spherical'}
        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

    tol : float, defaults to 1e-3.
        The convergence threshold. EM iterations will stop when the
        lower bound average gain is below this threshold.

    reg_covar : float, defaults to 1e-6.
        Non-negative regularization added to the diagonal of covariance.
        Allows to assure that the covariance matrices are all positive.

    max_iter : int, defaults to 100.
        The number of EM iterations to perform.

    n_init : int, defaults to 1.
        The number of initializations to perform. The best results are kept.

    init_params : {'kmeans', 'random'}, defaults to 'kmeans'.
        The method used to initialize the weights, the means and the
        precisions.
        Must be one of::

            'kmeans' : responsibilities are initialized using kmeans.
            'random' : responsibilities are initialized randomly.

    weights_init : array-like, shape (n_components, ), optional
        The user-provided initial weights, defaults to None.
        If it None, weights are initialized using the `init_params` method.

    means_init : array-like, shape (n_components, n_features), optional
        The user-provided initial means, defaults to None,
        If it None, means are initialized using the `init_params` method.

    precisions_init : array-like, optional.
        The user-provided initial precisions (inverse of the covariance
        matrices), defaults to None.
        If it 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, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.

    warm_start : bool, default to 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 <warm_start>`.

    verbose : int, default to 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 to 10.
        Number of iteration done before the next print.

    Attributes
    ----------
    weights_ : array-like, shape (n_components,)
        The weights of each mixture components.

    means_ : array-like, 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'

    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 was reached in fit(), 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.

    See Also
    --------
    BayesianGaussianMixture : Gaussian mixture model fit with a variational
        inference.
    i   R(   gMbP?gư>id   t   kmeansi    i
   c         C   sw   t  t |   j d | d | d | d | d | d | d | d | d	 | d
 |  
| |  _ | |  _ |	 |  _ |
 |  _ d  S(   NR   t   tolR6   t   max_itert   n_initt   init_paramst   random_statet
   warm_startt   verboset   verbose_interval(   t   superRf   t   __init__R   t   weights_initt
   means_initt   precisions_init(   t   selfR   R   Rh   R6   Ri   Rj   Rk   Rr   Rs   Rt   Rl   Rm   Rn   Ro   (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyRq   M  s    			c         C   s   | j  \ } } |  j d k r4 t d |  j   n  |  j d k	 r^ t |  j |  j  |  _ n  |  j d k	 r t |  j |  j |  |  _ n  |  j	 d k	 r t
 |  j	 |  j |  j |  |  _	 n  d S(   s7   Check the Gaussian mixture parameters are well defined.R+   R)   R*   R(   sl   Invalid value for 'covariance_type': %s 'covariance_type' should be in ['spherical', 'tied', 'diag', 'full']N(   R+   R)   R*   R(   (   R.   R   R   Rr   t   NoneR   R   Rs   R   Rt   R-   (   Ru   R4   RR   R   (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _check_parameters]  s    			c   	      C   s5  | j  \ } } t | | |  j |  j  \ } } } | | :} |  j d k rR | n |  j |  _ |  j d k rs | n |  j |  _ |  j	 d k r | |  _
 t | |  j  |  _ n |  j d k r t j g  |  j	 D] } t j | d t ^ q  |  _ n9 |  j d k r%t j |  j	 d t |  _ n |  j	 |  _ d S(   s   Initialization of the Gaussian mixture parameters.

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

        resp : array-like, shape (n_samples, n_components)
        R(   RI   R)   N(   R.   RH   R6   R   Rr   Rv   t   weights_Rs   t   means_Rt   t   covariances_RU   t   precisions_cholesky_R
   t   arrayR    RK   RL   (	   Ru   R4   R3   R_   RR   R	   R   R7   t	   prec_init(    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _initializet  s&    	!
!	1c         C   sp   | j  \ } } t | t j |  |  j |  j  \ |  _ |  _ |  _ |  j | :_ t	 |  j |  j  |  _
 d S(   s&  M step.

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

        log_resp : array-like, shape (n_samples, n_components)
            Logarithm of the posterior probabilities (or responsibilities) of
            the point of each sample in X.
        N(   R.   RH   R
   t   expR6   R   Rx   Ry   Rz   RU   R{   (   Ru   R4   t   log_respR_   RR   (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _m_step  s    c         C   s   t  | |  j |  j |  j  S(   N(   Re   Ry   R{   R   (   Ru   R4   (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _estimate_log_prob  s    c         C   s   t  j |  j  S(   N(   R
   RV   Rx   (   Ru   (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _estimate_log_weights  s    c         C   s   | S(   N(    (   Ru   RR   t   log_prob_norm(    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _compute_lower_bound  s    c         C   s   t  |  d d d g  d  S(   NRx   Ry   R{   (   R   (   Ru   (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _check_is_fitted  s    c         C   s   |  j  |  j |  j |  j f S(   N(   Rx   Ry   Rz   R{   (   Ru   (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _get_parameters  s    c         C   s   | \ |  _  |  _ |  _ |  _ |  j j \ } } |  j d k r t j |  j j  |  _ x| t	 |  j  D]( \ } } t j
 | | j  |  j | <qg Wn@ |  j d k r t j
 |  j |  j j  |  _ n |  j d |  _ d  S(   NR(   R)   i   (   Rx   Ry   Rz   R{   R.   R   R
   R/   t   precisions_RJ   R1   R    (   Ru   t   paramsRR   R   R8   Rc   (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _set_parameters  s    #c         C   s   |  j  j \ } } |  j d k r= |  j | | d d } n^ |  j d k r\ |  j | } n? |  j d k r | | d d } n |  j d k r |  j } n  | |  j } t | | |  j d  S(   s2   Return the number of free parameters in the model.R(   i   g       @R*   R)   R+   (   Ry   R.   R   R   t   int(   Ru   RR   R   t
   cov_paramst   mean_params(    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   _n_parameters  s    c         C   s:   d |  j  |  | j d |  j   t j | j d  S(   s   Bayesian information criterion for the current model on the input X.

        Parameters
        ----------
        X : array of shape (n_samples, n_dimensions)

        Returns
        -------
        bic : float
            The lower the better.
        ii    (   t   scoreR.   R   R
   RV   (   Ru   R4   (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   bic  s    c         C   s*   d |  j  |  | j d d |  j   S(   s   Akaike information criterion for the current model on the input X.

        Parameters
        ----------
        X : array of shape (n_samples, n_dimensions)

        Returns
        -------
        aic : float
            The lower the better.
        ii    i   (   R   R.   R   (   Ru   R4   (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   aic  s    N(   t   __name__t
   __module__t   __doc__Rv   R   Rq   Rw   R~   R   R   R   R   R   R   R   R   R   R   (    (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyRf     s$   				!									(   R   t   numpyR
   t   scipyR    t   baseR   R   t   externals.six.movesR   t   utilsR   t   utils.validationR   t   utils.extmathR   R   R   R   R#   R&   R-   R:   R?   RB   RD   RH   RU   RZ   Re   Rf   (    (    (    s?   lib/python2.7/site-packages/sklearn/mixture/gaussian_mixture.pyt   <module>   s,   	!					-					'	4	(	6