php-ml/docs/math/distance.md
Attila Bakos 7d5c6b15a4 Updates to the documentation (linguistic corrections) (#414)
* Fix typo in Features list

* Update distance.md documentation

* Fix grammatical mistakes in documentation

* Fix grammatical mistakes in documentation

* Fix grammatical mistakes in documentation

* Fix grammatical mistakes in documentation

* Fix grammatical mistakes in documentation

* Fix grammatical mistakes in documentation

* Fix grammatical mistakes in documentation

* Fix grammatical mistakes in documentation

* Fix grammatical mistakes in documentation
2019-11-02 11:41:34 +01:00

2.0 KiB

Distance

Selected algorithms require the use of a function for calculating the distance.

Euclidean

Class for calculating Euclidean distance.

euclidean

To calculate Euclidean distance:

$a = [4, 6];
$b = [2, 5];

$euclidean = new Euclidean();
$euclidean->distance($a, $b);
// return 2.2360679774998

Manhattan

Class for calculating Manhattan distance.

manhattan

To calculate Manhattan distance:

$a = [4, 6];
$b = [2, 5];

$manhattan = new Manhattan();
$manhattan->distance($a, $b);
// return 3

Chebyshev

Class for calculating Chebyshev distance.

chebyshev

To calculate Chebyshev distance:

$a = [4, 6];
$b = [2, 5];

$chebyshev = new Chebyshev();
$chebyshev->distance($a, $b);
// return 2

Minkowski

Class for calculating Minkowski distance.

minkowski

To calculate Minkowski distance:

$a = [4, 6];
$b = [2, 5];

$minkowski = new Minkowski();
$minkowski->distance($a, $b);
// return 2.080

You can provide the lambda parameter:

$a = [6, 10, 3];
$b = [2, 5, 5];

$minkowski = new Minkowski($lambda = 5);
$minkowski->distance($a, $b);
// return 5.300

Custom distance

To apply your own function of distance use the Distance interface. Example:

class CustomDistance implements Distance
{
    /**
     * @param array $a
     * @param array $b
     *
     * @return float
     */
    public function distance(array $a, array $b) : float
    {
        $distance = [];
        $count = count($a);

        for ($i = 0; $i < $count; ++$i) {
            $distance[] = $a[$i] * $b[$i];
        }

        return min($distance);
    }
}