
:>"^Í  ã               @   sk   d  Z  d d l Z Gd d „  d e ƒ Z d d „  Z d d d „ Z d d d	 d
 „ Z d d d d „ Z d S)aµ  Code for doing k-nearest-neighbors classification.

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.

é    Nc               @   s"   e  Z d  Z d Z d d „  Z d S)ÚkNNa  Holds information necessary to do nearest neighbors classification.

    Attribues:
     - 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.N)ÚsetÚclassesÚxsÚysÚk)Úself© r	   ú,/tmp/pip-build-ww9dw3qa/biopython/Bio/kNN.pyÚ__init__,   s    		zkNN.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   r	   r	   r
   r   "   s   r   c             C   s   d S)z8Return integer one (dummy method for equally weighting).é   r	   )ÚxÚyr	   r	   r
   Úequal_weight4   s    r   c             C   sC   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   ÚnumpyÚasarrayr   r   r   )r   r   r   ÚtypecodeÚknnr	   r	   r
   Útrain:   s    			r   c             C   s|  | d k r t  } t j | ƒ } g  } | ry xÇ t t |  j ƒ ƒ D]/ } | | |  j | ƒ } | j | | f ƒ qC Wn~ t j t | ƒ ƒ } xf t t |  j ƒ ƒ D]O } | |  j | | d d … <t j t j	 | | ƒ ƒ } | j | | f ƒ q¤ W| j
 ƒ  i  } x |  j D] }	 d | |	 <qWxP | d |  j … D]; \ } } |  j | }
 | |
 | | |  j | ƒ | |
 <q9W| 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   ÚappendZzerosÚsqrtÚdotÚsortr   r   r   )r   r   Ú	weight_fnÚdistance_fnÚorderÚiÚdistÚtempÚweightsr   Úklassr	   r	   r
   Ú	calculateJ   s*    
 &r'   c       	      C   s~   | d k r t  } t |  | d | d | ƒ} d } d } x> | j ƒ  D]0 \ } } | d k sj | | k rF | } | } qF W| 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.
    Nr   r    )r   r'   Úitems)	r   r   r   r    r%   Z
most_classZmost_weightr&   Zweightr	   r	   r
   Úclassifyw   s    
r)   )r   r   Úobjectr   r   r   r'   r)   r	   r	   r	   r
   Ú<module>   s   -