Skip to content

Commit

Permalink
[coding standard] fix imports order and drop unused docs typehints (#…
Browse files Browse the repository at this point in the history
…145)

* fix imports order

* drop unused docs typehints, make use of return types where possible
  • Loading branch information
Tomáš Votruba authored and akondas committed Nov 6, 2017
1 parent a077265 commit f4650c6
Show file tree
Hide file tree
Showing 106 changed files with 301 additions and 1,733 deletions.
2 changes: 1 addition & 1 deletion docs/math/distance.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class CustomDistance implements Distance
*
* @return float
*/
public function distance(array $a, array $b): float
public function distance(array $a, array $b) : float
{
$distance = [];
$count = count($a);
Expand Down
15 changes: 0 additions & 15 deletions src/Phpml/Association/Apriori.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ class Apriori implements Associator

/**
* Apriori constructor.
*
* @param float $support
* @param float $confidence
*/
public function __construct(float $support = 0.0, float $confidence = 0.0)
{
Expand Down Expand Up @@ -261,8 +258,6 @@ private function candidates(array $samples) : array
*
* @param mixed[] $set
* @param mixed[] $subset
*
* @return float
*/
private function confidence(array $set, array $subset) : float
{
Expand All @@ -276,8 +271,6 @@ private function confidence(array $set, array $subset) : float
* @see \Phpml\Association\Apriori::samples
*
* @param mixed[] $sample
*
* @return float
*/
private function support(array $sample) : float
{
Expand All @@ -290,8 +283,6 @@ private function support(array $sample) : float
* @see \Phpml\Association\Apriori::samples
*
* @param mixed[] $sample
*
* @return int
*/
private function frequency(array $sample) : int
{
Expand All @@ -307,8 +298,6 @@ private function frequency(array $sample) : int
*
* @param mixed[][] $system
* @param mixed[] $set
*
* @return bool
*/
private function contains(array $system, array $set) : bool
{
Expand All @@ -322,8 +311,6 @@ private function contains(array $system, array $set) : bool
*
* @param mixed[] $set
* @param mixed[] $subset
*
* @return bool
*/
private function subset(array $set, array $subset) : bool
{
Expand All @@ -335,8 +322,6 @@ private function subset(array $set, array $subset) : bool
*
* @param mixed[] $set1
* @param mixed[] $set2
*
* @return bool
*/
private function equals(array $set1, array $set2) : bool
{
Expand Down
41 changes: 3 additions & 38 deletions src/Phpml/Classification/DecisionTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Phpml\Classification;

use Phpml\Classification\DecisionTree\DecisionTreeLeaf;
use Phpml\Exception\InvalidArgumentException;
use Phpml\Helper\Predictable;
use Phpml\Helper\Trainable;
use Phpml\Math\Statistic\Mean;
use Phpml\Classification\DecisionTree\DecisionTreeLeaf;

class DecisionTree implements Classifier
{
Expand Down Expand Up @@ -63,14 +63,10 @@ class DecisionTree implements Classifier
private $featureImportances = null;

/**
*
* @var array
*/
private $columnNames = null;

/**
* @param int $maxDepth
*/
public function __construct(int $maxDepth = 10)
{
$this->maxDepth = $maxDepth;
Expand Down Expand Up @@ -129,8 +125,6 @@ public static function getColumnTypes(array $samples) : array
/**
* @param array $records
* @param int $depth
*
* @return DecisionTreeLeaf
*/
protected function getSplitLeaf(array $records, int $depth = 0) : DecisionTreeLeaf
{
Expand Down Expand Up @@ -190,11 +184,6 @@ protected function getSplitLeaf(array $records, int $depth = 0) : DecisionTreeLe
return $split;
}

/**
* @param array $records
*
* @return DecisionTreeLeaf
*/
protected function getBestSplit(array $records) : DecisionTreeLeaf
{
$targets = array_intersect_key($this->targets, array_flip($records));
Expand Down Expand Up @@ -277,10 +266,6 @@ protected function getSelectedFeatures() : array

/**
* @param mixed $baseValue
* @param array $colValues
* @param array $targets
*
* @return float
*/
public function getGiniIndex($baseValue, array $colValues, array $targets) : float
{
Expand Down Expand Up @@ -342,8 +327,6 @@ protected function preprocess(array $samples) : array

/**
* @param array $columnValues
*
* @return bool
*/
protected static function isCategoricalColumn(array $columnValues) : bool
{
Expand Down Expand Up @@ -376,8 +359,6 @@ protected static function isCategoricalColumn(array $columnValues) : bool
* otherwise the given value will be used as a maximum for number of columns
* randomly selected for each split operation.
*
* @param int $numFeatures
*
* @return $this
*
* @throws InvalidArgumentException
Expand All @@ -395,8 +376,6 @@ public function setNumFeatures(int $numFeatures)

/**
* Used to set predefined features to consider while deciding which column to use for a split
*
* @param array $selectedFeatures
*/
protected function setSelectedFeatures(array $selectedFeatures)
{
Expand All @@ -407,8 +386,6 @@ protected function setSelectedFeatures(array $selectedFeatures)
* A string array to represent columns. Useful when HTML output or
* column importances are desired to be inspected.
*
* @param array $names
*
* @return $this
*
* @throws InvalidArgumentException
Expand All @@ -424,10 +401,7 @@ public function setColumnNames(array $names)
return $this;
}

/**
* @return string
*/
public function getHtml()
public function getHtml() : string
{
return $this->tree->getHTML($this->columnNames);
}
Expand All @@ -436,10 +410,8 @@ public function getHtml()
* This will return an array including an importance value for
* each column in the given dataset. The importance values are
* normalized and their total makes 1.<br/>
*
* @return array
*/
public function getFeatureImportances()
public function getFeatureImportances() : array
{
if ($this->featureImportances !== null) {
return $this->featureImportances;
Expand Down Expand Up @@ -473,11 +445,6 @@ public function getFeatureImportances()
/**
* Collects and returns an array of internal nodes that use the given
* column as a split criterion
*
* @param int $column
* @param DecisionTreeLeaf $node
*
* @return array
*/
protected function getSplitNodesByColumn(int $column, DecisionTreeLeaf $node) : array
{
Expand Down Expand Up @@ -506,8 +473,6 @@ protected function getSplitNodesByColumn(int $column, DecisionTreeLeaf $node) :
}

/**
* @param array $sample
*
* @return mixed
*/
protected function predictSample(array $sample)
Expand Down
25 changes: 5 additions & 20 deletions src/Phpml/Classification/DecisionTree/DecisionTreeLeaf.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,22 @@ class DecisionTreeLeaf
*/
public $level = 0;

/**
* @param array $record
*
* @return bool
*/
public function evaluate($record)
public function evaluate(array $record) : bool
{
$recordField = $record[$this->columnIndex];

if ($this->isContinuous) {
return Comparison::compare((string) $recordField, $this->numericValue, $this->operator);
}

return $recordField == $this->value;
}

/**
* Returns Mean Decrease Impurity (MDI) in the node.
* For terminal nodes, this value is equal to 0
*
* @param int $parentRecordCount
*
* @return float
*/
public function getNodeImpurityDecrease(int $parentRecordCount)
public function getNodeImpurityDecrease(int $parentRecordCount) : float
{
if ($this->isTerminal) {
return 0.0;
Expand All @@ -119,12 +110,8 @@ public function getNodeImpurityDecrease(int $parentRecordCount)

/**
* Returns HTML representation of the node including children nodes
*
* @param $columnNames
*
* @return string
*/
public function getHTML($columnNames = null)
public function getHTML($columnNames = null) : string
{
if ($this->isTerminal) {
$value = "<b>$this->classValue</b>";
Expand Down Expand Up @@ -170,10 +157,8 @@ public function getHTML($columnNames = null)

/**
* HTML representation of the tree without column names
*
* @return string
*/
public function __toString()
public function __toString() : string
{
return $this->getHTML();
}
Expand Down
39 changes: 7 additions & 32 deletions src/Phpml/Classification/Ensemble/AdaBoost.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Phpml\Classification\Ensemble;

use Phpml\Classification\Classifier;
use Phpml\Classification\Linear\DecisionStump;
use Phpml\Classification\WeightedClassifier;
use Phpml\Math\Statistic\Mean;
use Phpml\Math\Statistic\StandardDeviation;
use Phpml\Classification\Classifier;
use Phpml\Helper\Predictable;
use Phpml\Helper\Trainable;
use Phpml\Math\Statistic\Mean;
use Phpml\Math\Statistic\StandardDeviation;

class AdaBoost implements Classifier
{
Expand Down Expand Up @@ -75,8 +75,6 @@ class AdaBoost implements Classifier
* ADAptive BOOSTing (AdaBoost) is an ensemble algorithm to
* improve classification performance of 'weak' classifiers such as
* DecisionStump (default base classifier of AdaBoost).
*
* @param int $maxIterations
*/
public function __construct(int $maxIterations = 50)
{
Expand All @@ -85,9 +83,6 @@ public function __construct(int $maxIterations = 50)

/**
* Sets the base classifier that will be used for boosting (default = DecisionStump)
*
* @param string $baseClassifier
* @param array $classifierOptions
*/
public function setBaseClassifier(string $baseClassifier = DecisionStump::class, array $classifierOptions = [])
{
Expand All @@ -96,9 +91,6 @@ public function setBaseClassifier(string $baseClassifier = DecisionStump::class,
}

/**
* @param array $samples
* @param array $targets
*
* @throws \Exception
*/
public function train(array $samples, array $targets)
Expand Down Expand Up @@ -143,10 +135,8 @@ public function train(array $samples, array $targets)
/**
* Returns the classifier with the lowest error rate with the
* consideration of current sample weights
*
* @return Classifier
*/
protected function getBestClassifier()
protected function getBestClassifier() : Classifier
{
$ref = new \ReflectionClass($this->baseClassifier);
if ($this->classifierOptions) {
Expand All @@ -169,10 +159,8 @@ protected function getBestClassifier()
/**
* Resamples the dataset in accordance with the weights and
* returns the new dataset
*
* @return array
*/
protected function resample()
protected function resample() : array
{
$weights = $this->weights;
$std = StandardDeviation::population($weights);
Expand All @@ -198,12 +186,8 @@ protected function resample()

/**
* Evaluates the classifier and returns the classification error rate
*
* @param Classifier $classifier
*
* @return float
*/
protected function evaluateClassifier(Classifier $classifier)
protected function evaluateClassifier(Classifier $classifier) : float
{
$total = (float) array_sum($this->weights);
$wrong = 0;
Expand All @@ -219,12 +203,8 @@ protected function evaluateClassifier(Classifier $classifier)

/**
* Calculates alpha of a classifier
*
* @param float $errorRate
*
* @return float
*/
protected function calculateAlpha(float $errorRate)
protected function calculateAlpha(float $errorRate) : float
{
if ($errorRate == 0) {
$errorRate = 1e-10;
Expand All @@ -235,9 +215,6 @@ protected function calculateAlpha(float $errorRate)

/**
* Updates the sample weights
*
* @param Classifier $classifier
* @param float $alpha
*/
protected function updateWeights(Classifier $classifier, float $alpha)
{
Expand All @@ -256,8 +233,6 @@ protected function updateWeights(Classifier $classifier, float $alpha)
}

/**
* @param array $sample
*
* @return mixed
*/
public function predictSample(array $sample)
Expand Down
Loading

0 comments on commit f4650c6

Please sign in to comment.