Home >Backend Development >PHP Tutorial >How to implement K nearest neighbor algorithm in PHP
How to implement K nearest neighbor algorithm in PHP
K nearest neighbor algorithm is a simple and commonly used machine learning algorithm, widely used in classification and regression problems. Its basic principle is to classify the sample to be classified into the category to which the nearest K known samples belong by calculating the distance between the sample to be classified and the known samples. In this article, we will introduce how to implement the K-nearest neighbor algorithm in PHP and provide code examples.
Known sample data:
$knownSamples = array(
array('class' => 'A', 'features' => array(2, 3)), array('class' => 'A', 'features' => array(4, 5)), array('class' => 'B', 'features' => array(1, 1)), array('class' => 'B', 'features' => array(3, 2)),
);
Sample data to be classified:
$unknownSample = array('features' => array(2, 2));
function euclideanDistance($sample1, $sample2) {
$sum = 0; for ($i = 0; $i < count($sample1); $i++) { $sum += pow($sample1[$i] - $sample2[$i], 2); } return sqrt($sum);
}
function findNeighbors($knownSamples, $unknownSample, $k) {
$distances = array(); foreach ($knownSamples as $knownSample) { $distance = euclideanDistance($knownSample['features'], $unknownSample['features']); $distances[] = array('class' => $knownSample['class'], 'distance' => $distance); } usort($distances, function ($a, $b) { return $a['distance'] - $b['distance']; }); return array_slice($distances, 0, $k);
}
function classify($neighbors) {
$classes = array(); foreach ($neighbors as $neighbor) { $classes[] = $neighbor['class']; } $classCounts = array_count_values($classes); arsort($classCounts); return key($classCounts);
}
function euclideanDistance($sample1, $sample2) { $sum = 0; for ($i = 0; $i < count($sample1); $i++) { $sum += pow($sample1[$i] - $sample2[$i], 2); } return sqrt($sum); } function findNeighbors($knownSamples, $unknownSample, $k) { $distances = array(); foreach ($knownSamples as $knownSample) { $distance = euclideanDistance($knownSample['features'], $unknownSample['features']); $distances[] = array('class' => $knownSample['class'], 'distance' => $distance); } usort($distances, function ($a, $b) { return $a['distance'] - $b['distance']; }); return array_slice($distances, 0, $k); } function classify($neighbors) { $classes = array(); foreach ($neighbors as $neighbor) { $classes[] = $neighbor['class']; } $classCounts = array_count_values($classes); arsort($classCounts); return key($classCounts); } $knownSamples = array( array('class' => 'A', 'features' => array(2, 3)), array('class' => 'A', 'features' => array(4, 5)), array('class' => 'B', 'features' => array(1, 1)), array('class' => 'B', 'features' => array(3, 2)), ); $unknownSample = array('features' => array(2, 2)); $neighbors = findNeighbors($knownSamples, $unknownSample, 3); $class = classify($neighbors); echo "待分类样本的类别为:" . $class;
The above code will output the category of the sample to be classified.
Summary:
This article introduces how to use PHP to implement the K nearest neighbor algorithm. By calculating the distance between the sample to be classified and the known sample, K nearest neighbors are found, and then classified according to the categories of these nearest neighbors. The K nearest neighbor algorithm is a simple and commonly used algorithm suitable for many classification and regression problems. Implementing the K-nearest neighbor algorithm using PHP is relatively simple and only requires writing a few functions to complete. I hope this article can help readers understand and apply the K-nearest neighbor algorithm.
The above is the detailed content of How to implement K nearest neighbor algorithm in PHP. For more information, please follow other related articles on the PHP Chinese website!