Use C-style casts (#124)

This commit is contained in:
Marcin Michalski 2017-09-02 21:41:06 +02:00 committed by Arkadiusz Kondas
parent 8c06a55a16
commit ba2b8c8a9c
6 changed files with 13 additions and 12 deletions

View File

@ -225,9 +225,9 @@ class DecisionTree implements Classifier
// will also be saved into the leaf for future access // will also be saved into the leaf for future access
if ($this->columnTypes[$i] == self::CONTINUOUS) { if ($this->columnTypes[$i] == self::CONTINUOUS) {
$matches = []; $matches = [];
preg_match("/^([<>=]{1,2})\s*(.*)/", strval($split->value), $matches); preg_match("/^([<>=]{1,2})\s*(.*)/", (string) $split->value, $matches);
$split->operator = $matches[1]; $split->operator = $matches[1];
$split->numericValue = floatval($matches[2]); $split->numericValue = (float) $matches[2];
} }
$bestSplit = $split; $bestSplit = $split;
@ -301,7 +301,7 @@ class DecisionTree implements Classifier
$sum = array_sum(array_column($countMatrix, $i)); $sum = array_sum(array_column($countMatrix, $i));
if ($sum > 0) { if ($sum > 0) {
foreach ($this->labels as $label) { foreach ($this->labels as $label) {
$part += pow($countMatrix[$label][$i] / floatval($sum), 2); $part += pow($countMatrix[$label][$i] / (float) $sum, 2);
} }
} }

View File

@ -81,7 +81,7 @@ class DecisionTreeLeaf
if ($this->isContinuous) { if ($this->isContinuous) {
$op = $this->operator; $op = $this->operator;
$value = $this->numericValue; $value = $this->numericValue;
$recordField = strval($recordField); $recordField = (string) $recordField;
eval("\$result = $recordField $op $value;"); eval("\$result = $recordField $op $value;");
return $result; return $result;
@ -139,7 +139,7 @@ class DecisionTreeLeaf
$col = "col_$this->columnIndex"; $col = "col_$this->columnIndex";
} }
if (!preg_match('/^[<>=]{1,2}/', strval($value))) { if (!preg_match('/^[<>=]{1,2}/', (string) $value)) {
$value = "=$value"; $value = "=$value";
} }

View File

@ -285,7 +285,7 @@ class DecisionStump extends WeightedClassifier
} }
$target = $targets[$index]; $target = $targets[$index];
if (strval($predicted) != strval($targets[$index])) { if ((string) $predicted != (string) $targets[$index]) {
$wrong += $this->weights[$index]; $wrong += $this->weights[$index];
} }
@ -300,7 +300,7 @@ class DecisionStump extends WeightedClassifier
foreach ($prob as $leaf => $counts) { foreach ($prob as $leaf => $counts) {
$leafTotal = (float) array_sum($prob[$leaf]); $leafTotal = (float) array_sum($prob[$leaf]);
foreach ($counts as $label => $count) { foreach ($counts as $label => $count) {
if (strval($leaf) == strval($label)) { if ((string) $leaf == (string) $label) {
$dist[$leaf] = $count / $leafTotal; $dist[$leaf] = $count / $leafTotal;
} }
} }
@ -323,7 +323,7 @@ class DecisionStump extends WeightedClassifier
protected function predictProbability(array $sample, $label) : float protected function predictProbability(array $sample, $label) : float
{ {
$predicted = $this->predictSampleBinary($sample); $predicted = $this->predictSampleBinary($sample);
if (strval($predicted) == strval($label)) { if ((string) $predicted == (string) $label) {
return $this->prob[$label]; return $this->prob[$label];
} }

View File

@ -288,7 +288,7 @@ class LogisticRegression extends Adaline
{ {
$predicted = $this->predictSampleBinary($sample); $predicted = $this->predictSampleBinary($sample);
if (strval($predicted) == strval($label)) { if ((string) $predicted == (string) $label) {
$sample = $this->checkNormalizedSample($sample); $sample = $this->checkNormalizedSample($sample);
return abs($this->output($sample) - 0.5); return abs($this->output($sample) - 0.5);

View File

@ -113,7 +113,7 @@ class Perceptron implements Classifier, IncrementalEstimator
// Set all target values to either -1 or 1 // Set all target values to either -1 or 1
$this->labels = [1 => $labels[0], -1 => $labels[1]]; $this->labels = [1 => $labels[0], -1 => $labels[1]];
foreach ($targets as $key => $target) { 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 // Set samples and feature count vars
@ -275,7 +275,7 @@ class Perceptron implements Classifier, IncrementalEstimator
{ {
$predicted = $this->predictSampleBinary($sample); $predicted = $this->predictSampleBinary($sample);
if (strval($predicted) == strval($label)) { if ((string) $predicted == (string) $label) {
$sample = $this->checkNormalizedSample($sample); $sample = $this->checkNormalizedSample($sample);
return abs($this->output($sample)); return abs($this->output($sample));

View File

@ -36,7 +36,8 @@ class EigenDecompositionTest extends TestCase
// (We, for now, omit non-symmetric matrices whose eigenvalues can be complex numbers) // (We, for now, omit non-symmetric matrices whose eigenvalues can be complex numbers)
$len = 3; $len = 3;
$A = array_fill(0, $len, array_fill(0, $len, 0.0)); $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 ($i = 0; $i < $len; ++$i) {
for ($k = 0; $k < $len; ++$k) { for ($k = 0; $k < $len; ++$k) {
if ($i > $k) { if ($i > $k) {