WOLFRAM

Enable JavaScript to interact with content and submit forms on Wolfram websites. Learn how
Wolfram Language & System Documentation Center

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
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Hyperparameters  
Neighbors  
Weights  
Variables  
Information  
Fitting  
Applications  
Classification  
See Also
Related Guides
History
Cite this Page

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 [...][...]
  • Hyperparameters
  • 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
  • Variables
  • 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
  • Properties
  • 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

Examples

open all close all

Basic Examples  (2)

Specify a generic nearest model:

Wolfram Language code: NearestModel[]

Use different distance weighting:

Wolfram Language code: NearestModel[<|"Weights" -> "Distance"|>]

Scope  (14)

Hyperparameters  (5)

Neighbors  (4)

Specify the degree of the neighbors:

Wolfram Language code: NearestModel[3]

Explicitly specify the number of neighbors:

Wolfram Language code: NearestModel[<|"Neighbors" -> 3|>]

Use all the neighbors within the specified distance:

Wolfram Language code: NearestModel[<|"Neighbors" -> {All, 10.}|>]

Try all the odd numbers between 3 and 11:

Wolfram Language code: NearestModel[<|"Neighbors" -> 3 ;; 11 ;; 2|>]

Weights  (1)

Specify a distance weighting function:

Wolfram Language code: NearestModel[<|"Weights" -> "Distance"|>]

Variables  (3)

Specify the input variable count:

Wolfram Language code: NearestModel[3, 2]

Use explicit variable names:

Wolfram Language code: NearestModel[3, {x, y}]

Specify variable count without specifying neighbor count:

Wolfram Language code: NearestModel["Neighbors" -> Automatic, 2]

Information  (4)

View general information about a model:

Wolfram Language code: Information[NearestModel[]]

Some information is only available when variables or parameters are fully specified:

Wolfram Language code: Information[NearestModel[<|"Weights" -> "Distance", "Neighbors" -> 3|>, 1]]

Extract a single property:

Wolfram Language code: Information[NearestModel[<|"Weights" -> "Distance", "Neighbors" -> 3|>, 1], "Hyperparameters"]

Extract multiple properties:

Wolfram Language code: Information[NearestModel[<|"Weights" -> "Distance", "Neighbors" -> 3|>, a], {"Hyperparameters", "Variables"}]

Fitting  (2)

Fit a decision tree model with the default hyperparameters:

Wolfram Language code: ModelFit[{...}, NearestModel[]]

Fit multiple models at once:

Wolfram Language code: report = ModelFit[{...}, NearestModel[3 ;; 5], "Report"]

Compare the choice of hyperparameter values via cross-validation:

Wolfram Language code: report["CrossValidationChart"]

Applications  (1)

Classification  (1)

Retrieve the Fisher Iris dataset:

Wolfram Language code: data = ResourceData["Sample Tabular Data: Fisher Iris"]

Classify the plant species, allowing the automatic selection of the number of neighbors:

Wolfram Language code: species = ModelFit[data -> "Species", NearestModel["Neighbors" -> Automatic]]

Predict the species of an unknown flower:

Wolfram Language code: species[<|"SepalLength" -> Quantity[5.2, "Centimeters"], "SepalWidth" -> Quantity[3.1, "Centimeters"], "PetalLength" -> Quantity[1.2, "Centimeters"], "PetalWidth" -> Quantity[0.23, "Centimeters"]|>]

History

Introduced in 2026 (15.0)

Wolfram Research (2026), NearestModel, Wolfram Language function, https://reference.wolfram.com/language/ref/NearestModel.html.

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: 14-August-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: 14-August-2026]}

Top [フレーム]

AltStyle によって変換されたページ (->オリジナル) /