__ __ __ __ _____ _ _ _____ _ _ _ | \/ | \ \ / / | __ \ (_) | | / ____| | | | | | \ / |_ __\ V / | |__) | __ ___ ____ _| |_ ___ | (___ | |__ ___| | | | |\/| | '__|> < | ___/ '__| \ \ / / _` | __/ _ \ \___ \| '_ \ / _ \ | | | | | | |_ / . \ | | | | | |\ V / (_| | || __/ ____) | | | | __/ | | |_| |_|_(_)_/ \_\ |_| |_| |_| \_/ \__,_|\__\___| |_____/|_| |_|\___V 2.1 if you need WebShell for Seo everyday contact me on Telegram Telegram Address : @jackleetFor_More_Tools:
<?php
declare(strict_types=1);
namespace Phpml\Classification;
use Phpml\Helper\Predictable;
use Phpml\Helper\Trainable;
use Phpml\Math\Distance;
use Phpml\Math\Distance\Euclidean;
class KNearestNeighbors implements Classifier
{
use Trainable;
use Predictable;
/**
* @var int
*/
private $k;
/**
* @var Distance
*/
private $distanceMetric;
/**
* @param Distance|null $distanceMetric (if null then Euclidean distance as default)
*/
public function __construct(int $k = 3, ?Distance $distanceMetric = null)
{
if ($distanceMetric === null) {
$distanceMetric = new Euclidean();
}
$this->k = $k;
$this->samples = [];
$this->targets = [];
$this->distanceMetric = $distanceMetric;
}
/**
* @return mixed
*/
protected function predictSample(array $sample)
{
$distances = $this->kNeighborsDistances($sample);
$predictions = (array) array_combine(array_values($this->targets), array_fill(0, count($this->targets), 0));
foreach (array_keys($distances) as $index) {
++$predictions[$this->targets[$index]];
}
arsort($predictions);
reset($predictions);
return key($predictions);
}
/**
* @throws \Phpml\Exception\InvalidArgumentException
*/
private function kNeighborsDistances(array $sample): array
{
$distances = [];
foreach ($this->samples as $index => $neighbor) {
$distances[$index] = $this->distanceMetric->distance($sample, $neighbor);
}
asort($distances);
return array_slice($distances, 0, $this->k, true);
}
}
| Name | Type | Size | Permission | Actions |
|---|---|---|---|---|
| DecisionTree | Folder | 0777 |
|
|
| Ensemble | Folder | 0777 |
|
|
| Linear | Folder | 0777 |
|
|
| Classifier.php | File | 131 B | 0777 |
|
| DecisionTree.php | File | 14.62 KB | 0777 |
|
| KNearestNeighbors.php | File | 1.68 KB | 0777 |
|
| MLPClassifier.php | File | 1.38 KB | 0777 |
|
| NaiveBayes.php | File | 5.6 KB | 0777 |
|
| SVC.php | File | 744 B | 0777 |
|
| WeightedClassifier.php | File | 369 B | 0777 |
|