2
$\begingroup$

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

p=Predict[data]

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

PredictorInformation[p]
$\endgroup$

2 Answers 2

4
$\begingroup$

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

enter image description here

SeedRandom[1]
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}

$\endgroup$
1
$\begingroup$

No.

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$.

$\endgroup$

Your Answer

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

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