Skip to content

Commit

Permalink
Upgrade to PHP 7.1 (#150)
Browse files Browse the repository at this point in the history
* upgrade to PHP 7.1

* bump travis and composer to PHP 7.1

* fix tests
  • Loading branch information
Tomáš Votruba authored and akondas committed Nov 14, 2017
1 parent 331d4b1 commit 653c7c7
Show file tree
Hide file tree
Showing 127 changed files with 419 additions and 420 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ matrix:
fast_finish: true

include:
- os: linux
php: '7.0'

- os: linux
php: '7.1'

Expand All @@ -18,7 +15,7 @@ matrix:
language: generic
env:
- _OSX=10.11
- _PHP: php70
- _PHP: php71

before_install:
- if [[ "${TRAVIS_OS_NAME}" == "osx" ]]; then /usr/bin/env bash tools/prepare_osx_env.sh ; fi
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}
},
"require": {
"php": ">=7.0.0"
"php": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^6.0",
Expand Down
12 changes: 6 additions & 6 deletions src/Phpml/Association/Apriori.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class Apriori implements Associator
{
use Trainable, Predictable;

const ARRAY_KEY_ANTECEDENT = 'antecedent';
public const ARRAY_KEY_ANTECEDENT = 'antecedent';

const ARRAY_KEY_CONFIDENCE = 'confidence';
public const ARRAY_KEY_CONFIDENCE = 'confidence';

const ARRAY_KEY_CONSEQUENT = 'consequent';
public const ARRAY_KEY_CONSEQUENT = 'consequent';

const ARRAY_KEY_SUPPORT = 'support';
public const ARRAY_KEY_SUPPORT = 'support';

/**
* Minimum relative probability of frequent transactions.
Expand Down Expand Up @@ -116,7 +116,7 @@ protected function predictSample(array $sample) : array
/**
* Generate rules for each k-length frequent item set.
*/
private function generateAllRules()
private function generateAllRules(): void
{
for ($k = 2; !empty($this->large[$k]); ++$k) {
foreach ($this->large[$k] as $frequent) {
Expand All @@ -130,7 +130,7 @@ private function generateAllRules()
*
* @param mixed[] $frequent
*/
private function generateRules(array $frequent)
private function generateRules(array $frequent): void
{
foreach ($this->antecedents($frequent) as $antecedent) {
if ($this->confidence <= ($confidence = $this->confidence($frequent, $antecedent))) {
Expand Down
8 changes: 4 additions & 4 deletions src/Phpml/Classification/DecisionTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class DecisionTree implements Classifier
{
use Trainable, Predictable;

const CONTINUOUS = 1;
const NOMINAL = 2;
public const CONTINUOUS = 1;
public const NOMINAL = 2;

/**
* @var array
Expand Down Expand Up @@ -72,7 +72,7 @@ public function __construct(int $maxDepth = 10)
$this->maxDepth = $maxDepth;
}

public function train(array $samples, array $targets)
public function train(array $samples, array $targets): void
{
$this->samples = array_merge($this->samples, $samples);
$this->targets = array_merge($this->targets, $targets);
Expand Down Expand Up @@ -354,7 +354,7 @@ public function setNumFeatures(int $numFeatures)
/**
* Used to set predefined features to consider while deciding which column to use for a split
*/
protected function setSelectedFeatures(array $selectedFeatures)
protected function setSelectedFeatures(array $selectedFeatures): void
{
$this->selectedFeatures = $selectedFeatures;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Phpml/Classification/Ensemble/AdaBoost.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function __construct(int $maxIterations = 50)
/**
* Sets the base classifier that will be used for boosting (default = DecisionStump)
*/
public function setBaseClassifier(string $baseClassifier = DecisionStump::class, array $classifierOptions = [])
public function setBaseClassifier(string $baseClassifier = DecisionStump::class, array $classifierOptions = []): void
{
$this->baseClassifier = $baseClassifier;
$this->classifierOptions = $classifierOptions;
Expand All @@ -93,7 +93,7 @@ public function setBaseClassifier(string $baseClassifier = DecisionStump::class,
/**
* @throws \Exception
*/
public function train(array $samples, array $targets)
public function train(array $samples, array $targets): void
{
// Initialize usual variables
$this->labels = array_keys(array_count_values($targets));
Expand Down Expand Up @@ -149,7 +149,7 @@ protected function getBestClassifier() : Classifier
$classifier->setSampleWeights($this->weights);
$classifier->train($this->samples, $this->targets);
} else {
list($samples, $targets) = $this->resample();
[$samples, $targets] = $this->resample();
$classifier->train($samples, $targets);
}

Expand Down Expand Up @@ -216,7 +216,7 @@ protected function calculateAlpha(float $errorRate) : float
/**
* Updates the sample weights
*/
protected function updateWeights(Classifier $classifier, float $alpha)
protected function updateWeights(Classifier $classifier, float $alpha): void
{
$sumOfWeights = array_sum($this->weights);
$weightsT1 = [];
Expand Down
4 changes: 2 additions & 2 deletions src/Phpml/Classification/Ensemble/Bagging.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function setClassifer(string $classifier, array $classifierOptions = [])
return $this;
}

public function train(array $samples, array $targets)
public function train(array $samples, array $targets): void
{
$this->samples = array_merge($this->samples, $samples);
$this->targets = array_merge($this->targets, $targets);
Expand All @@ -117,7 +117,7 @@ public function train(array $samples, array $targets)
$this->classifiers = $this->initClassifiers();
$index = 0;
foreach ($this->classifiers as $classifier) {
list($samples, $targets) = $this->getRandomSubset($index);
[$samples, $targets] = $this->getRandomSubset($index);
$classifier->train($samples, $targets);
++$index;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Phpml/Classification/KNearestNeighbors.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class KNearestNeighbors implements Classifier
/**
* @param Distance|null $distanceMetric (if null then Euclidean distance as default)
*/
public function __construct(int $k = 3, Distance $distanceMetric = null)
public function __construct(int $k = 3, ?Distance $distanceMetric = null)
{
if (null === $distanceMetric) {
$distanceMetric = new Euclidean();
Expand Down
4 changes: 2 additions & 2 deletions src/Phpml/Classification/Linear/Adaline.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ class Adaline extends Perceptron
/**
* Batch training is the default Adaline training algorithm
*/
const BATCH_TRAINING = 1;
public const BATCH_TRAINING = 1;

/**
* Online training: Stochastic gradient descent learning
*/
const ONLINE_TRAINING = 2;
public const ONLINE_TRAINING = 2;

/**
* Training type may be either 'Batch' or 'Online' learning
Expand Down
14 changes: 7 additions & 7 deletions src/Phpml/Classification/Linear/DecisionStump.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DecisionStump extends WeightedClassifier
{
use Predictable, OneVsRest;

const AUTO_SELECT = -1;
public const AUTO_SELECT = -1;

/**
* @var int
Expand Down Expand Up @@ -86,7 +86,7 @@ public function __construct(int $columnIndex = self::AUTO_SELECT)
/**
* @throws \Exception
*/
protected function trainBinary(array $samples, array $targets, array $labels)
protected function trainBinary(array $samples, array $targets, array $labels): void
{
$this->binaryLabels = $labels;
$this->featureCount = count($samples[0]);
Expand Down Expand Up @@ -146,7 +146,7 @@ protected function trainBinary(array $samples, array $targets, array $labels)
* points to be probed. The more split counts, the better performance but
* worse processing time (Default value is 10.0)
*/
public function setNumericalSplitCount(float $count)
public function setNumericalSplitCount(float $count): void
{
$this->numSplitCount = $count;
}
Expand All @@ -171,7 +171,7 @@ protected function getBestNumericalSplit(array $samples, array $targets, int $co
// Before trying all possible split points, let's first try
// the average value for the cut point
$threshold = array_sum($values) / (float) count($values);
list($errorRate, $prob) = $this->calculateErrorRate($targets, $threshold, $operator, $values);
[$errorRate, $prob] = $this->calculateErrorRate($targets, $threshold, $operator, $values);
if ($split == null || $errorRate < $split['trainingErrorRate']) {
$split = ['value' => $threshold, 'operator' => $operator,
'prob' => $prob, 'column' => $col,
Expand All @@ -181,7 +181,7 @@ protected function getBestNumericalSplit(array $samples, array $targets, int $co
// Try other possible points one by one
for ($step = $minValue; $step <= $maxValue; $step += $stepSize) {
$threshold = (float) $step;
list($errorRate, $prob) = $this->calculateErrorRate($targets, $threshold, $operator, $values);
[$errorRate, $prob] = $this->calculateErrorRate($targets, $threshold, $operator, $values);
if ($errorRate < $split['trainingErrorRate']) {
$split = ['value' => $threshold, 'operator' => $operator,
'prob' => $prob, 'column' => $col,
Expand All @@ -203,7 +203,7 @@ protected function getBestNominalSplit(array $samples, array $targets, int $col)

foreach (['=', '!='] as $operator) {
foreach ($distinctVals as $val) {
list($errorRate, $prob) = $this->calculateErrorRate($targets, $val, $operator, $values);
[$errorRate, $prob] = $this->calculateErrorRate($targets, $val, $operator, $values);

if ($split == null || $split['trainingErrorRate'] < $errorRate) {
$split = ['value' => $val, 'operator' => $operator,
Expand Down Expand Up @@ -289,7 +289,7 @@ protected function predictSampleBinary(array $sample)
return $this->binaryLabels[1];
}

protected function resetBinary()
protected function resetBinary(): void
{
}

Expand Down
12 changes: 6 additions & 6 deletions src/Phpml/Classification/Linear/LogisticRegression.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ class LogisticRegression extends Adaline
/**
* Batch training: Gradient descent algorithm (default)
*/
const BATCH_TRAINING = 1;
public const BATCH_TRAINING = 1;

/**
* Online training: Stochastic gradient descent learning
*/
const ONLINE_TRAINING = 2;
public const ONLINE_TRAINING = 2;

/**
* Conjugate Batch: Conjugate Gradient algorithm
*/
const CONJUGATE_GRAD_TRAINING = 3;
public const CONJUGATE_GRAD_TRAINING = 3;

/**
* Cost function to optimize: 'log' and 'sse' are supported <br>
Expand Down Expand Up @@ -97,7 +97,7 @@ public function __construct(
* Sets the learning rate if gradient descent algorithm is
* selected for training
*/
public function setLearningRate(float $learningRate)
public function setLearningRate(float $learningRate): void
{
$this->learningRate = $learningRate;
}
Expand All @@ -106,7 +106,7 @@ public function setLearningRate(float $learningRate)
* Lambda (λ) parameter of regularization term. If 0 is given,
* then the regularization term is cancelled
*/
public function setLambda(float $lambda)
public function setLambda(float $lambda): void
{
$this->lambda = $lambda;
}
Expand Down Expand Up @@ -139,7 +139,7 @@ protected function runTraining(array $samples, array $targets)
/**
* Executes Conjugate Gradient method to optimize the weights of the LogReg model
*/
protected function runConjugateGradient(array $samples, array $targets, \Closure $gradientFunc)
protected function runConjugateGradient(array $samples, array $targets, \Closure $gradientFunc): void
{
if (empty($this->optimizer)) {
$this->optimizer = (new ConjugateGradient($this->featureCount))
Expand Down
10 changes: 6 additions & 4 deletions src/Phpml/Classification/Linear/Perceptron.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ public function __construct(float $learningRate = 0.001, int $maxIterations = 10
$this->maxIterations = $maxIterations;
}

public function partialTrain(array $samples, array $targets, array $labels = [])
public function partialTrain(array $samples, array $targets, array $labels = []): void
{
$this->trainByLabel($samples, $targets, $labels);
}

public function trainBinary(array $samples, array $targets, array $labels)
public function trainBinary(array $samples, array $targets, array $labels): void
{
if ($this->normalizer) {
$this->normalizer->transform($samples);
Expand All @@ -111,7 +111,7 @@ public function trainBinary(array $samples, array $targets, array $labels)
$this->runTraining($samples, $targets);
}

protected function resetBinary()
protected function resetBinary(): void
{
$this->labels = [];
$this->optimizer = null;
Expand Down Expand Up @@ -148,6 +148,8 @@ public function getCostValues() : array
/**
* Trains the perceptron model with Stochastic Gradient Descent optimization
* to get the correct set of weights
*
* @return void|mixed
*/
protected function runTraining(array $samples, array $targets)
{
Expand All @@ -169,7 +171,7 @@ protected function runTraining(array $samples, array $targets)
* Executes a Gradient Descent algorithm for
* the given cost function
*/
protected function runGradientDescent(array $samples, array $targets, \Closure $gradientFunc, bool $isBatch = false)
protected function runGradientDescent(array $samples, array $targets, \Closure $gradientFunc, bool $isBatch = false): void
{
$class = $isBatch ? GD::class : StochasticGD::class;

Expand Down
2 changes: 1 addition & 1 deletion src/Phpml/Classification/MLPClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function predictSample(array $sample)
/**
* @param mixed $target
*/
protected function trainSample(array $sample, $target)
protected function trainSample(array $sample, $target): void
{

// Feed-forward.
Expand Down
10 changes: 5 additions & 5 deletions src/Phpml/Classification/NaiveBayes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class NaiveBayes implements Classifier
{
use Trainable, Predictable;

const CONTINUOS = 1;
const NOMINAL = 2;
const EPSILON = 1e-10;
public const CONTINUOS = 1;
public const NOMINAL = 2;
public const EPSILON = 1e-10;

/**
* @var array
Expand Down Expand Up @@ -57,7 +57,7 @@ class NaiveBayes implements Classifier
*/
private $labels = [];

public function train(array $samples, array $targets)
public function train(array $samples, array $targets): void
{
$this->samples = array_merge($this->samples, $samples);
$this->targets = array_merge($this->targets, $targets);
Expand All @@ -77,7 +77,7 @@ public function train(array $samples, array $targets)
* Calculates vital statistics for each label & feature. Stores these
* values in private array in order to avoid repeated calculation
*/
private function calculateStatistics(string $label, array $samples)
private function calculateStatistics(string $label, array $samples): void
{
$this->std[$label] = array_fill(0, $this->featureCount, 0);
$this->mean[$label] = array_fill(0, $this->featureCount, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/Phpml/Classification/SVC.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct(
int $kernel = Kernel::LINEAR,
float $cost = 1.0,
int $degree = 3,
float $gamma = null,
?float $gamma = null,
float $coef0 = 0.0,
float $tolerance = 0.001,
int $cacheSize = 100,
Expand Down
2 changes: 1 addition & 1 deletion src/Phpml/Classification/WeightedClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class WeightedClassifier implements Classifier
/**
* Sets the array including a weight for each sample
*/
public function setSampleWeights(array $weights)
public function setSampleWeights(array $weights): void
{
$this->weights = $weights;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Phpml/Clustering/DBSCAN.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class DBSCAN implements Clusterer
*/
private $distanceMetric;

public function __construct(float $epsilon = 0.5, int $minSamples = 3, Distance $distanceMetric = null)
public function __construct(float $epsilon = 0.5, int $minSamples = 3, ?Distance $distanceMetric = null)
{
if (null === $distanceMetric) {
$distanceMetric = new Euclidean();
Expand Down
Loading

0 comments on commit 653c7c7

Please sign in to comment.