class_docstr: The NearestNeighborsModel represents rows of an SFrame in a structure that
  is used to quickly and efficiently find the nearest neighbors of a query
  point.
  
  This model cannot be constructed directly.  Instead, use
  :func:`turicreate.nearest_neighbors.create` to create an instance of this
  model. A detailed list of parameter options and code samples are available
  in the documentation for the create function.
class_docstr_tok: ["The","NearestNeighborsModel","represents","rows","of","an","SFrame","in","a","structure","that","is","used","to","quickly","and","efficiently","find","the","nearest","neighbors","of","a","query","point",".","This","model","can","not","be","constructed","directly",".","Instead",",","use",":","func",":","`","turicreate.nearest_neighbors.create","`","to","create","an","instance","of","this","model",".","A","detailed","list","of","parameter","options","and","code","samples","are","available","in","the","documentation","for","the","create","function","."]
docstr: For each row of the input 'dataset', retrieve the nearest neighbors
  from the model's stored data. In general, the query dataset does not
  need to be the same as the reference data stored in the model, but if
  it is, the 'include_self_edges' parameter can be set to False to
  exclude results that match query points to themselves.
  
  Parameters
  ----------
  dataset : SFrame
      Query data. Must contain columns with the same names and types as
      the features used to train the model. Additional columns are
      allowed, but ignored. Please see the nearest neighbors
      :func:`~turicreate.nearest_neighbors.create` documentation for more
      detail on allowable data types.
  
  label : str, optional
      Name of the query SFrame column with row labels. If 'label' is not
      specified, row numbers are used to identify query dataset rows in
      the output SFrame.
  
  k : int, optional
      Number of nearest neighbors to return from the reference set for
      each query observation. The default is 5 neighbors, but setting it
      to ``None`` will return all neighbors within ``radius`` of the
      query point.
  
  radius : float, optional
      Only neighbors whose distance to a query point is smaller than this
      value are returned. The default is ``None``, in which case the
      ``k`` nearest neighbors are returned for each query point,
      regardless of distance.
  
  verbose: bool, optional
      If True, print progress updates and model details.
  
  Returns
  -------
  out : SFrame
      An SFrame with the k-nearest neighbors of each query observation.
      The result contains four columns: the first is the label of the
      query observation, the second is the label of the nearby reference
      observation, the third is the distance between the query and
      reference observations, and the fourth is the rank of the reference
      observation among the query's k-nearest neighbors.
  
  See Also
  --------
  similarity_graph
  
  Notes
  -----
  - The `dataset` input to this method *can* have missing values (in
    contrast to the reference dataset used to create the nearest
    neighbors model). Missing numeric values are imputed to be the mean
    of the corresponding feature in the reference dataset, and missing
    strings are imputed to be empty strings.
  
  - If both ``k`` and ``radius`` are set to ``None``, each query point
    returns all of the reference set. If the reference dataset has
    :math:`n` rows and the query dataset has :math:`m` rows, the output
    is an SFrame with :math:`nm` rows.
  
  - For models created with the 'lsh' method, the query results may have
    fewer query labels than input query points. Because LSH is an
    approximate method, a query point may have fewer than 'k' neighbors.
    If LSH returns no neighbors at all for a query, the query point is
    omitted from the results.
  
  Examples
  --------
  First construct a toy SFrame and create a nearest neighbors model:
  
  >>> sf = turicreate.SFrame({'label': range(3),
  ...                       'feature1': [0.98, 0.62, 0.11],
  ...                       'feature2': [0.69, 0.58, 0.36]})
  >>> model = turicreate.nearest_neighbors.create(sf, 'label')
  
  A new SFrame contains query observations with same schema as the
  reference SFrame. This SFrame is passed to the ``query`` method.
  
  >>> queries = turicreate.SFrame({'label': range(3),
  ...                            'feature1': [0.05, 0.61, 0.99],
  ...                            'feature2': [0.06, 0.97, 0.86]})
  >>> model.query(queries, 'label', k=2)
  +-------------+-----------------+----------------+------+
  | query_label | reference_label |    distance    | rank |
  +-------------+-----------------+----------------+------+
  |      0      |        2        | 0.305941170816 |  1   |
  |      0      |        1        | 0.771556867638 |  2   |
  |      1      |        1        | 0.390128184063 |  1   |
  |      1      |        0        | 0.464004310325 |  2   |
  |      2      |        0        | 0.170293863659 |  1   |
  |      2      |        1        | 0.464004310325 |  2   |
  +-------------+-----------------+----------------+------+
docstr_tok: ["For","each","row","of","the","input","'dataset","'",",","retrieve","the","nearest","neighbors","from","the","model","'s","stored","data",".","In","general",",","the","query","dataset","does","not","need","to","be","the","same","as","the","reference","data","stored","in","the","model",",","but","if","it","is",",","the","'include_self_edges","'","parameter","can","be","set","to","False","to","exclude","results","that","match","query","points","to","themselves",".","Parameters","--","--","--","--","--","dataset",":","SFrame","Query","data",".","Must","contain","columns","with","the","same","names","and","types","as","the","features","used","to","train","the","model",".","Additional","columns","are","allowed",",","but","ignored",".","Please","see","the","nearest","neighbors",":","func",":","`","~turicreate.nearest_neighbors.create","`","documentation","for","more","detail","on","allowable","data","types",".","label",":","str",",","optional","Name","of","the","query","SFrame","column","with","row","labels",".","If","'label","'","is","not","specified",",","row","numbers","are","used","to","identify","query","dataset","rows","in","the","output","SFrame",".","k",":","int",",","optional","Number","of","nearest","neighbors","to","return","from","the","reference","set","for","each","query","observation",".","The","default","is","5","neighbors",",","but","setting","it","to","``","None","``","will","return","all","neighbors","within","``","radius","``","of","the","query","point",".","radius",":","float",",","optional","Only","neighbors","whose","distance","to","a","query","point","is","smaller","than","this","value","are","returned",".","The","default","is","``","None","``",",","in","which","case","the","``","k","``","nearest","neighbors","are","returned","for","each","query","point",",","regardless","of","distance",".","verbose",":","bool",",","optional","If","True",",","print","progress","updates","and","model","details",".","Returns","--","--","--","-","out",":","SFrame","An","SFrame","with","the","k-nearest","neighbors","of","each","query","observation",".","The","result","contains","four","columns",":","the","first","is","the","label","of","the","query","observation",",","the","second","is","the","label","of","the","nearby","reference","observation",",","the","third","is","the","distance","between","the","query","and","reference","observations",",","and","the","fourth","is","the","rank","of","the","reference","observation","among","the","query","'s","k-nearest","neighbors",".","See","Also","--","--","--","--","similarity_graph","Notes","--","--","-","-","The","`","dataset","`","input","to","this","method","*","can","*","have","missing","values","(","in","contrast","to","the","reference","dataset","used","to","create","the","nearest","neighbors","model",")",".","Missing","numeric","values","are","imputed","to","be","the","mean","of","the","corresponding","feature","in","the","reference","dataset",",","and","missing","strings","are","imputed","to","be","empty","strings",".","-","If","both","``","k","``","and","``","radius","``","are","set","to","``","None","``",",","each","query","point","returns","all","of","the","reference","set",".","If","the","reference","dataset","has",":","math",":","`","n","`","rows","and","the","query","dataset","has",":","math",":","`","m","`","rows",",","the","output","is","an","SFrame","with",":","math",":","`","nm","`","rows",".","-","For","models","created","with","the","'lsh","'","method",",","the","query","results","may","have","fewer","query","labels","than","input","query","points",".","Because","LSH","is","an","approximate","method",",","a","query","point","may","have","fewer","than","'","k","'","neighbors",".","If","LSH","returns","no","neighbors","at","all","for","a","query",",","the","query","point","is","omitted","from","the","results",".","Examples","--","--","--","--","First","construct","a","toy","SFrame","and","create","a","nearest","neighbors","model",":",">",">",">","sf","=","turicreate.SFrame","(","{","'label","'",":","range","(","3",")",",","...","'feature1","'",":","[","0.98",",","0.62",",","0.11","]",",","...","'feature2","'",":","[","0.69",",","0.58",",","0.36","]","}",")",">",">",">","model","=","turicreate.nearest_neighbors.create","(","sf",",","'label","'",")","A","new","SFrame","contains","query","observations","with","same","schema","as","the","reference","SFrame",".","This","SFrame","is","passed","to","the","``","query","``","method",".",">",">",">","queries","=","turicreate.SFrame","(","{","'label","'",":","range","(","3",")",",","...","'feature1","'",":","[","0.05",",","0.61",",","0.99","]",",","...","'feature2","'",":","[","0.06",",","0.97",",","0.86","]","}",")",">",">",">","model.query","(","queries",",","'label","'",",","k=2",")","+","--","--","--","--","--","--","-+","--","--","--","--","--","--","--","--","-+","--","--","--","--","--","--","--","--","+","--","--","--","+","|","query_label","|","reference_label","|","distance","|","rank","|","+","--","--","--","--","--","--","-+","--","--","--","--","--","--","--","--","-+","--","--","--","--","--","--","--","--","+","--","--","--","+","|","0","|","2","|","0.305941170816","|","1","|","|","0","|","1","|","0.771556867638","|","2","|","|","1","|","1","|","0.390128184063","|","1","|","|","1","|","0","|","0.464004310325","|","2","|","|","2","|","0","|","0.170293863659","|","1","|","|","2","|","1","|","0.464004310325","|","2","|","+","--","--","--","--","--","--","-+","--","--","--","--","--","--","--","--","-+","--","--","--","--","--","--","--","--","+","--","--","--","+"]
code: def query(self, dataset, label=None, k=5, radius=None, verbose=True):
      """
      For each row of the input 'dataset', retrieve the nearest neighbors
      from the model's stored data. In general, the query dataset does not
      need to be the same as the reference data stored in the model, but if
      it is, the 'include_self_edges' parameter can be set to False to
      exclude results that match query points to themselves.
  
      Parameters
      ----------
      dataset : SFrame
          Query data. Must contain columns with the same names and types as
          the features used to train the model. Additional columns are
          allowed, but ignored. Please see the nearest neighbors
          :func:`~turicreate.nearest_neighbors.create` documentation for more
          detail on allowable data types.
  
      label : str, optional
          Name of the query SFrame column with row labels. If 'label' is not
          specified, row numbers are used to identify query dataset rows in
          the output SFrame.
  
      k : int, optional
          Number of nearest neighbors to return from the reference set for
          each query observation. The default is 5 neighbors, but setting it
          to ``None`` will return all neighbors within ``radius`` of the
          query point.
  
      radius : float, optional
          Only neighbors whose distance to a query point is smaller than this
          value are returned. The default is ``None``, in which case the
          ``k`` nearest neighbors are returned for each query point,
          regardless of distance.
  
      verbose: bool, optional
          If True, print progress updates and model details.
  
      Returns
      -------
      out : SFrame
          An SFrame with the k-nearest neighbors of each query observation.
          The result contains four columns: the first is the label of the
          query observation, the second is the label of the nearby reference
          observation, the third is the distance between the query and
          reference observations, and the fourth is the rank of the reference
          observation among the query's k-nearest neighbors.
  
      See Also
      --------
      similarity_graph
  
      Notes
      -----
      - The `dataset` input to this method *can* have missing values (in
        contrast to the reference dataset used to create the nearest
        neighbors model). Missing numeric values are imputed to be the mean
        of the corresponding feature in the reference dataset, and missing
        strings are imputed to be empty strings.
  
      - If both ``k`` and ``radius`` are set to ``None``, each query point
        returns all of the reference set. If the reference dataset has
        :math:`n` rows and the query dataset has :math:`m` rows, the output
        is an SFrame with :math:`nm` rows.
  
      - For models created with the 'lsh' method, the query results may have
        fewer query labels than input query points. Because LSH is an
        approximate method, a query point may have fewer than 'k' neighbors.
        If LSH returns no neighbors at all for a query, the query point is
        omitted from the results.
  
      Examples
      --------
      First construct a toy SFrame and create a nearest neighbors model:
  
      >>> sf = turicreate.SFrame({'label': range(3),
      ...                       'feature1': [0.98, 0.62, 0.11],
      ...                       'feature2': [0.69, 0.58, 0.36]})
      >>> model = turicreate.nearest_neighbors.create(sf, 'label')
  
      A new SFrame contains query observations with same schema as the
      reference SFrame. This SFrame is passed to the ``query`` method.
  
      >>> queries = turicreate.SFrame({'label': range(3),
      ...                            'feature1': [0.05, 0.61, 0.99],
      ...                            'feature2': [0.06, 0.97, 0.86]})
      >>> model.query(queries, 'label', k=2)
      +-------------+-----------------+----------------+------+
      | query_label | reference_label |    distance    | rank |
      +-------------+-----------------+----------------+------+
      |      0      |        2        | 0.305941170816 |  1   |
      |      0      |        1        | 0.771556867638 |  2   |
      |      1      |        1        | 0.390128184063 |  1   |
      |      1      |        0        | 0.464004310325 |  2   |
      |      2      |        0        | 0.170293863659 |  1   |
      |      2      |        1        | 0.464004310325 |  2   |
      +-------------+-----------------+----------------+------+
      """
  
      ## Validate the 'dataset' input
      _tkutl._raise_error_if_not_sframe(dataset, "dataset")
      _tkutl._raise_error_if_sframe_empty(dataset, "dataset")
  
      ## Get model features
      ref_features = self.features
      sf_features = _tkutl._toolkits_select_columns(
          dataset, ref_features
      )
  
      ## Validate and preprocess the 'label' input
      if label is None:
          query_labels = _turicreate.SArray.from_sequence(len(dataset))
  
      else:
          if not label in dataset.column_names():
              raise ValueError(
                  "Input 'label' must be a string matching the name of a "
                  + "column in the reference SFrame 'dataset'."
              )
  
          if (
              not dataset[label].dtype == str
              and not dataset[label].dtype == int
          ):
              raise TypeError(
                  "The label column must contain integers or strings."
              )
  
          if label in ref_features:
              raise ValueError(
                  "The label column cannot be one of the features."
              )
  
          query_labels = dataset[label]
  
      ## Validate neighborhood parameters 'k' and 'radius'
      if k is not None:
          if not isinstance(k, int):
              raise ValueError("Input 'k' must be an integer.")
  
          if k <= 0:
              raise ValueError("Input 'k' must be larger than 0.")
  
      if radius is not None:
          if not isinstance(radius, (int, float)):
              raise ValueError(
                  "Input 'radius' must be an integer or float."
              )
  
          if radius < 0:
              raise ValueError("Input 'radius' must be non-negative.")
  
      ## Set k and radius to special values to indicate 'None'
      if k is None:
          k = -1
  
      if radius is None:
          radius = -1.0
  
      opts = {
          "model": self.__proxy__,
          "model_name": self.__name__,
          "features": sf_features,
          "query_labels": query_labels,
          "k": k,
          "radius": radius,
      }
  
      with QuietProgress(verbose):
          result = _turicreate.extensions._nearest_neighbors.query(opts)
  
      return result["neighbors"]
  
imports: from __future__ import print_function
  from __future__ import division
  from __future__ import absolute_import
  import turicreate
  from turicreate.toolkits._model import Model
  from turicreate.data_structures.sgraph import SGraph
  import turicreate.toolkits._internal_utils
  from turicreate.toolkits._private_utils import _validate_row_label
  from turicreate.toolkits._private_utils import _validate_lists
  from turicreate.toolkits._private_utils import _robust_column_name
  from turicreate.toolkits._main import ToolkitError
  from turicreate.toolkits.distances._util import _convert_distance_names_to_functions
  from turicreate.toolkits.distances._util import _validate_composite_distance
  from turicreate.toolkits.distances._util import _scrub_composite_distance_features
  from turicreate.toolkits.distances._util import _get_composite_distance_features
  from turicreate._cython.cy_server import QuietProgress
  import array
  import copy
  import six
label_desc: Use image node_id 9 for calling the NearestNeighborsModel obj's underlying member method code with example usage: obj.query(dataset, label, k, radius, verbose) and returns: result