ó
‡ˆ\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 m Z d
 e j e e e ƒ f d „  ƒ  YZ d S(   s)   Principal Component Analysis Base ClassesiÿÿÿÿN(   t   linalgi   (   t   BaseEstimatort   TransformerMixin(   t   check_array(   t   check_is_fitted(   t   six(   t   ABCMetat   abstractmethodt   _BasePCAc           B   sD   e  Z d  Z d „  Z d „  Z e d d „ ƒ Z d „  Z d „  Z	 RS(   sw   Base class for PCA methods.

    Warning: This class should not be used directly.
    Use derived classes instead.
    c         C   s£   |  j  } |  j } |  j rD | t j | d d … t j f ƒ } n  t j | |  j d ƒ } t j | j	 | | ƒ } | j
 d d t | ƒ d … c |  j 7<| S(   sr  Compute data covariance with the generative model.

        ``cov = components_.T * S**2 * components_ + sigma2 * eye(n_features)``
        where  S**2 contains the explained variances, and sigma2 contains the
        noise variances.

        Returns
        -------
        cov : array, shape=(n_features, n_features)
            Estimated covariance of data.
        Ng        i   (   t   components_t   explained_variance_t   whitent   npt   sqrtt   newaxist   maximumt   noise_variance_t   dott   Tt   flatt   len(   t   selfR	   t   exp_vart   exp_var_difft   cov(    (    s9   lib/python2.7/site-packages/sklearn/decomposition/base.pyt   get_covariance   s    			))c         C   se  |  j  j d } |  j d k r3 t j | ƒ |  j S|  j | k rU t j |  j ƒ  ƒ S|  j  } |  j	 } |  j
 r™ | t j | d d … t j f ƒ } n  t j | |  j d ƒ } t j | | j ƒ |  j } | j d d t | ƒ d … c d | 7<t j | j t j t j | ƒ | ƒ ƒ } | |  j d :} | j d d t | ƒ d … c d |  j 7<| S(   s8  Compute data precision matrix with the generative model.

        Equals the inverse of the covariance but computed with
        the matrix inversion lemma for efficiency.

        Returns
        -------
        precision : array, shape=(n_features, n_features)
            Estimated precision of data.
        i   i    Ng        g      ð?i   (   R	   t   shapet   n_components_R   t   eyeR   R    t   invR   R
   R   R   R   R   R   R   R   R   (   R   t
   n_featuresR	   R   R   t	   precision(    (    s9   lib/python2.7/site-packages/sklearn/decomposition/base.pyt   get_precision0   s"    			)*-c         C   s   d S(   sœ  Placeholder for fit. Subclasses should implement this method!

        Fit the model with X.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            Training data, where n_samples is the number of samples and
            n_features is the number of features.

        Returns
        -------
        self : object
            Returns the instance itself.
        N(    (   t   Xt   y(    (    s9   lib/python2.7/site-packages/sklearn/decomposition/base.pyt   fitQ   s    c         C   s‚   t  |  d d g d t ƒt | ƒ } |  j d k	 rD | |  j } n  t j | |  j j ƒ } |  j	 r~ | t j
 |  j ƒ :} n  | S(   sn  Apply dimensionality reduction to X.

        X is projected on the first principal components previously extracted
        from a training set.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            New data, where n_samples is the number of samples
            and n_features is the number of features.

        Returns
        -------
        X_new : array-like, shape (n_samples, n_components)

        Examples
        --------

        >>> import numpy as np
        >>> from sklearn.decomposition import IncrementalPCA
        >>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
        >>> ipca = IncrementalPCA(n_components=2, batch_size=3)
        >>> ipca.fit(X)
        IncrementalPCA(batch_size=3, copy=True, n_components=2, whiten=False)
        >>> ipca.transform(X) # doctest: +SKIP
        t   mean_R	   t
   all_or_anyN(   R   t   allR   R$   t   NoneR   R   R	   R   R   R   R
   (   R   R!   t   X_transformed(    (    s9   lib/python2.7/site-packages/sklearn/decomposition/base.pyt	   transformc   s    	c         C   sd   |  j  rF t j | t j |  j d d … t j f ƒ |  j ƒ |  j St j | |  j ƒ |  j Sd S(   s_  Transform data back to its original space.

        In other words, return an input X_original whose transform would be X.

        Parameters
        ----------
        X : array-like, shape (n_samples, n_components)
            New data, where n_samples is the number of samples
            and n_components is the number of components.

        Returns
        -------
        X_original array-like, shape (n_samples, n_features)

        Notes
        -----
        If whitening is enabled, inverse_transform will compute the
        exact inverse operation, which includes reversing whitening.
        N(   R   R   R   R   R
   R   R	   R$   (   R   R!   (    (    s9   lib/python2.7/site-packages/sklearn/decomposition/base.pyt   inverse_transformˆ   s    	+N(
   t   __name__t
   __module__t   __doc__R   R    R   R'   R#   R)   R*   (    (    (    s9   lib/python2.7/site-packages/sklearn/decomposition/base.pyR      s   		!	%(   R-   t   numpyR   t   scipyR    t   baseR   R   t   utilsR   t   utils.validationR   t	   externalsR   t   abcR   R   t   with_metaclassR   (    (    (    s9   lib/python2.7/site-packages/sklearn/decomposition/base.pyt   <module>   s   
