/** * @param array $samples * @param array $targets * * @throws InvalidArgumentException */ public function __construct(array $samples, array $targets) { if (count($samples) != count($targets)) { throw InvalidArgumentException::arraySizeNotMatch(); } $this->samples = $samples; $this->targets = $targets; }
/** * @param Dataset $dataset * @param float $testSize * @param int $seed * * @throws InvalidArgumentException */ public function __construct(Dataset $dataset, float $testSize = 0.3, int $seed = null) { if (0 >= $testSize || 1 <= $testSize) { throw InvalidArgumentException::percentNotInRange('testSize'); } $this->seedGenerator($seed); $this->splitDataset($dataset, $testSize); }
/** * @param int $clustersNumber * @param int $initialization * * @throws InvalidArgumentException */ public function __construct(int $clustersNumber, int $initialization = self::INIT_KMEANS_PLUS_PLUS) { if ($clustersNumber <= 0) { throw InvalidArgumentException::invalidClustersNumber(); } $this->clustersNumber = $clustersNumber; $this->initialization = $initialization; }
/** * @param string $language * * @return StopWords * * @throws InvalidArgumentException */ public static function factory($language = 'English') : StopWords { $className = __NAMESPACE__ . "\\StopWords\\{$language}"; if (!class_exists($className)) { throw InvalidArgumentException::invalidStopWordsLanguage($language); } return new $className(); }
/** * @param int $nodesNumber * @param string $nodeClass * @param ActivationFunction|null $activationFunction * * @throws InvalidArgumentException */ public function __construct(int $nodesNumber = 0, string $nodeClass = Neuron::class, ActivationFunction $activationFunction = null) { if (!in_array(Node::class, class_implements($nodeClass))) { throw InvalidArgumentException::invalidLayerNodeClass(); } for ($i = 0; $i < $nodesNumber; ++$i) { $this->nodes[] = $this->createNode($nodeClass, $activationFunction); } }
/** * @param array $layers * @param ActivationFunction|null $activationFunction * * @throws InvalidArgumentException */ public function __construct(array $layers, ActivationFunction $activationFunction = null) { if (count($layers) < 2) { throw InvalidArgumentException::invalidLayersNumber(); } $this->addInputLayer(array_shift($layers)); $this->addNeuronLayers($layers, $activationFunction); $this->addBiasNodes(); $this->generateSynapses(); }
/** * @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::arraySizeNotMatch(); } $distance = 0; $count = count($a); for ($i = 0; $i < $count; ++$i) { $distance += abs($a[$i] - $b[$i]); } return $distance; }
/** * @param array $actualLabels * @param array $predictedLabels * @param bool $normalize * * @return float|int * * @throws InvalidArgumentException */ public static function score(array $actualLabels, array $predictedLabels, bool $normalize = true) { if (count($actualLabels) != count($predictedLabels)) { throw InvalidArgumentException::arraySizeNotMatch(); } $score = 0; foreach ($actualLabels as $index => $label) { if ($label == $predictedLabels[$index]) { ++$score; } } if ($normalize) { $score = $score / count($actualLabels); } return $score; }
/** * @param array|float[] $a * @param bool $sample * * @return float * * @throws InvalidArgumentException */ public static function population(array $a, $sample = true) { if (empty($a)) { throw InvalidArgumentException::arrayCantBeEmpty(); } $n = count($a); if ($sample && $n === 1) { throw InvalidArgumentException::arraySizeToSmall(2); } $mean = Mean::arithmetic($a); $carry = 0.0; foreach ($a as $val) { $d = $val - $mean; $carry += $d * $d; } if ($sample) { --$n; } return sqrt((double) ($carry / $n)); }
/** * @param array|int[]|float[] $x * @param array|int[]|float[] $y * * @return float * * @throws InvalidArgumentException */ public static function pearson(array $x, array $y) { if (count($x) !== count($y)) { throw InvalidArgumentException::arraySizeNotMatch(); } $count = count($x); $meanX = Mean::arithmetic($x); $meanY = Mean::arithmetic($y); $axb = 0; $a2 = 0; $b2 = 0; for ($i = 0; $i < $count; ++$i) { $a = $x[$i] - $meanX; $b = $y[$i] - $meanY; $axb = $axb + $a * $b; $a2 = $a2 + pow($a, 2); $b2 = $b2 + pow($b, 2); } $corr = $axb / sqrt((double) ($a2 * $b2)); return $corr; }
/** * @param Matrix $matrix * * @return Matrix * * @throws InvalidArgumentException */ public function multiply(Matrix $matrix) { if ($this->columns != $matrix->getRows()) { throw InvalidArgumentException::inconsistentMatrixSupplied(); } $product = []; $multiplier = $matrix->toArray(); for ($i = 0; $i < $this->rows; ++$i) { $columns = $matrix->getColumns(); for ($j = 0; $j < $columns; ++$j) { $product[$i][$j] = 0; for ($k = 0; $k < $this->columns; ++$k) { $product[$i][$j] += $this->matrix[$i][$k] * $multiplier[$k][$j]; } } } return new self($product, false); }
/** * @param array $array * * @throws InvalidArgumentException */ private static function checkArrayLength(array $array) { if (0 == count($array)) { throw InvalidArgumentException::arrayCantBeEmpty(); } }