represents an untrained nearest neighbors model.
NearestModel[k]
uses only the specified number of k-neighbors.
NearestModel[hpars,vars]
uses the explicit hyperparameters hpars and variable specification vars.
NearestModel
represents an untrained nearest neighbors model.
NearestModel[k]
uses only the specified number of k-neighbors.
NearestModel[hpars,vars]
uses the explicit hyperparameters hpars and variable specification vars.
Details
- NearestModel represents a k-nearest neighbors model (k-NN), which assigns a value to a point based on its nearest neighbors.
- The current model representation can be expanded using NearestModel[...][...]
- The following hyperparameters can be specified:
-
"Neighbors" Automatic number of neighbors (K) "Weights" "Uniform" weighting function of the neighbors - Possible "Neighbors" specification includes:
-
Automatic automatically find the best number of neighbors k use the k neighbors to predict a value {k,d} use up to k neighbors within distance d {All,d} use all the neighbors within distance d - Possible "Weights" specification includes:
-
"Uniform" all neighbors are given equal weight "Distance" closer neighbors are given more weight - When not specified, variables will automatically be enumerated using x[i].
- Valid variable specifications vars include:
-
n the number of variables symb a symbolic representation of a single variable {symb1,…} a list of symbolic variables - Model properties can be extracted using Information[NearestModel[…],prop].
- Valid basic properties include:
-
"BaseType" model base type "Name" model name "ShortName" short identifier to use as label "InputType" supported input types "OutputType" supported output types - Valid data-related properties include:
-
"ColumnNames" names of the input features "ColumnVariableMap" map between column names and model variables "InputSize" dimensionality of the input "OutputSize" dimensionality of the output "Trainable" whether the model is fully specified and can be trained "Trained" whether the model can be evaluated numerically "VariableColumnMap" map between model variables and column names "Variables" name of the model variables
Hyperparameters
Variables
Properties
Examples
open all close allBasic Examples (2)
Scope (14)
Hyperparameters (5)
Neighbors (4)
Specify the degree of the neighbors:
NearestModel[3]Explicitly specify the number of neighbors:
NearestModel[<|"Neighbors" -> 3|>]Use all the neighbors within the specified distance:
NearestModel[<|"Neighbors" -> {All, 10.}|>]Try all the odd numbers between 3 and 11:
NearestModel[<|"Neighbors" -> 3 ;; 11 ;; 2|>]Variables (3)
Information (4)
View general information about a model:
Information[NearestModel[]]Some information is only available when variables or parameters are fully specified:
Information[NearestModel[<|"Weights" -> "Distance", "Neighbors" -> 3|>, 1]]Information[NearestModel[<|"Weights" -> "Distance", "Neighbors" -> 3|>, 1], "Hyperparameters"]Information[NearestModel[<|"Weights" -> "Distance", "Neighbors" -> 3|>, a], {"Hyperparameters", "Variables"}]Fitting (2)
Fit a decision tree model with the default hyperparameters:
ModelFit[{...}, NearestModel[]]report = ModelFit[{...}, NearestModel[3 ;; 5], "Report"]Compare the choice of hyperparameter values via cross-validation:
report["CrossValidationChart"]Applications (1)
Classification (1)
Retrieve the Fisher Iris dataset:
data = ResourceData["Sample Tabular Data: Fisher Iris"]Classify the plant species, allowing the automatic selection of the number of neighbors:
species = ModelFit[data -> "Species", NearestModel["Neighbors" -> Automatic]]Predict the species of an unknown flower:
species[<|"SepalLength" -> Quantity[5.2, "Centimeters"], "SepalWidth" -> Quantity[3.1, "Centimeters"], "PetalLength" -> Quantity[1.2, "Centimeters"], "PetalWidth" -> Quantity[0.23, "Centimeters"]|>]Related Guides
History
Introduced in 2026 (15.0)
Text
Wolfram Research (2026), NearestModel, Wolfram Language function, https://reference.wolfram.com/language/ref/NearestModel.html.
CMS
Wolfram Language. 2026. "NearestModel." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NearestModel.html.
APA
Wolfram Language. (2026). NearestModel. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NearestModel.html
BibTeX
@misc{reference.wolfram_2026_nearestmodel, author="Wolfram Research", title="{NearestModel}", year="2026", howpublished="\url{https://reference.wolfram.com/language/ref/NearestModel.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_nearestmodel, organization={Wolfram Research}, title={NearestModel}, year={2026}, url={https://reference.wolfram.com/language/ref/NearestModel.html}, note=[Accessed: 12-June-2026]}