Skip to content

Commit

Permalink
add fit method for Transformer interface
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas committed Jun 16, 2016
1 parent 4554011 commit 557f344
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 2 deletions.
17 changes: 17 additions & 0 deletions src/Phpml/Exception/PreprocessorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types = 1);

namespace Phpml\Exception;

class PreprocessorException extends \Exception
{

/**
* @return PreprocessorException
*/
public static function fitNotAllowed()
{
return new self('Fit is not allowed for this preprocessor.');
}

}
18 changes: 17 additions & 1 deletion src/Phpml/FeatureExtraction/TfIdfTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,31 @@ class TfIdfTransformer implements Transformer
/**
* @param array $samples
*/
public function transform(array &$samples)
public function __construct(array $samples = null)
{
if($samples) {
$this->fit($samples);
}
}

/**
* @param array $samples
*/
public function fit(array $samples)
{
$this->countTokensFrequency($samples);

$count = count($samples);
foreach ($this->idf as &$value) {
$value = log($count / $value, 10);
}
}

/**
* @param array $samples
*/
public function transform(array &$samples)
{
foreach ($samples as &$sample) {
foreach ($sample as $index => &$feature) {
$feature = $feature * $this->idf[$index];
Expand Down
8 changes: 8 additions & 0 deletions src/Phpml/FeatureExtraction/TokenCountVectorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public function __construct(Tokenizer $tokenizer, float $minDF = 0)
$this->frequencies = [];
}

/**
* @param array $samples
*/
public function fit(array $samples)
{
// TODO: Implement fit() method.
}

/**
* @param array $samples
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Phpml/Preprocessing/Imputer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public function __construct($missingValue = null, Strategy $strategy, int $axis
$this->axis = $axis;
}

/**
* @param array $samples
*/
public function fit(array $samples)
{
// TODO: Implement fit() method.
}

/**
* @param array $samples
*/
Expand Down
11 changes: 11 additions & 0 deletions src/Phpml/Preprocessing/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Phpml\Preprocessing;

use Phpml\Exception\NormalizerException;
use Phpml\Exception\PreprocessorException;

class Normalizer implements Preprocessor
{
Expand All @@ -30,6 +31,16 @@ public function __construct(int $norm = self::NORM_L2)
$this->norm = $norm;
}

/**
* @param array $samples
*
* @throws PreprocessorException
*/
public function fit(array $samples)
{
throw PreprocessorException::fitNotAllowed();
}

/**
* @param array $samples
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Phpml/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

interface Transformer
{

/**
* @param array $samples
*/
public function fit(array $samples);

/**
* @param array $samples
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Phpml/FeatureExtraction/TfIdfTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testTfIdfTransformation()
[0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0.602, 5 => 0.903],
];

$transformer = new TfIdfTransformer();
$transformer = new TfIdfTransformer($samples);
$transformer->transform($samples);

$this->assertEquals($tfIdfSamples, $samples, '', 0.001);
Expand Down

0 comments on commit 557f344

Please sign in to comment.