Ë
    £�Dj\  ã                   ó    — d dl Z G d„ d«      Zy)é    Nc                   ót   — e Zd ZdZdZd„ Zdd„Zd„ Zd„ Zd„ Z	d	„ Z
d
„ Zdd„Zdd„Zd„ Zd„ Zdd„Zdd„Zdd„Zy)ÚPcazˆ
    A basic class for Principal Component Analysis (PCA).

    p is the number of dimensions, while N is the number of data points
    )ÚrÚgÚbÚcÚyÚmÚkc                 ó®   — | j                   }|t        j                  |d¬«      z
  }|t        j                  |d¬«      z  }|| _        || _        d | _        y )Nr   ©Úaxis)ÚAÚnpÚmeanÚstdÚMÚNÚ_eig)Úselfr   r   r   s       ú[C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels/sandbox/pca.pyÚ__calcz
Pca.__calc   sJ   € Ø�F‰FˆØ
ŒB�G‰G�A˜1ÔÑ
ˆØ
ŒB�F‰F�1˜!ÔÑ
ˆàˆŒØˆŒØˆ�	ó    Nc           	      óB  — t        j                  |«      j                  }|j                  \  }}||c| _        | _        ||kD  rddlm}  |dt        «       || _	        |j                  «       | _        | j                  «        t        j                  | j                  t        |dz
  t!        | j                  «      z  «      dz   «      d| | _        |�t!        |«      |k7  rt#        d«      ‚|€d| _        yt%        |D �cg c]  }t'        |«      ‘Œ c}«      | _        yc c}w )z$
        p X N matrix input
        r   )Úwarnzp > n - intentional?é   Nznames must match data dimension)r   ÚarrayÚTÚshapeÚnÚpÚwarningsr   ÚRuntimeWarningr   ÚcopyÚ_origAÚ
_Pca__calcÚtileÚ_colorsÚintÚlenÚ
ValueErrorÚtupleÚstrÚnames)r   Údatar.   r   r    r!   r   Úxs           r   Ú__init__zPca.__init__   sâ   € ô �H‰H�T‹N×ÑˆØ�g‰g‰ˆˆ!Ø˜!ˆˆŒˆtŒvØˆqŠ5Ý%ÙÐ'¬Ô8ØˆŒØ—F‘F“HˆŒà�‰Œä—g‘g˜dŸl™l¬3°°!±´S¸¿¹Ó5FÑ/FÓ+GÈÑ+IÓJÈ2ÈAÐNˆŒØÐ¤ U£¨q¢ÜÐ>Ó?Ð?Ø"˜]�Tˆ�
´ÀuÖ6MÀ!´s¸1µvÒ6MÓ0Nˆ�
ùÒ6Ms   Ã=Dc                 óT   — t        j                  | j                  j                  «      S )z?
        returns the covariance matrix for the dataset
        )r   Úcovr   r   ©r   s    r   ÚgetCovarianceMatrixzPca.getCovarianceMatrix+   s   € ô �v‰v�d—f‘f—h‘hÓÐr   c                 óþ   — | j                   €ft        j                  j                  | j	                  «       «      }t        j
                  |d   «      ddd…   }|d   |   |d   dd…|f   f}|| _         | j                   S )zQ
        returns a tuple of (eigenvalues,eigenvectors) for the data set.
        Nr   éÿÿÿÿr   )r   r   ÚlinalgÚeigr5   Úargsort)r   ÚresÚsortis      r   ÚgetEigensystemzPca.getEigensystem1   ss   € ð �9‰9ÐÜ—)‘)—-‘- × 8Ñ 8Ó :Ó;ˆCÜ—*‘*˜S ™VÓ$¡T r TÑ*ˆEØ�Q‘˜‘˜s 1™v¢a¨ g™Ð/ˆCØˆDŒIØ�y‰yÐr   c                 ó(   — | j                  «       d   S )Nr   ©r=   r4   s    r   ÚgetEigenvalueszPca.getEigenvalues<   ó   € Ø×"Ñ"Ó$ QÑ'Ð'r   c                 ó(   — | j                  «       d   S )Nr   r?   r4   s    r   ÚgetEigenvectorszPca.getEigenvectors?   rA   r   c                 óR   — | j                  «       }|t        j                  |«      z  S )z=
        "energies" are just normalized eigenvectors
        )r@   r   Úsum)r   Úvs     r   ÚgetEnergieszPca.getEnergiesB   s$   € ð ×
Ñ
Ó
ˆØ”—‘˜“‰{Ðr   c           
      óˆ  — ddl m} | j                  dd…|f   | j                  dd…|f   }}|r|j                  «        |j	                  ||«       | j                  «       \  }}|j                  «       \  }	}
|j                  «       \  }}|
|	z
  ||z
  }}t        ||j                  | j                  «      D ]6  \  }}}|j                  dd|||   z  |||   z  d||z  dz  dz  z  ||¬«       Œ8 | j                  �I|j                  d| j                  |   z   dz   «       |j                  d| j                  |   z   dz   «       yy)	zü
        Generates a 2-dimensional plot of the data set and principle components
        using matplotlib.

        ix specifies which p-dimension to put on the x-axis of the plot
        and iy specifies which to put on the y-axis (0-indexed)
        r   Ngš™™™™™©?é   g      à?)Ú
head_widthÚfcÚecú$z/\sigma$)Úmatplotlib.pyplotÚpyplotr   ÚclfÚscatterr=   ÚxlimÚylimÚzipr   r(   Úarrowr.   ÚxlabelÚylabel)r   ÚixÚiyrP   Úpltr0   r	   ÚvalsÚevsÚxlÚxuÚylÚyuÚdxÚdyÚvalÚvecr   s                     r   Úplot2dz
Pca.plot2dI   s5  € õ 	(Ø�F‰F’1�R�4‰L˜Ÿ™¢ " ™ˆ!ˆÙØ�G‰GŒIØ�‰�A�aÔØ×$Ñ$Ó&‰ˆˆSà�h‰h‹j‰ˆˆ2Ø�h‰h‹j‰ˆˆ2Ø�"‰u�r˜"‘uˆ2ˆÜ˜T #§%¡%¨¯©Ó5ò 	\‰IˆC��AØ�I‰I�a˜˜#˜c "™g™+ c¨#¨b©'¡k¸TÀ2ÀbÁ5ÈÁ7ÈSÁ.Ñ=PÐTUÐYZˆIÕ[ð	\ð �:‰:Ð!Ø�J‰J�s˜4Ÿ:™: b™>Ñ)¨+Ñ5Ô6Ø�J‰J�s˜4Ÿ:™: b™>Ñ)¨+Ñ5Õ6ð "r   c           	      ó:  — ddl mc m} |r|j                  «        t	        j
                  d«      }| j                  «       | j                  «       z  }|j                  |||||   ||   ||   d¬«       |j                  | j                  dd…|f   | j                  dd…|f   | j                  dd…|f   d¬«       | j                  rE|j                  | j                  |   dz   | j                  |   dz   | j                  |   dz   ¬«       y|j                  «        y)	zõ
        Generates a 3-dimensional plot of the data set and principle components
        using mayavi.

        ix, iy, and iz specify which of the input p-dimensions to place on each of
        the x,y,z axes, respectively (0-indexed).
        r   Né   é   )Úscale_factorg333333Ó?z/sigma)rV   rW   Úzlabel)Úenthought.mayavi.mlabÚmayaviÚmlabrP   r   ÚzerosrC   r@   Úquiver3dÚpoints3dr   r.   Úaxes)r   rX   rY   ÚizrP   r   Úz3rF   s           r   Úplot3dz
Pca.plot3dc   sì   € ÷ 	*Ð)ÙØ�E‰EŒGÜ�8‰8�A‹;ˆØ×ÑÓ! $×"5Ñ"5Ó"7Ñ7ˆØ	�
‰
�2�b˜˜A˜b™E ! B¡%¨¨"©¸1ˆ
Ô=Ø	�
‰
�4—6‘6š!˜B˜$‘< §¡¢q¨ t¡¨T¯V©V²A°b°D©\Àsˆ
ÔKØ�:Š:Ø�F‰F˜$Ÿ*™* R™.¨Ñ1¸¿¹ÀB¹ÈÑ9PÐX\×XbÑXbÐceÑXfÐgoÑXoˆFÕpà�F‰F�Hr   c                 óÖ  — t        j                  |«      r/|t        j                  | j                  j                  d   «      z  }|t        j
                  | j                  d¬«      z  }| j                  j                  d   }t        j                  t        j                  | j                  «      |k  d¬«      }| j                  |   | _        | j                  «        |t        |«      z
  S )a   
        clips out all data points that are more than a certain number
        of standard deviations from the mean.

        sigs can be either a single value or a length-p sequence that
        specifies the number of standard deviations along each of the
        p dimensions.
        r   r   r   )r   ÚisscalarÚonesr   r   r   ÚallÚabsr   r&   rE   )r   Úsigsr    r
   s       r   ÚsigclipzPca.sigclipw   sŸ   € ô �;‰;�tÔØ”b—g‘g˜dŸf™fŸl™l¨1™oÓ.Ñ.ˆDØ”B—F‘F˜4Ÿ6™6 qÔ)Ñ)ˆØ�F‰F�L‰L˜‰OˆÜ�F‰F”2—6‘6˜$Ÿ&™&“> DÑ(¨aÔ0ˆØ�v‰v�a‰yˆŒØ�‰ŒØ”�Q“‰xˆr   c                 ób   — | j                   j                  «       | _        | j                  «        y ©N)r%   r$   r   r&   r4   s    r   Úresetz	Pca.reset‰   s   € Ø—‘×!Ñ!Ó#ˆŒØ�‰�r   c                 ó–  — t        |||fD �cg c]  }|du‘Œ c}«      }|dk(  rt        d«      }ni|dkD  rt        d«      ‚|�| j                  «       |kD  }nC|�t        d|«      }n4|�'t	        j
                  | j                  «       «      |k  }nt        d«      ‚|€| j                  j                  }nUt	        j                  |d¬«      }| j                  j                  j                  d   |j                  d   k7  rt        d«      ‚t	        j                  | j                  «       «      j                  |z  }||   j                  S c c}w )	aD  
        projects the normalized values onto the components

        enthresh, nPCs, and cumen determine how many PCs to use

        if vals is None, the normalized data vectors are the values to project.
        Otherwise, it should be convertable to a p x N array

        returns n,p(>threshold) dimension array
        Nr   r   z&cannot specify more than one thresholdzShould be unreachableF)r$   zshape for vals does not match)rE   Úslicer+   Úenergiesr   ÚcumsumÚRuntimeErrorr   r   r   r   ÚmatrixrC   )	r   r[   ÚenthreshÚnPCsÚcumenÚeÚnonnonesr
   Úprojs	            r   ÚprojectzPca.projectŽ   s  € ô °¸$ÀÐ/FÖG¨!˜ šÒGÓHˆØ�qŠ=Ü�d“‰AØ˜Š\ÜÐEÓFÐFàÐ#Ø—M‘M“O hÑ.‘ØÐ!Ü˜$˜tÓ$‘ØÐ"Ü—I‘I˜dŸm™m›oÓ.°%Ñ7‘ä"Ð#:Ó;Ð;àˆ<Ø—6‘6—8‘8‰Dä—8‘8˜D eÔ,ˆDØ�v‰v�x‰x�~‰~˜aÑ  D§J¡J¨q¡MÒ1Ü Ð!@ÓAÐAÜ�y‰y˜×-Ñ-Ó/Ó0×2Ñ2°4Ñ7ˆØ�A‰w�y‰yÐùò- Hs   �Ec                 óÊ  — t        j                  |«      }|j                  \  }}| j                  j                  d   }||kD  rt	        d«      ‚t         j
                  j                  t        j                  | j                  «       «      j                  «      }t        j                  ||f«      }||dd…d|…f<   ||j                  z  }|r)t        j                  |j                  «      j                  S t        j                  | j                  d¬«      }	t        j                  | j                  d¬«      }
t        j                  |j                  «      |
z  |	z   j                  S )zP
        input is an n X q array, where q <= p

        output is p X n
        r   zq > pNr   r   )r   Ú
atleast_2dr   r   r+   r8   Úinvr„   rC   r   rn   r   r   r   r   )r   r   Únormedr    Úqr!   ÚevinvÚzsrŠ   ÚmnsÚsdss              r   Ú	deprojectzPca.deproject±   s  € ô �-‰-˜Ó
ˆØ�g‰g‰ˆˆ!Ø�F‰F�L‰L˜‰OˆØˆqŠ5Ü˜WÓ%Ð%ä�i‰i�m‰mœBŸI™I d×&:Ñ&:Ó&<Ó=×?Ñ?Ó@ˆä�X‰X�q˜�e‹_ˆØˆŠ1ˆRˆaˆRˆ4‰à�R—T‘T‰zˆáÜ—8‘8˜DŸF™FÓ#×%Ñ%Ð%ä—‘˜Ÿ™ AÔ&ˆCÜ—‘�t—v‘v 1Ô%ˆCÜ—H‘H˜TŸV™VÓ$ SÑ(¨Ñ,×/Ñ/Ð/r   c                 óð  — |€| j                   }n@|j                  }|j                  d   | j                   j                  d   k7  rt        d«      ‚| j	                  «       }t        j                  |«      }|dd…|f   |dd…|f<   | j                  |d«      }|j                  |z
  }|j                  t        j                  | j                  d¬«      z  }|t        j                  | j                   d¬«      z   S )z¯
        pc can be a scalar or any sequence of pc indecies

        if vals is None, the source data is self.A, else whatever is in vals
        (which must be p x m)
        Nr   z1vals do not have the correct number of componentsFr   r   )r   r   r   r+   r‹   r   Ú
zeros_liker•   r   r   r   )r   Úpcr[   ÚpcsÚzpcsÚupcr   ÚBs           r   Ú
subtractPCzPca.subtractPCË   sÅ   € ð ˆ<Ø—6‘6‰Dà—6‘6ˆDØ�z‰z˜!‰}˜tŸv™vŸ|™|¨A™Ò.Ü Ð!TÓUÐUà�L‰L‹NˆÜ�]‰]˜3ÓˆØ’q˜�t‘9ˆŠQˆrˆT‰
Ø�N‰N˜4 Ó&ˆà�F‰F�3‰JˆØ�C‰C”—‘�t—v‘v 1Ô%Ñ%ˆØ”—‘˜Ÿ™ QÔ'Ñ'Ð'r   r}   )r   r   T)r   r   é   T)NNNN)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r(   r&   r1   r5   r=   r@   rC   rG   re   rt   r{   r~   r‹   r•   r�   © r   r   r   r      sV   „ ñð
 *€GòóOò* ò	ò(ò(òó7ó4ò(ò$ó
!óF0ô4(r   r   )Únumpyr   r   r£   r   r   ú<module>r¥      s   ðã ÷[(ò [(r   