U
    oe                     @   s   d Z ddlZddlmZ ede zddlZddlZW n* ek
rb   ddlm	Z	 e	ddY nX G dd dZ
dd	d
Zdd Zdd ZdS )a  Code for doing logistic regressions (DEPRECATED).

Classes:
 - LogisticRegression    Holds information for a LogisticRegression classifier.

Functions:
 - train        Train a new classifier.
 - calculate    Calculate the probabilities of each class, given an observation.
 - classify     Classify an observation into a class.

This module has been deprecated, please consider an alternative like scikit-learn
insead.
    N)BiopythonDeprecationWarningzThe 'Bio.LogisticRegression' module is deprecated and will be removed in a future release of Biopython. Consider using scikit-learn instead.)MissingPythonDependencyErrorzYPlease install NumPy if you want to use Bio.LogisticRegression. See http://www.numpy.org/c                   @   s   e Zd ZdZdd ZdS )LogisticRegressionzHolds information necessary to do logistic regression classification.

    Attributes:
     - beta - List of the weights for each dimension.

    c                 C   s
   g | _ dS )zInitialize the class.N)beta)self r   5lib/python3.8/site-packages/Bio/LogisticRegression.py__init__3   s    zLogisticRegression.__init__N)__name__
__module____qualname____doc__r	   r   r   r   r   r   +   s   r   c                 C   s  t | t |krtdt|}|ddhkr4td|dkr@d}t | t | d d  }}|dksj|dkrrtdt||f|}| |ddddf< t|}t||}	t||}
d}d	}d
}d}d }}||k rtt	|
|}|d|  }|	t
| d|	 t
d|   }t|}|dk	r8|t| |dk	rp||k rX|d }|}
t|| |krpq||
 }}|d7 }t|| }t	||	| }t	t	|||}tj||}t|d
 dkr||9 }|
|7 }
qtdt }t|
|_|S )a^  Train a logistic regression classifier on a training set.

    Argument xs is a list of observations and ys is a list of the class
    assignments, which should be 0 or 1.  xs and ys should contain the
    same number of elements.  update_fn is an optional callback function
    that takes as parameters that iteration number and log likelihood.
    z$xs and ys should be the same length.r      zClasses should be 0's and 1'sNdz.No observations or observation of 0 dimension.i  g{Gz?      ?g       @gMbP?zDidn't converge.)len
ValueErrorsetnpZonesZ	transposeasarrayZzerosexpdotlogsumiterZfabsZidentitynumpyZlinalgZsolveRuntimeErrorr   listr   )ZxsZysZ	update_fntypecodeclassesNZndimsXZXtyr   ZMAX_ITERATIONSZCONVERGE_THRESHOLDZstepsizeiZold_betaZold_llikebetaXpZlogpZllikWZXtypZXtWXZdeltalrr   r   r   train8   s\    

$






r(   c                 C   s<   t dg| }t t | j|}|d|  }d| |gS )zCalculate the probability for each class.

    Arguments:
     - lr is a LogisticRegression object.
     - x is the observed data.

    Returns a list of the probability that it fits each class.
    r   r   )r   r   r   r   r   )r'   xr$   r%   r   r   r   	calculate   s    
r*   c                 C   s"   t | |}|d |d krdS dS )z%Classify an observation into a class.r   r   )r*   )r'   r)   Zprobsr   r   r   classify   s    
r+   )NN)r   warningsZBior   warnr   r   Znumpy.linalgImportErrorr   r   r(   r*   r+   r   r   r   r   <module>   s(   
K