ó
cÂY]c           @   s½   d  Z  d d l Z d d l Z d d l m Z d d l m Z m	 Z	 d d l
 m Z d d l m Z d d l m Z m Z m Z d d	 l m Z d d
 l m Z d e e	 f d „  ƒ  YZ d S(   s!   
Nearest Centroid Classification
iÿÿÿÿN(   t   sparsei   (   t   BaseEstimatort   ClassifierMixin(   t   pairwise_distances(   t   LabelEncoder(   t   check_arrayt	   check_X_yt   check_is_fitted(   t   csc_median_axis_0(   t   check_classification_targetst   NearestCentroidc           B   s/   e  Z d  Z d d d „ Z d „  Z d „  Z RS(   sP  Nearest centroid classifier.

    Each class is represented by its centroid, with test samples classified to
    the class with the nearest centroid.

    Read more in the :ref:`User Guide <nearest_centroid_classifier>`.

    Parameters
    ----------
    metric : string, or callable
        The metric to use when calculating distance between instances in a
        feature array. If metric is a string or callable, it must be one of
        the options allowed by metrics.pairwise.pairwise_distances for its
        metric parameter.
        The centroids for the samples corresponding to each class is the point
        from which the sum of the distances (according to the metric) of all
        samples that belong to that particular class are minimized.
        If the "manhattan" metric is provided, this centroid is the median and
        for all other metrics, the centroid is now set to be the mean.

    shrink_threshold : float, optional (default = None)
        Threshold for shrinking centroids to remove features.

    Attributes
    ----------
    centroids_ : array-like, shape = [n_classes, n_features]
        Centroid of each class

    Examples
    --------
    >>> from sklearn.neighbors.nearest_centroid import NearestCentroid
    >>> import numpy as np
    >>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
    >>> y = np.array([1, 1, 1, 2, 2, 2])
    >>> clf = NearestCentroid()
    >>> clf.fit(X, y)
    NearestCentroid(metric='euclidean', shrink_threshold=None)
    >>> print(clf.predict([[-0.8, -1]]))
    [1]

    See also
    --------
    sklearn.neighbors.KNeighborsClassifier: nearest neighbors classifier

    Notes
    -----
    When used for text classification with tf-idf vectors, this classifier is
    also known as the Rocchio classifier.

    References
    ----------
    Tibshirani, R., Hastie, T., Narasimhan, B., & Chu, G. (2002). Diagnosis of
    multiple cancer types by shrunken centroids of gene expression. Proceedings
    of the National Academy of Sciences of the United States of America,
    99(10), 6567-6572. The National Academy of Sciences.

    t	   euclideanc         C   s   | |  _  | |  _ d  S(   N(   t   metrict   shrink_threshold(   t   selfR   R   (    (    sA   lib/python2.7/site-packages/sklearn/neighbors/nearest_centroid.pyt   __init__Q   s    	c         C   sE  |  j  d k r t d ƒ ‚ n  |  j  d k rK t | | d g ƒ \ } } n t | | d d g ƒ \ } } t j | ƒ } | r– |  j r– t d ƒ ‚ n  t | ƒ | j \ } } t ƒ  } | j	 | ƒ } | j
 |  _
 } | j }	 |	 d k  rÿ t d |	 ƒ ‚ n  t j |	 | f d	 t j ƒ|  _ t j |	 ƒ }
 xÝ t |	 ƒ D]Ï } | | k } t j | ƒ |
 | <| r}t j | ƒ d
 } n  |  j  d k rÏ| sµt j | | d d
 ƒ|  j | <qt | | ƒ |  j | <q<|  j  d k rît j d ƒ n  | | j d d
 ƒ |  j | <q<W|  j rAt j | d d
 ƒ} t j d |
 d | ƒ } | |  j | d } | j d d
 ƒ } t j | | |	 ƒ } | t j | ƒ 7} | j t | ƒ d ƒ } | | } |  j | | } t j | ƒ } t j | ƒ |  j } t j | d
 d d | ƒ| | 9} | | } | t j  d d … f | |  _ n  |  S(   sÛ  
        Fit the NearestCentroid model according to the given training data.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape = [n_samples, n_features]
            Training vector, where n_samples is the number of samples and
            n_features is the number of features.
            Note that centroid shrinking cannot be used with sparse matrices.
        y : array, shape = [n_samples]
            Target values (integers)
        t   precomputeds   Precomputed is not supported.t	   manhattant   csct   csrs2   threshold shrinking not supported for sparse inputi   s>   The number of classes has to be greater than one; got %d classt   dtypei    t   axisR   sj   Averaging for metrics other than euclidean and manhattan not supported. The average is set to be the mean.g      ð?i   t   outN(!   R   t
   ValueErrorR   t   spt   issparseR   R	   t   shapeR   t   fit_transformt   classes_t   sizet   npt   emptyt   float64t
   centroids_t   zerost   ranget   sumt   wheret   medianR   t   warningst   warnt   meant   sqrtt   reshapet   lent   signt   abst   clipt   Nonet   newaxis(   R   t   Xt   yt   is_X_sparset	   n_samplest
   n_featurest   let   y_indt   classest	   n_classest   nkt	   cur_classt   center_maskt   dataset_centroid_t   mt   variancet   st   mmt   mst	   deviationt   signst   msd(    (    sA   lib/python2.7/site-packages/sklearn/neighbors/nearest_centroid.pyt   fitU   s`    
		!#!	


#c         C   sK   t  |  d ƒ t | d d ƒ} |  j t | |  j d |  j ƒj d d ƒ S(   sò  Perform classification on an array of test vectors X.

        The predicted class C for each sample in X is returned.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]

        Returns
        -------
        C : array, shape = [n_samples]

        Notes
        -----
        If the metric constructor parameter is "precomputed", X is assumed to
        be the distance matrix between the data to be predicted and
        ``self.centroids_``.
        R!   t   accept_sparseR   R   R   i   (   R   R   R   R   R!   R   t   argmin(   R   R2   (    (    sA   lib/python2.7/site-packages/sklearn/neighbors/nearest_centroid.pyt   predict«   s    	N(   t   __name__t
   __module__t   __doc__R0   R   RG   RJ   (    (    (    sA   lib/python2.7/site-packages/sklearn/neighbors/nearest_centroid.pyR
      s   9	V(   RM   R'   t   numpyR   t   scipyR    R   t   baseR   R   t   metrics.pairwiseR   t   preprocessingR   t   utils.validationR   R   R   t   utils.sparsefuncsR   t   utils.multiclassR	   R
   (    (    (    sA   lib/python2.7/site-packages/sklearn/neighbors/nearest_centroid.pyt   <module>   s   