U
    ]<`                     @   s@   d Z ddlZddlZG dd dZdddZdd Zd	d
 ZdS )a:  Code for doing logistic regressions.

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.
    Nc                   @   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.N)beta)self r   5lib/python3.8/site-packages/Bio/LogisticRegression.py__init__    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 }dd |
D |_|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.c                 S   s   g | ]}t |qS r   )float).0xr   r   r   
<listcomp>l   s     ztrain.<locals>.<listcomp>)len
ValueErrorsetnumpyZonesZ	transposeasarrayZzerosexpdotlogsumiterZfabsZidentityZlinalgZsolveRuntimeErrorr   r   )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   train%   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'   r   r$   r%   r   r   r   	calculatep   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   r   Znumpy.linalgr   r(   r)   r*   r   r   r   r   <module>   s   
K