
    &h                          d Z ddlZ	 ddlZddlmZ  ej                  de        G d d      Z	d	 Z
dd
ZddZddZy# e$ r ddlmZ  ed      dw xY w)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                       e Zd ZdZd Zy)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                 L    t               | _        g | _        g | _        d| _        y)zInitialize the class.N)setclassesxsysk)selfs    V/mounts/lovelace/software/anaconda3/envs/py312/lib/python3.12/site-packages/Bio/kNN.py__init__zkNN.__init__?   s     u    N)__name__
__module____qualname____doc__r    r   r   r   r   5   s    r   r   c                      y)z8Return integer one (dummy method for equally weighting).   r   )xys     r   equal_weightr   G   s     r   c                     t               }t        |      |_        t        j                  | |      |_        ||_        ||_        |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knns        r   trainr    M   s;     %Cb'CKZZH%CFCFCEJr   c                    |t         }t        j                  |      }g }|rMt        t	        | j
                              D ]+  } ||| j
                  |         }|j                  ||f       - nt        j                  t	        |            }t        t	        | j
                              D ]S  }|| j
                  |   z
  |dd t        j                  t        j                  ||            }|j                  ||f       U |j                          i }| j                  D ]  }	d||	<   	 |d| j                   D ]3  \  }}| j                  |   }
||
    ||| j
                  |         z   ||
<   5 |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   klasss              r   	calculater1   ]   sV     	


1AEs366{# 	$Aq#&&),DLL$#	$
 xxA s366{# 	$A#&&)mDG77266$-.DLL$#	$ 
JJL G[[ 
355> Baq	 )Asvvay*AAB Nr   c                     |t         }t        | |||      }d}d}|j                         D ]  \  }}|||kD  s|}|} |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   r1   items)	r   r   r)   r*   r/   
most_classmost_weightr0   weights	            r   classifyr7      sa      	Q)MGJK  !v+!5J K! r   )N)NN)r   warningsnumpyr   ImportErrorBior   r   warnr   r   r    r1   r7   r   r   r   <module>r=      sz   2  , A $ *ZK  0
&Ts	   ? A