From ba2b8c8a9ccb1ce072d49d835b0d4b12cc0a4a62 Mon Sep 17 00:00:00 2001 From: Marcin Michalski Date: Sat, 2 Sep 2017 21:41:06 +0200 Subject: [PATCH] Use C-style casts (#124) --- src/Phpml/Classification/DecisionTree.php | 6 +++--- src/Phpml/Classification/DecisionTree/DecisionTreeLeaf.php | 4 ++-- src/Phpml/Classification/Linear/DecisionStump.php | 6 +++--- src/Phpml/Classification/Linear/LogisticRegression.php | 2 +- src/Phpml/Classification/Linear/Perceptron.php | 4 ++-- tests/Phpml/Math/LinearAlgebra/EigenDecompositionTest.php | 3 ++- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Phpml/Classification/DecisionTree.php b/src/Phpml/Classification/DecisionTree.php index 6cf6870..feab32e 100644 --- a/src/Phpml/Classification/DecisionTree.php +++ b/src/Phpml/Classification/DecisionTree.php @@ -225,9 +225,9 @@ class DecisionTree implements Classifier // will also be saved into the leaf for future access if ($this->columnTypes[$i] == self::CONTINUOUS) { $matches = []; - preg_match("/^([<>=]{1,2})\s*(.*)/", strval($split->value), $matches); + preg_match("/^([<>=]{1,2})\s*(.*)/", (string) $split->value, $matches); $split->operator = $matches[1]; - $split->numericValue = floatval($matches[2]); + $split->numericValue = (float) $matches[2]; } $bestSplit = $split; @@ -301,7 +301,7 @@ class DecisionTree implements Classifier $sum = array_sum(array_column($countMatrix, $i)); if ($sum > 0) { foreach ($this->labels as $label) { - $part += pow($countMatrix[$label][$i] / floatval($sum), 2); + $part += pow($countMatrix[$label][$i] / (float) $sum, 2); } } diff --git a/src/Phpml/Classification/DecisionTree/DecisionTreeLeaf.php b/src/Phpml/Classification/DecisionTree/DecisionTreeLeaf.php index 9002458..b658c21 100644 --- a/src/Phpml/Classification/DecisionTree/DecisionTreeLeaf.php +++ b/src/Phpml/Classification/DecisionTree/DecisionTreeLeaf.php @@ -81,7 +81,7 @@ class DecisionTreeLeaf if ($this->isContinuous) { $op = $this->operator; $value = $this->numericValue; - $recordField = strval($recordField); + $recordField = (string) $recordField; eval("\$result = $recordField $op $value;"); return $result; @@ -139,7 +139,7 @@ class DecisionTreeLeaf $col = "col_$this->columnIndex"; } - if (!preg_match('/^[<>=]{1,2}/', strval($value))) { + if (!preg_match('/^[<>=]{1,2}/', (string) $value)) { $value = "=$value"; } diff --git a/src/Phpml/Classification/Linear/DecisionStump.php b/src/Phpml/Classification/Linear/DecisionStump.php index 776a6a2..2780638 100644 --- a/src/Phpml/Classification/Linear/DecisionStump.php +++ b/src/Phpml/Classification/Linear/DecisionStump.php @@ -285,7 +285,7 @@ class DecisionStump extends WeightedClassifier } $target = $targets[$index]; - if (strval($predicted) != strval($targets[$index])) { + if ((string) $predicted != (string) $targets[$index]) { $wrong += $this->weights[$index]; } @@ -300,7 +300,7 @@ class DecisionStump extends WeightedClassifier foreach ($prob as $leaf => $counts) { $leafTotal = (float) array_sum($prob[$leaf]); foreach ($counts as $label => $count) { - if (strval($leaf) == strval($label)) { + if ((string) $leaf == (string) $label) { $dist[$leaf] = $count / $leafTotal; } } @@ -323,7 +323,7 @@ class DecisionStump extends WeightedClassifier protected function predictProbability(array $sample, $label) : float { $predicted = $this->predictSampleBinary($sample); - if (strval($predicted) == strval($label)) { + if ((string) $predicted == (string) $label) { return $this->prob[$label]; } diff --git a/src/Phpml/Classification/Linear/LogisticRegression.php b/src/Phpml/Classification/Linear/LogisticRegression.php index 0447ef8..e3f0482 100644 --- a/src/Phpml/Classification/Linear/LogisticRegression.php +++ b/src/Phpml/Classification/Linear/LogisticRegression.php @@ -288,7 +288,7 @@ class LogisticRegression extends Adaline { $predicted = $this->predictSampleBinary($sample); - if (strval($predicted) == strval($label)) { + if ((string) $predicted == (string) $label) { $sample = $this->checkNormalizedSample($sample); return abs($this->output($sample) - 0.5); diff --git a/src/Phpml/Classification/Linear/Perceptron.php b/src/Phpml/Classification/Linear/Perceptron.php index 000059f..1c534a2 100644 --- a/src/Phpml/Classification/Linear/Perceptron.php +++ b/src/Phpml/Classification/Linear/Perceptron.php @@ -113,7 +113,7 @@ class Perceptron implements Classifier, IncrementalEstimator // Set all target values to either -1 or 1 $this->labels = [1 => $labels[0], -1 => $labels[1]]; foreach ($targets as $key => $target) { - $targets[$key] = strval($target) == strval($this->labels[1]) ? 1 : -1; + $targets[$key] = (string) $target == (string) $this->labels[1] ? 1 : -1; } // Set samples and feature count vars @@ -275,7 +275,7 @@ class Perceptron implements Classifier, IncrementalEstimator { $predicted = $this->predictSampleBinary($sample); - if (strval($predicted) == strval($label)) { + if ((string) $predicted == (string) $label) { $sample = $this->checkNormalizedSample($sample); return abs($this->output($sample)); diff --git a/tests/Phpml/Math/LinearAlgebra/EigenDecompositionTest.php b/tests/Phpml/Math/LinearAlgebra/EigenDecompositionTest.php index 8293d5e..1db2c66 100644 --- a/tests/Phpml/Math/LinearAlgebra/EigenDecompositionTest.php +++ b/tests/Phpml/Math/LinearAlgebra/EigenDecompositionTest.php @@ -36,7 +36,8 @@ class EigenDecompositionTest extends TestCase // (We, for now, omit non-symmetric matrices whose eigenvalues can be complex numbers) $len = 3; $A = array_fill(0, $len, array_fill(0, $len, 0.0)); - srand(intval(microtime(true) * 1000)); + $seed = microtime(true) * 1000; + srand((int) $seed); for ($i = 0; $i < $len; ++$i) { for ($k = 0; $k < $len; ++$k) { if ($i > $k) {