mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-24 22:07:33 +00:00
implement minkowski distance metric function
This commit is contained in:
parent
d82a12497a
commit
79b76fb1a4
49
src/Phpml/Metric/Distance/Minkowski.php
Normal file
49
src/Phpml/Metric/Distance/Minkowski.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace Phpml\Metric\Distance;
|
||||
|
||||
use Phpml\Exception\InvalidArgumentException;
|
||||
use Phpml\Metric\Distance;
|
||||
|
||||
class Minkowski implements Distance
|
||||
{
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $lambda;
|
||||
|
||||
/**
|
||||
* @param float $lambda
|
||||
*/
|
||||
public function __construct(float $lambda = 3)
|
||||
{
|
||||
$this->lambda = $lambda;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $a
|
||||
* @param array $b
|
||||
*
|
||||
* @return float
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function distance(array $a, array $b): float
|
||||
{
|
||||
if (count($a) !== count($b)) {
|
||||
throw InvalidArgumentException::sizeNotMatch();
|
||||
}
|
||||
|
||||
$distance = 0;
|
||||
$count = count($a);
|
||||
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$distance += pow(abs($a[$i] - $b[$i]), $this->lambda);
|
||||
}
|
||||
|
||||
return pow($distance, 1 / $this->lambda);
|
||||
}
|
||||
}
|
77
tests/Phpml/Metric/Distance/MinkowskiTest.php
Normal file
77
tests/Phpml/Metric/Distance/MinkowskiTest.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace tests\Phpml\Metric;
|
||||
|
||||
use Phpml\Metric\Distance\Minkowski;
|
||||
|
||||
class MinkowskiTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var Minkowski
|
||||
*/
|
||||
private $distanceMetric;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->distanceMetric = new Minkowski();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Phpml\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testThrowExceptionOnInvalidArguments()
|
||||
{
|
||||
$a = [0, 1, 2];
|
||||
$b = [0, 2];
|
||||
|
||||
$this->distanceMetric->distance($a, $b);
|
||||
}
|
||||
|
||||
public function testCalculateDistanceForOneDimension()
|
||||
{
|
||||
$a = [4];
|
||||
$b = [2];
|
||||
|
||||
$expectedDistance = 2;
|
||||
$actualDistance = $this->distanceMetric->distance($a, $b);
|
||||
|
||||
$this->assertEquals($expectedDistance, $actualDistance);
|
||||
}
|
||||
|
||||
public function testCalculateDistanceForTwoDimensions()
|
||||
{
|
||||
$a = [4, 6];
|
||||
$b = [2, 5];
|
||||
|
||||
$expectedDistance = 2.080;
|
||||
$actualDistance = $this->distanceMetric->distance($a, $b);
|
||||
|
||||
$this->assertEquals($expectedDistance, $actualDistance, '', $delta=0.001);
|
||||
}
|
||||
|
||||
public function testCalculateDistanceForThreeDimensions()
|
||||
{
|
||||
$a = [6, 10, 3];
|
||||
$b = [2, 5, 5];
|
||||
|
||||
$expectedDistance = 5.819;
|
||||
$actualDistance = $this->distanceMetric->distance($a, $b);
|
||||
|
||||
$this->assertEquals($expectedDistance, $actualDistance, '', $delta=0.001);
|
||||
}
|
||||
|
||||
public function testCalculateDistanceForThreeDimensionsWithDifferentLambda()
|
||||
{
|
||||
$distanceMetric = new Minkowski($lambda = 5);
|
||||
|
||||
$a = [6, 10, 3];
|
||||
$b = [2, 5, 5];
|
||||
|
||||
$expectedDistance = 5.300;
|
||||
$actualDistance = $distanceMetric->distance($a, $b);
|
||||
|
||||
$this->assertEquals($expectedDistance, $actualDistance, '', $delta=0.001);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user