ó
cĀY]c           @   sx   d  Z  d d l m Z m Z d d l m Z d   Z d   Z d d d d e	 d d	  Z
 d d d d e	 d d
  Z d S(   s!   Nearest Neighbors graph functionsi   (   t   KNeighborsMixint   RadiusNeighborsMixin(   t   NearestNeighborsc         C   sx   t  d d d g | | | g  } |  j   } xD | D]< \ } } | | | k r4 t d | | | | f   q4 q4 Wd S(   s*   Check the validity of the input parameterst   metrict   pt   metric_paramssA   Got %s for %s, while the estimator has %s for the same parameter.N(   t   zipt
   get_paramst
   ValueError(   t   XR   R   R   t   paramst
   est_paramst
   param_namet
   func_param(    (    s6   lib/python2.7/site-packages/sklearn/neighbors/graph.pyt   _check_params   s    c         C   s   | r |  j  } n d } | S(   s,   Return the query based on include_self paramN(   t   _fit_Xt   None(   R	   t   include_selft   query(    (    s6   lib/python2.7/site-packages/sklearn/neighbors/graph.pyt   _query_include_self   s    t   connectivityt	   minkowskii   c   	   
   C   s}   t  |  t  s? t | d | d | d | d | j |   }  n t |  | | |  t |  |  } |  j d | d | d |  S(   s.	  Computes the (weighted) graph of k-Neighbors for points in X

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

    Parameters
    ----------
    X : array-like or BallTree, shape = [n_samples, n_features]
        Sample data, in the form of a numpy array or a precomputed
        :class:`BallTree`.

    n_neighbors : int
        Number of neighbors for each sample.

    mode : {'connectivity', 'distance'}, optional
        Type of returned matrix: 'connectivity' will return the connectivity
        matrix with ones and zeros, and 'distance' will return the distances
        between neighbors according to the given metric.

    metric : string, default 'minkowski'
        The distance metric used to calculate the k-Neighbors for each sample
        point. The DistanceMetric class gives a list of available metrics.
        The default distance is 'euclidean' ('minkowski' metric with the p
        param equal to 2.)

    p : int, default 2
        Power parameter for the Minkowski metric. When p = 1, this is
        equivalent to using manhattan_distance (l1), and euclidean_distance
        (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.

    metric_params : dict, optional
        additional keyword arguments for the metric function.

    include_self : bool, default=False.
        Whether or not to mark each sample as the first nearest neighbor to
        itself. If `None`, then True is used for mode='connectivity' and False
        for mode='distance' as this will preserve backwards compatibility.

    n_jobs : int or None, optional (default=None)
        The number of parallel jobs to run for neighbors search.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

    Returns
    -------
    A : sparse matrix in CSR format, shape = [n_samples, n_samples]
        A[i, j] is assigned the weight of edge that connects i to j.

    Examples
    --------
    >>> X = [[0], [3], [1]]
    >>> from sklearn.neighbors import kneighbors_graph
    >>> A = kneighbors_graph(X, 2, mode='connectivity', include_self=True)
    >>> A.toarray()
    array([[1., 0., 1.],
           [0., 1., 1.],
           [1., 0., 1.]])

    See also
    --------
    radius_neighbors_graph
    R   R   R   t   n_jobsR	   t   n_neighborst   mode(   t
   isinstanceR    R   t   fitR   R   t   kneighbors_graph(	   R	   R   R   R   R   R   R   R   R   (    (    s6   lib/python2.7/site-packages/sklearn/neighbors/graph.pyR   "   s    @c   	      C   sw   t  |  t  sB t d | d | d | d | d |  j |   }  n t |  | | |  t |  |  } |  j | | |  S(   s«	  Computes the (weighted) graph of Neighbors for points in X

    Neighborhoods are restricted the points at a distance lower than
    radius.

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

    Parameters
    ----------
    X : array-like or BallTree, shape = [n_samples, n_features]
        Sample data, in the form of a numpy array or a precomputed
        :class:`BallTree`.

    radius : float
        Radius of neighborhoods.

    mode : {'connectivity', 'distance'}, optional
        Type of returned matrix: 'connectivity' will return the connectivity
        matrix with ones and zeros, and 'distance' will return the distances
        between neighbors according to the given metric.

    metric : string, default 'minkowski'
        The distance metric used to calculate the neighbors within a
        given radius for each sample point. The DistanceMetric class
        gives a list of available metrics. The default distance is
        'euclidean' ('minkowski' metric with the param equal to 2.)

    p : int, default 2
        Power parameter for the Minkowski metric. When p = 1, this is
        equivalent to using manhattan_distance (l1), and euclidean_distance
        (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.

    metric_params : dict, optional
        additional keyword arguments for the metric function.

    include_self : bool, default=False
        Whether or not to mark each sample as the first nearest neighbor to
        itself. If `None`, then True is used for mode='connectivity' and False
        for mode='distance' as this will preserve backwards compatibility.

    n_jobs : int or None, optional (default=None)
        The number of parallel jobs to run for neighbors search.
        ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.
        ``-1`` means using all processors. See :term:`Glossary <n_jobs>`
        for more details.

    Returns
    -------
    A : sparse matrix in CSR format, shape = [n_samples, n_samples]
        A[i, j] is assigned the weight of edge that connects i to j.

    Examples
    --------
    >>> X = [[0], [3], [1]]
    >>> from sklearn.neighbors import radius_neighbors_graph
    >>> A = radius_neighbors_graph(X, 1.5, mode='connectivity',
    ...                            include_self=True)
    >>> A.toarray()
    array([[1., 0., 1.],
           [0., 1., 0.],
           [1., 0., 1.]])

    See also
    --------
    kneighbors_graph
    t   radiusR   R   R   R   (   R   R   R   R   R   R   t   radius_neighbors_graph(	   R	   R   R   R   R   R   R   R   R   (    (    s6   lib/python2.7/site-packages/sklearn/neighbors/graph.pyR   l   s    EN(   t   __doc__t   baseR    R   t   unsupervisedR   R   R   R   t   FalseR   R   (    (    (    s6   lib/python2.7/site-packages/sklearn/neighbors/graph.pyt   <module>   s   		
I	