mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-01-27 00:58:30 +00:00
php-cs-fxier
This commit is contained in:
parent
60c796f5d9
commit
633974fea0
@ -57,5 +57,4 @@ class InvalidArgumentException extends \Exception
|
|||||||
{
|
{
|
||||||
return new self('Inconsistent matrix aupplied');
|
return new self('Inconsistent matrix aupplied');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,5 +21,4 @@ class MatrixException extends \Exception
|
|||||||
{
|
{
|
||||||
return new self('Column out of range');
|
return new self('Column out of range');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
declare (strict_types = 1);
|
declare (strict_types = 1);
|
||||||
|
|
||||||
namespace Phpml\Math;
|
namespace Phpml\Math;
|
||||||
@ -40,7 +41,7 @@ class Matrix
|
|||||||
$this->columns = count($matrix[0]);
|
$this->columns = count($matrix[0]);
|
||||||
|
|
||||||
if ($validate) {
|
if ($validate) {
|
||||||
for ($i = 0; $i < $this->rows; $i++) {
|
for ($i = 0; $i < $this->rows; ++$i) {
|
||||||
if (count($matrix[$i]) !== $this->columns) {
|
if (count($matrix[$i]) !== $this->columns) {
|
||||||
throw InvalidArgumentException::matrixDimensionsDidNotMatch();
|
throw InvalidArgumentException::matrixDimensionsDidNotMatch();
|
||||||
}
|
}
|
||||||
@ -88,7 +89,7 @@ class Matrix
|
|||||||
}
|
}
|
||||||
|
|
||||||
$values = [];
|
$values = [];
|
||||||
for ($i = 0; $i < $this->rows; $i++) {
|
for ($i = 0; $i < $this->rows; ++$i) {
|
||||||
$values[] = $this->matrix[$i][$column];
|
$values[] = $this->matrix[$i][$column];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,7 +118,7 @@ class Matrix
|
|||||||
$determinant = $this->matrix[0][0] * $this->matrix[1][1] -
|
$determinant = $this->matrix[0][0] * $this->matrix[1][1] -
|
||||||
$this->matrix[0][1] * $this->matrix[1][0];
|
$this->matrix[0][1] * $this->matrix[1][0];
|
||||||
} else {
|
} else {
|
||||||
for ($j = 0; $j < $this->columns; $j++) {
|
for ($j = 0; $j < $this->columns; ++$j) {
|
||||||
$subMatrix = $this->crossOut(0, $j);
|
$subMatrix = $this->crossOut(0, $j);
|
||||||
if (fmod($j, 2) == 0) {
|
if (fmod($j, 2) == 0) {
|
||||||
$determinant += $this->matrix[0][$j] * $subMatrix->getDeterminant();
|
$determinant += $this->matrix[0][$j] * $subMatrix->getDeterminant();
|
||||||
@ -144,8 +145,8 @@ class Matrix
|
|||||||
public function transpose()
|
public function transpose()
|
||||||
{
|
{
|
||||||
$newMatrix = [];
|
$newMatrix = [];
|
||||||
for ($i = 0; $i < $this->rows; $i++) {
|
for ($i = 0; $i < $this->rows; ++$i) {
|
||||||
for ($j = 0; $j < $this->columns; $j++) {
|
for ($j = 0; $j < $this->columns; ++$j) {
|
||||||
$newMatrix[$j][$i] = $this->matrix[$i][$j];
|
$newMatrix[$j][$i] = $this->matrix[$i][$j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -168,14 +169,15 @@ class Matrix
|
|||||||
|
|
||||||
$product = [];
|
$product = [];
|
||||||
$multiplier = $matrix->toArray();
|
$multiplier = $matrix->toArray();
|
||||||
for ($i = 0; $i < $this->rows; $i++) {
|
for ($i = 0; $i < $this->rows; ++$i) {
|
||||||
for ($j = 0; $j < $matrix->getColumns(); $j++) {
|
for ($j = 0; $j < $matrix->getColumns(); ++$j) {
|
||||||
$product[$i][$j] = 0;
|
$product[$i][$j] = 0;
|
||||||
for ($k = 0; $k < $this->columns; $k++) {
|
for ($k = 0; $k < $this->columns; ++$k) {
|
||||||
$product[$i][$j] += $this->matrix[$i][$k] * $multiplier[$k][$j];
|
$product[$i][$j] += $this->matrix[$i][$k] * $multiplier[$k][$j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new self($product, false);
|
return new self($product, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -187,8 +189,8 @@ class Matrix
|
|||||||
public function divideByScalar($value)
|
public function divideByScalar($value)
|
||||||
{
|
{
|
||||||
$newMatrix = array();
|
$newMatrix = array();
|
||||||
for ($i = 0; $i < $this->rows; $i++) {
|
for ($i = 0; $i < $this->rows; ++$i) {
|
||||||
for ($j = 0; $j < $this->columns; $j++) {
|
for ($j = 0; $j < $this->columns; ++$j) {
|
||||||
$newMatrix[$i][$j] = $this->matrix[$i][$j] / $value;
|
$newMatrix[$i][$j] = $this->matrix[$i][$j] / $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -208,8 +210,8 @@ class Matrix
|
|||||||
}
|
}
|
||||||
|
|
||||||
$newMatrix = array();
|
$newMatrix = array();
|
||||||
for ($i = 0; $i < $this->rows; $i++) {
|
for ($i = 0; $i < $this->rows; ++$i) {
|
||||||
for ($j = 0; $j < $this->columns; $j++) {
|
for ($j = 0; $j < $this->columns; ++$j) {
|
||||||
$subMatrix = $this->crossOut($i, $j);
|
$subMatrix = $this->crossOut($i, $j);
|
||||||
if (fmod($i + $j, 2) == 0) {
|
if (fmod($i + $j, 2) == 0) {
|
||||||
$newMatrix[$i][$j] = ($subMatrix->getDeterminant());
|
$newMatrix[$i][$j] = ($subMatrix->getDeterminant());
|
||||||
@ -234,20 +236,19 @@ class Matrix
|
|||||||
{
|
{
|
||||||
$newMatrix = [];
|
$newMatrix = [];
|
||||||
$r = 0;
|
$r = 0;
|
||||||
for ($i = 0; $i < $this->rows; $i++) {
|
for ($i = 0; $i < $this->rows; ++$i) {
|
||||||
$c = 0;
|
$c = 0;
|
||||||
if ($row != $i) {
|
if ($row != $i) {
|
||||||
for ($j = 0; $j < $this->columns; $j++) {
|
for ($j = 0; $j < $this->columns; ++$j) {
|
||||||
if ($column != $j) {
|
if ($column != $j) {
|
||||||
$newMatrix[$r][$c] = $this->matrix[$i][$j];
|
$newMatrix[$r][$c] = $this->matrix[$i][$j];
|
||||||
$c++;
|
++$c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$r++;
|
++$r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new self($newMatrix, false);
|
return new self($newMatrix, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ class LeastSquares implements Regression
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* coefficient(b) = (X'X)-1X'Y
|
* coefficient(b) = (X'X)-1X'Y.
|
||||||
*/
|
*/
|
||||||
private function computeCoefficients()
|
private function computeCoefficients()
|
||||||
{
|
{
|
||||||
|
@ -48,5 +48,4 @@ class LeastSquaresTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
$this->assertEquals(4094, $regression->predict([60000, 1996]), '', $delta);
|
$this->assertEquals(4094, $regression->predict([60000, 1996]), '', $delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user