Case 2. Regression: apartment price estimation

Implementation in pure PHP

In this case, we apply the same k-nearest neighbors idea to a regression task. Instead of predicting a class label, we estimate a numeric value: the price of an apartment. We compute distances to known examples, take the k closest ones, and average their prices.

Example of use:

 
<?php

// k-Nearest Neighbors (k-NN) for regression: estimate apartment price.
function euclideanDistance(array $a, array $b): float {
    
// Euclidean distance between two vectors (points).
    
$sum 0.0;

    foreach (
$a as $i => $value) {
        
$sum += ($value $b[$i]) ** 2;
    }

    return 
sqrt($sum);
}

// Dataset:
// Each item is [features, price].
// Here features are [area_m2, distance_to_center_km] and price is a numeric value.
$dataset = [
    [[
4012], 120000],
    [[
5010], 150000],
    [[
608], 190000],
    [[
706], 240000],
    [[
805], 300000],
];

// Apartment we want to price.
$query = [657];

// How many nearest neighbors to use.
$k 3;

$distances = [];

foreach (
$dataset as [$features$price]) {
    
$distances[] = [
        
'distance' => euclideanDistance($features$query),
        
'price' => $price,
    ];
}

usort($distances, fn ($a$b) => $a['distance'] <=> $b['distance']);

$neighbors array_slice($distances0$k);

$sum 0;

foreach (
$neighbors as $neighbor) {
    
$sum += $neighbor['price'];
}

$prediction $sum $k;