Skip to content

Commit

Permalink
Make SVM non-locale aware (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas authored Jul 4, 2018
1 parent 4a3194f commit 8fdb3d1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This changelog references the relevant changes done in PHP-ML library.
* fix ensure DataTransformer::testSet samples array is not empty (#204)
* fix optimizer initial theta randomization (#239)
* fix travis build on osx (#281)
* fix SVM locale (non-locale aware) (#288)
* typo, tests, code styles and documentation fixes (#265, #261, #254, #253, #251, #250, #248, #245, #243)

* 0.6.2 (2018-02-22)
Expand Down
2 changes: 1 addition & 1 deletion src/SupportVectorMachine/DataTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private static function sampleRow(array $sample): string
{
$row = [];
foreach ($sample as $index => $feature) {
$row[] = sprintf('%s:%s', $index + 1, $feature);
$row[] = sprintf('%s:%F', $index + 1, $feature);
}

return implode(' ', $row);
Expand Down
2 changes: 1 addition & 1 deletion src/SupportVectorMachine/SupportVectorMachine.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private function getOSExtension(): string
private function buildTrainCommand(string $trainingSetFileName, string $modelFileName): string
{
return sprintf(
'%ssvm-train%s -s %s -t %s -c %s -n %s -d %s%s -r %s -p %s -m %s -e %s -h %d -b %d %s %s',
'%ssvm-train%s -s %s -t %s -c %s -n %F -d %s%s -r %s -p %F -m %F -e %F -h %d -b %d %s %s',
$this->binPath,
$this->getOSExtension(),
$this->type,
Expand Down
23 changes: 21 additions & 2 deletions tests/Classification/SVCTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,32 @@ public function testSaveAndRestore(): void
$classifier->train($trainSamples, $trainLabels);
$predicted = $classifier->predict($testSamples);

$filename = 'svc-test-'.random_int(100, 999).'-'.uniqid();
$filepath = tempnam(sys_get_temp_dir(), $filename);
$filepath = tempnam(sys_get_temp_dir(), uniqid('svc-test', true));
$modelManager = new ModelManager();
$modelManager->saveToFile($classifier, $filepath);

$restoredClassifier = $modelManager->restoreFromFile($filepath);
$this->assertEquals($classifier, $restoredClassifier);
$this->assertEquals($predicted, $restoredClassifier->predict($testSamples));
$this->assertEquals($predicted, $testLabels);
}

public function testWithNonDotDecimalLocale(): void
{
$currentLocale = setlocale(LC_NUMERIC, '0');
setlocale(LC_NUMERIC, 'pl_PL.utf8');

$trainSamples = [[1, 3], [1, 4], [2, 4], [3, 1], [4, 1], [4, 2]];
$trainLabels = ['a', 'a', 'a', 'b', 'b', 'b'];

$testSamples = [[3, 2], [5, 1], [4, 3]];
$testLabels = ['b', 'b', 'b'];

$classifier = new SVC(Kernel::LINEAR, $cost = 1000);
$classifier->train($trainSamples, $trainLabels);

$this->assertEquals($classifier->predict($testSamples), $testLabels);

setlocale(LC_NUMERIC, $currentLocale);
}
}
16 changes: 8 additions & 8 deletions tests/SupportVectorMachine/DataTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public function testTransformDatasetToTrainingSet(): void
$labels = ['a', 'a', 'b', 'b'];

$trainingSet =
'0 1:1 2:1 '.PHP_EOL.
'0 1:2 2:1 '.PHP_EOL.
'1 1:3 2:2 '.PHP_EOL.
'1 1:4 2:5 '.PHP_EOL
'0 1:1.000000 2:1.000000 '.PHP_EOL.
'0 1:2.000000 2:1.000000 '.PHP_EOL.
'1 1:3.000000 2:2.000000 '.PHP_EOL.
'1 1:4.000000 2:5.000000 '.PHP_EOL
;

$this->assertEquals($trainingSet, DataTransformer::trainingSet($samples, $labels));
Expand All @@ -30,10 +30,10 @@ public function testTransformSamplesToTestSet(): void
$samples = [[1, 1], [2, 1], [3, 2], [4, 5]];

$testSet =
'0 1:1 2:1 '.PHP_EOL.
'0 1:2 2:1 '.PHP_EOL.
'0 1:3 2:2 '.PHP_EOL.
'0 1:4 2:5 '.PHP_EOL
'0 1:1.000000 2:1.000000 '.PHP_EOL.
'0 1:2.000000 2:1.000000 '.PHP_EOL.
'0 1:3.000000 2:2.000000 '.PHP_EOL.
'0 1:4.000000 2:5.000000 '.PHP_EOL
;

$this->assertEquals($testSet, DataTransformer::testSet($samples));
Expand Down

0 comments on commit 8fdb3d1

Please sign in to comment.