mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-01-23 23:28:24 +00:00
add fit method for Transformer interface
This commit is contained in:
parent
4554011899
commit
557f344018
17
src/Phpml/Exception/PreprocessorException.php
Normal file
17
src/Phpml/Exception/PreprocessorException.php
Normal 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.');
|
||||
}
|
||||
|
||||
}
|
@ -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];
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -6,6 +6,12 @@ namespace Phpml;
|
||||
|
||||
interface Transformer
|
||||
{
|
||||
|
||||
/**
|
||||
* @param array $samples
|
||||
*/
|
||||
public function fit(array $samples);
|
||||
|
||||
/**
|
||||
* @param array $samples
|
||||
*/
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user