I am using Machine learning in Mathematica 11 for a specific set of data. When I use


I get the output model by method of NearestNeighbors, which Mathematica automatically takes as the best method. In my understanding, in Nearest Neighbors technique, we need to specify the number of neighbors (k) which are considered for assigning weights for each point.

How do I see what is the value of k which Mathematica has taken automatically? How can I tune this k value?

I also could not find anything when I used


2 Answers 2


You can specify the number of neighbors using the suboption "NeighborsNumber" for the method "NearestNeighbors":

enter image description here

data = Table[x -> Sin[4 x] + RandomReal[.4], {x, RandomReal[{0, 6}, 30]}];

pdefault = Predict[data, Method -> "NearestNeighbors"];
p1 = Predict[data, Method -> {"NearestNeighbors", "NeighborsNumber" -> 1}];
p10= Predict[data, Method -> {"NearestNeighbors", "NeighborsNumber" -> 10}];

{pdefault[2.], p1[2.], p10[2.]}

{1.21519, 1.21519, 0.138903}



The nearest-neighbor algorithm is really the "k=1" nearest-neighbor algorithm: Assign the category of the (single) nearest labeled training point.

In the "k-nearest-neighbor" algorithm, you specify $k$.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.