create euclidean distance function

This commit is contained in:
Arkadiusz Kondas 2016-04-04 22:25:27 +02:00
parent f6a5611586
commit ce1653a5a7
6 changed files with 127 additions and 28 deletions

View File

@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
namespace Phpml\Classifier;
@ -9,7 +10,7 @@ interface Classifier
* @param array $features
* @param array $labels
*/
public function train($features, $labels);
public function train(array $features, array $labels);
/**
* @param mixed $feature

View File

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace Phpml\Classifier;
class KNearestNeighbors implements Classifier
{
/**
*
* @var int
*/
private $k;
/**
* @var array
*/
private $features;
/**
* @var array
*/
private $labels;
/**
* @param int $k
*/
public function __construct(int $k = 3)
{
$this->k = $k;
$this->features = [];
$this->labels = [];
}
/**
* @param array $features
* @param array $labels
*/
public function train(array $features, array $labels)
{
$this->features = $features;
$this->labels = $labels;
}
/**
* @param mixed $feature
* @return mixed
*/
public function predict($feature)
{
}
}

View File

@ -1,10 +1,27 @@
<?php
declare(strict_types=1);
namespace Phpml\Classifier;
abstract class NaiveBayes implements Classifier
class NaiveBayes implements Classifier
{
/**
* @param array $features
* @param array $labels
*/
public function train(array $features, array $labels)
{
}
/**
* @param mixed $feature
* @return mixed
*/
public function predict($feature)
{
}
}

View File

@ -1,26 +0,0 @@
<?php
use Phpml\Classifier\NaiveBayes;
class GaussianNaiveBayes extends NaiveBayes
{
/**
* @param array $features
* @param array $labels
*/
public function train($features, $labels)
{
}
/**
* @param mixed $feature
* @return mixed
*/
public function predict($feature)
{
}
}

View File

@ -0,0 +1,17 @@
<?php
declare(strict_types = 1);
namespace Phpml\Exception;
class InvalidArgumentException extends \Exception
{
/**
* @return InvalidArgumentException
*/
public static function parametersSizeNotMatch()
{
return new self('Size of parameters not match');
}
}

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types = 1);
namespace Phpml\Metric;
use Phpml\Exception\InvalidArgumentException;
class Distance
{
/**
* @param array $a
* @param array $b
*
* @return float
*
* @throws InvalidArgumentException
*/
public static function euclidean(array $a, array $b): float
{
if(count($a) != count($b))
{
throw InvalidArgumentException::parametersSizeNotMatch();
}
$distance = 0;
for($i=0; $i<count($a); $i++)
{
$distance += pow($a[$i] - $b[$i], 2);
}
return sqrt($distance);
}
}