add fit method for Transformer interface

This commit is contained in:
Arkadiusz Kondas 2016-06-17 00:08:10 +02:00
parent 4554011899
commit 557f344018
7 changed files with 68 additions and 2 deletions

View File

@ -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.');
}
}

View File

@ -16,7 +16,17 @@ 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);
@ -24,7 +34,13 @@ class TfIdfTransformer implements Transformer
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];

View File

@ -46,6 +46,14 @@ class TokenCountVectorizer implements Transformer
$this->frequencies = [];
}
/**
* @param array $samples
*/
public function fit(array $samples)
{
// TODO: Implement fit() method.
}
/**
* @param array $samples
*/

View File

@ -38,6 +38,14 @@ class Imputer implements Preprocessor
$this->axis = $axis;
}
/**
* @param array $samples
*/
public function fit(array $samples)
{
// TODO: Implement fit() method.
}
/**
* @param array $samples
*/

View File

@ -5,6 +5,7 @@ declare (strict_types = 1);
namespace Phpml\Preprocessing;
use Phpml\Exception\NormalizerException;
use Phpml\Exception\PreprocessorException;
class Normalizer implements Preprocessor
{
@ -30,6 +31,16 @@ class Normalizer implements Preprocessor
$this->norm = $norm;
}
/**
* @param array $samples
*
* @throws PreprocessorException
*/
public function fit(array $samples)
{
throw PreprocessorException::fitNotAllowed();
}
/**
* @param array $samples
*/

View File

@ -6,6 +6,12 @@ namespace Phpml;
interface Transformer
{
/**
* @param array $samples
*/
public function fit(array $samples);
/**
* @param array $samples
*/

View File

@ -22,7 +22,7 @@ class TfIdfTransformerTest extends \PHPUnit_Framework_TestCase
[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);