knn
knn(
@numneighbors 3
@weight 1
@mode 0
) -> llll
Generates a K-Nearest Neighbor Search object for either regression or classification tasks. It uses an internal KDTree to find an input point's numneighbors nearest neighbors in an input dataset. The output returned is a weighted average of those neighbors' values from the output dataset.
Arguments
@numneighbors[int]: Number of neighbors to consider. (default:3).@weight[int]: Weight neighbors by distance when predicting new points. (default:1).0: Off1: On
@mode[int]: KNN prediction mode, determining the type of set required during the fit process, as well as the type of set returned during prediction. (default:0).0: Regression—meant to be fitted with adatasetas an output, via thefitfunction.1: Classification—meant to be fitted with alabelsetas an output, via thefitfunction.
Output
KNN object [llll]
Usage
$indata = null;
$outdata = null;
## generate basic dataset based on "less-than" function
for $i in 1...100 do (
$a = rand(0, 1);
$b = rand(0, 1);
$indata _= [$a $b]; ## input point
$outdata _= [$a < $b] ## expected output (0 or 1)
);
$indataset = dataset($indata); ## input points
$outdataset = dataset($outdata); ## expected outputs
$model = knn(); ## create knn model
## fit (i.e., train) model to learn input/output mappping from dataset
fit($model, $indataset, $outdataset);
writeobject($model, "./knn.json"); ## write as JSON for future use (optional)
$model = readobject("./knn.json"); ## read pre-trained model from JSON (optional)
$xpoint = 0.25 0.75; ## sample point
$pred = predict($model, $xpoint); ## generate prediction
print($pred, "prediction:") ## should be (almost) 1.0, as 0.25 < 0.75 is true