o
    Rŀg                      @   s   d Z ddlZzddlZW n ey   ddlmZ eddw ddlmZ ede G dd dZ	d	d
 Z
dddZdddZdddZdS )a  Code for doing k-nearest-neighbors classification (DEPRECATED).

k Nearest Neighbors is a supervised learning algorithm that classifies
a new observation based the classes in its surrounding neighborhood.

Glossary:
 - distance   The distance between two points in the feature space.
 - weight     The importance given to each point for classification.

Classes:
 - kNN           Holds information for a nearest neighbors classifier.


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

Weighting Functions:
 - equal_weight    Every example is given a weight of 1.

This module has been deprecated, please consider an alternative like scikit-learn
instead.
    N)MissingPythonDependencyErrorzJPlease install NumPy if you want to use Bio.kNN. See http://www.numpy.org/)BiopythonDeprecationWarningz}The 'Bio.kNN' module is deprecated and will be removed in a future release of Biopython. Consider using scikit-learn instead.c                   @   s   e Zd ZdZdd ZdS )kNNa  Holds information necessary to do nearest neighbors classification.

    Attributes:
     - classes  Set of the possible classes.
     - xs       List of the neighbors.
     - ys       List of the classes that the neighbors belong to.
     - k        Number of neighbors to look at.
    c                 C   s   t  | _g | _g | _d| _dS )zInitialize the class.N)setclassesxsysk)self r   ;/var/www/html/myenv/lib/python3.10/site-packages/Bio/kNN.py__init__?   s   
zkNN.__init__N)__name__
__module____qualname____doc__r   r   r   r   r   r   5   s    	r   c                 C   s   dS )z8Return integer one (dummy method for equally weighting).   r   )xyr   r   r   equal_weightG   s   r   c                 C   s.   t  }t||_t| ||_||_||_|S )a(  Train a k nearest neighbors classifier on a training set.

    xs is a list of observations and ys is a list of the class assignments.
    Thus, xs and ys should contain the same number of elements.  k is
    the number of neighbors that should be examined when doing the
    classification.
    )r   r   r   npasarrayr   r   r	   )r   r   r	   typecodeknnr   r   r   trainM   s   
r   c                 C   s  |du rt }t|}g }|r)tt| jD ]}||| j| }|||f qn,tt|}tt| jD ]}|| j|  |dd< tt	||}|||f q7|
  i }| jD ]}	d||	< q^|d| j D ]\}}| j| }
||
 ||| j|  ||
< ql|S )a  Calculate the probability for each class.

    Arguments:
     - x is the observed data.
     - weight_fn is an optional function that takes x and a training
       example, and returns a weight.
     - distance_fn is an optional function that takes two points and
       returns the distance between them.  If distance_fn is None (the
       default), the Euclidean distance is used.

    Returns a dictionary of the class to the weight given to the class.
    Ng        )r   r   r   rangelenr   appendzerossqrtdotsortr   r	   r   )r   r   	weight_fndistance_fnorderidisttempweightsr	   klassr   r   r   	calculate]   s,   



r*   c           	      C   sR   |du rt }t| |||d}d}d}| D ]\}}|du s"||kr&|}|}q|S )a%  Classify an observation into a class.

    If not specified, weight_fn will give all neighbors equal weight.
    distance_fn is an optional function that takes two points and returns
    the distance between them.  If distance_fn is None (the default),
    the Euclidean distance is used.
    N)r"   r#   )r   r*   items)	r   r   r"   r#   r(   
most_classmost_weightr)   weightr   r   r   classify   s   r/   )N)NN)r   warningsnumpyr   ImportErrorBior   r   warnr   r   r   r*   r/   r   r   r   r   <module>   s,   

-