a
    )a;9                     @   s   d dl 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ZddlmZmZmZmZ dd	lmZ d dlZed
rd dlmZ nd dlmZ edrd dlmZ n"ed
rd dlmZ nd dlmZ G dd deZdS )    N)sqrt)issparse)check_array)check_is_fitted)stable_cumsum   )	getFPTypeget_patch_messagesklearn_check_versionPatchingConditionsChain)support_usm_ndarray0.22)PCA0.23)_infer_dimension)_infer_dimension_c                   @   sd   e Zd ZejZdddZdd	 Zd
d Zdd Zdd Z	dddZ
e dd Ze dddZdS )r   NTFauto        c                 C   s.   || _ || _|| _|| _|| _|| _|| _d S )N)n_componentscopywhiten
svd_solvertoliterated_powerrandom_state)selfr   r   r   r   r   r   r    r   Alib/python3.9/site-packages/daal4py/sklearn/decomposition/_pca.py__init__0   s    
zPCA.__init__c                 C   sz   |dkr||k rvt dn\d|  kr4t||ksNn t d|t||f n(|dkrvt|tjsvt d|t|f d S )Nmlez?n_components='mle' is only supported if n_samples >= n_featuresr   zZn_components=%r must be between 0 and min(n_samples, n_features)=%r with svd_solver='full'   zSn_components=%r must be of type int when greater than or equal to 1, was of type=%r)
ValueErrormin
isinstancenumbersZIntegraltype)r   r   	n_samples
n_featuresr   r   r   _validate_n_componentsB   s    

zPCA._validate_n_componentsc                    s  |j \}}t||}|dkr"|}n|dk r0|}n|}t|}tj|dd}||}	|	j | _|	j t	
 fddt|D }
tj|ddd	|d
}|| }|j}t	|j d}| }|| }|dkrtdrt||}nt|||}n8d|  k rdk r,n nt|}t	j||ddd }||k r|j d |krZ||d   | _n*|
 }||d |  8 }|||  | _nd| _|| | _| _|d | | _|| _|d | | _|d | | _t	|d | j | _d S )Nr   r    ZcovarianceMatrix)fptypeZoutputMatrixTypec                    s   g | ]} ||f qS r   r   ).0i
covariancer   r   
<listcomp>f       z)PCA._fit_full_daal4py.<locals>.<listcomp>ZcorrelationDense
eigenvalueT)r)   methodZresultsToComputeZisDeterministicZnComponentsr   r         ?rightZsider   )shaper"   r   daal4pyr-   computemeanZravelmean_nparrayrangeZpcaZeigenvectorsZmaximumZeigenvaluessumr
   r   r   r   searchsortednoise_variance_
n_samples_n_features_components_n_components_explained_variance_explained_variance_ratio_r   singular_values_)r   Xr   r&   r'   Zn_sf_minZdaal_n_componentsfpTypeZcovariance_algoZcovariance_resZ
variances_Zpca_algZpca_resrB   rD   Ztot_varrE   ratio_cumsumZ
resid_var_r   r,   r   _fit_full_daal4pyS   sh    




zPCA._fit_full_daal4pyc           	      C   s&  |j \}}| ||| | |t|j  d }| j}| j}|dkrhtdrXt| j|}qt	| j||}n6d|  k r|dk rn nt
| j}tj||ddd }|t||k r| j|d   | _nd| _|| | _| _| jd | | _|| _| jd | | _| jd | | _| jd | | _|||fS )	Nr   r   r   r2   r3   r4   r    r   )r5   r(   rJ   r"   rB   rF   r
   r   rD   r   r   rE   r:   r>   r8   r?   r@   rA   rC   )	r   rG   r   r&   r'   UVSrI   r   r   r   	_fit_full   s8    

zPCA._fit_fullc                 C   s$  t |rtdtdr4| j|tjtjgddd}nt|tjtjgddd}| jd u r|| j	dkrlt
|j}qt
|jd }n| j}| j	| _|jd |jd  d	k }| jd
krL|dkrd| _n|jd |jd |  }}}td|| | gd|| | gd|d	 gg}|dkrFt|d d df |d d df dkrFd| _nd| _|r^| jdkrtdrz| j|| jd}nt|| jd}td}|| jdkd| j dfg}	|	r||dfg}	|	r| ||}
nt| ||}
n.| jdv r| ||| j}
ntd| j|  |
S )NzOPCA does not support sparse input. See TruncatedSVD for a possible alternative.r   TF)dtypeZ	ensure_2dr   arpackr    r   r   r   r   fullgK=g¥gYi"`>
randomized)r   zsklearn.decomposition.PCA.fit'z?' SVD solver is not supported. Only 'full' solver is supported.z`The shape of X does not satisfy oneDAL requirements: number of features / number of samples >= 2)rP   rR   zUnrecognized svd_solver='{0}')r   	TypeErrorr
   Z_validate_datar:   float64float32r   r   r   r"   r5   Z_fit_svd_solverr;   dotr   r   and_conditionsrN   PCA_originalZ_fit_truncatedr!   format	write_log)r   rG   r   Zshape_good_for_daalnpkZregression_coefs_patching_status
_dal_readyresultr   r   r   _fit   sp    



&
zPCA._fitc                 C   s(  t drt|  nt| ddgtd t dr:| j|dd t|tjtjg|d}t|}t	 }| j
d urx| j
d	|d
< |r|r| jd | jd	 |d< q| jd	|d< n,|rtjd| jjd f| jd |jd|d< |jd | jkr
td|jd  d| j dtj|d|| j|}|jS )Nr   r9   rB   )Z
all_or_anyz1.0F)reset)rO   Zforce_all_finite)r    r8   r    r0   r   r2   rO   zX has z  features, but PCA is expecting z features as input)r)   )r
   r   allZ_check_feature_namesr   r:   rU   rV   r   dictr9   Zreshaper@   rD   rQ   r5   rO   rA   r!   r6   Zpca_transformr7   rB   ZtransformedData)r   rG   r   scale_eigenvaluescheck_XrH   Ztr_dataZtr_resr   r   r   _transform_daal4py  s@    


zPCA._transform_daal4pyc                 C   sJ   t d}|| jdkdfg}|  |r>| j|| jdddS t| |S )a  
        Apply dimensionality reduction to X.

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

        Parameters
        ----------
        X : array-like of 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 of shape (n_samples, n_components)
            Projection of X in the first principal components, where `n_samples`
            is the number of samples and `n_components` is the number of the components.
        z#sklearn.decomposition.PCA.transformr   Number of components <= 0.TFr   ri   rh   )r   rX   rC   r[   rj   r   rY   	transform)r   rG   r_   r`   r   r   r   rm   &  s    
zPCA.transformc           	      C   s   |  |\}}}td}||du dfg}|rx|| jdkdfg}|r`| j|| jddd}qtj| jdf|j	d}nJ|ddd| jf }| jr|t
|jd d	 9 }n||d| j 9 }|}|  |S )
ap  
        Fit the model with X and apply the dimensionality reduction on X.

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

        y : Ignored
            Ignored.

        Returns
        -------
        X_new : ndarray of shape (n_samples, n_components)
            Transformed values.

        Notes
        -----
        This method returns a Fortran-ordered array. To convert it to a
        C-ordered array, use 'np.ascontiguousarray'.
        z'sklearn.decomposition.PCA.fit_transformNzStock fitting was used.r   rk   Frl   re   r    )rb   r   rX   rC   rj   r   r:   emptyr@   rO   r   r5   r[   )	r   rG   yrK   rM   _r_   r`   ra   r   r   r   fit_transformF  s.    

zPCA.fit_transform)NTFr   r   r   N)FTT)N)__name__
__module____qualname__rY   __doc__r   r(   rJ   rN   rb   rj   r   rm   rq   r   r   r   r   r   -   s$          
?#M
$
r   )Znumpyr:   r$   Zmathr   Zscipy.sparser   Zsklearn.utilsr   Zsklearn.utils.validationr   Zsklearn.utils.extmathr   r6   Z_utilsr   r	   r
   r   Z_device_offloadr   ZloggingZsklearn.decomposition._pcar   rY   Zsklearn.decomposition.pcar   r   r   r   r   r   <module>   s&   