mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-01-09 16:36:34 +00:00
mode (dominant) from numbers
This commit is contained in:
parent
ed1e07e803
commit
a761d0e8f2
@ -12,9 +12,13 @@ class Mean
|
||||
* @param array $numbers
|
||||
*
|
||||
* @return float
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function arithmetic(array $numbers)
|
||||
{
|
||||
self::checkArrayLength($numbers);
|
||||
|
||||
return array_sum($numbers) / count($numbers);
|
||||
}
|
||||
|
||||
@ -26,11 +30,10 @@ class Mean
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function median(array $numbers) {
|
||||
$count = count($numbers);
|
||||
if (0 == $count) {
|
||||
throw InvalidArgumentException::arrayCantBeEmpty();
|
||||
}
|
||||
|
||||
self::checkArrayLength($numbers);
|
||||
|
||||
$count = count($numbers);
|
||||
$middleIndex = floor($count / 2);
|
||||
sort($numbers, SORT_NUMERIC);
|
||||
$median = $numbers[$middleIndex];
|
||||
@ -42,4 +45,32 @@ class Mean
|
||||
return $median;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $numbers
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function mode(array $numbers)
|
||||
{
|
||||
self::checkArrayLength($numbers);
|
||||
|
||||
$values = array_count_values($numbers);
|
||||
|
||||
return array_search(max($values), $values);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
private static function checkArrayLength(array $array)
|
||||
{
|
||||
if (0 == count($array)) {
|
||||
throw InvalidArgumentException::arrayCantBeEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -8,6 +8,15 @@ use Phpml\Math\Statistic\Mean;
|
||||
|
||||
class MeanTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @expectedException \Phpml\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testArithmeticThrowExceptionOnEmptyArray()
|
||||
{
|
||||
Mean::arithmetic([]);
|
||||
}
|
||||
|
||||
public function testArithmeticMean()
|
||||
{
|
||||
$delta = 0.01;
|
||||
@ -19,7 +28,7 @@ class MeanTest extends \PHPUnit_Framework_TestCase
|
||||
/**
|
||||
* @expectedException \Phpml\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testThrowExceptionOnEmptyArrayMedian()
|
||||
public function testMedianThrowExceptionOnEmptyArray()
|
||||
{
|
||||
Mean::median([]);
|
||||
}
|
||||
@ -38,4 +47,19 @@ class MeanTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals(3.5, Mean::median($numbers));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Phpml\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testModeThrowExceptionOnEmptyArray()
|
||||
{
|
||||
Mean::mode([]);
|
||||
}
|
||||
|
||||
public function testModeOnArray()
|
||||
{
|
||||
$numbers = [5, 2, 6, 1, 3, 4, 6, 6, 5];
|
||||
|
||||
$this->assertEquals(6, Mean::mode($numbers));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user