Home > Backend Development > PHP Tutorial > How to implement K nearest neighbor algorithm in PHP

How to implement K nearest neighbor algorithm in PHP

WBOY
Release: 2023-07-07 11:10:01
Original
1123 people have browsed it

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.

  1. Data preparation
    First, we need to prepare known sample data and sample data to be classified. It is known that the sample data contains categories and feature values, while the sample data to be classified only has feature values. To simplify the example, we assume that both the known sample data and the sample data to be classified are represented in the form of arrays. The following is a sample data:

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)),
Copy after login

);

Sample data to be classified:
$unknownSample = array('features' => array(2, 2));

  1. Calculate distance
    Next, we need to write a function to calculate the distance between the sample to be classified and The distance between the samples is known. Commonly used distance measurement methods include Euclidean distance, Manhattan distance, etc. The following is an example of calculating the Euclidean distance:

function euclideanDistance($sample1, $sample2) {

$sum = 0;
for ($i = 0; $i < count($sample1); $i++) {
    $sum += pow($sample1[$i] - $sample2[$i], 2);
}
return sqrt($sum);
Copy after login

}

  1. Find the K nearest Neighbors
    In this step, we need to write a function to find the K known samples closest to the sample to be classified. The following is an example function:

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);
Copy after login

}

  1. Classify
    Finally, we need to write a function to classify based on the categories of the K nearest neighbors. The following is a sample function:

function classify($neighbors) {

$classes = array();
foreach ($neighbors as $neighbor) {
    $classes[] = $neighbor['class'];
}
$classCounts = array_count_values($classes);
arsort($classCounts);
return key($classCounts);
Copy after login

}

  1. Full example
    The following is a complete example Code:
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;
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template