Skip to content

Commit 8fdb3d1

Browse files
authored
Make SVM non-locale aware (#288)
1 parent 4a3194f commit 8fdb3d1

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This changelog references the relevant changes done in PHP-ML library.
1515
* fix ensure DataTransformer::testSet samples array is not empty (#204)
1616
* fix optimizer initial theta randomization (#239)
1717
* fix travis build on osx (#281)
18+
* fix SVM locale (non-locale aware) (#288)
1819
* typo, tests, code styles and documentation fixes (#265, #261, #254, #253, #251, #250, #248, #245, #243)
1920

2021
* 0.6.2 (2018-02-22)

src/SupportVectorMachine/DataTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private static function sampleRow(array $sample): string
104104
{
105105
$row = [];
106106
foreach ($sample as $index => $feature) {
107-
$row[] = sprintf('%s:%s', $index + 1, $feature);
107+
$row[] = sprintf('%s:%F', $index + 1, $feature);
108108
}
109109

110110
return implode(' ', $row);

src/SupportVectorMachine/SupportVectorMachine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ private function getOSExtension(): string
269269
private function buildTrainCommand(string $trainingSetFileName, string $modelFileName): string
270270
{
271271
return sprintf(
272-
'%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',
272+
'%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',
273273
$this->binPath,
274274
$this->getOSExtension(),
275275
$this->type,

tests/Classification/SVCTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,32 @@ public function testSaveAndRestore(): void
5757
$classifier->train($trainSamples, $trainLabels);
5858
$predicted = $classifier->predict($testSamples);
5959

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

6564
$restoredClassifier = $modelManager->restoreFromFile($filepath);
6665
$this->assertEquals($classifier, $restoredClassifier);
6766
$this->assertEquals($predicted, $restoredClassifier->predict($testSamples));
67+
$this->assertEquals($predicted, $testLabels);
68+
}
69+
70+
public function testWithNonDotDecimalLocale(): void
71+
{
72+
$currentLocale = setlocale(LC_NUMERIC, '0');
73+
setlocale(LC_NUMERIC, 'pl_PL.utf8');
74+
75+
$trainSamples = [[1, 3], [1, 4], [2, 4], [3, 1], [4, 1], [4, 2]];
76+
$trainLabels = ['a', 'a', 'a', 'b', 'b', 'b'];
77+
78+
$testSamples = [[3, 2], [5, 1], [4, 3]];
79+
$testLabels = ['b', 'b', 'b'];
80+
81+
$classifier = new SVC(Kernel::LINEAR, $cost = 1000);
82+
$classifier->train($trainSamples, $trainLabels);
83+
84+
$this->assertEquals($classifier->predict($testSamples), $testLabels);
85+
86+
setlocale(LC_NUMERIC, $currentLocale);
6887
}
6988
}

tests/SupportVectorMachine/DataTransformerTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public function testTransformDatasetToTrainingSet(): void
1616
$labels = ['a', 'a', 'b', 'b'];
1717

1818
$trainingSet =
19-
'0 1:1 2:1 '.PHP_EOL.
20-
'0 1:2 2:1 '.PHP_EOL.
21-
'1 1:3 2:2 '.PHP_EOL.
22-
'1 1:4 2:5 '.PHP_EOL
19+
'0 1:1.000000 2:1.000000 '.PHP_EOL.
20+
'0 1:2.000000 2:1.000000 '.PHP_EOL.
21+
'1 1:3.000000 2:2.000000 '.PHP_EOL.
22+
'1 1:4.000000 2:5.000000 '.PHP_EOL
2323
;
2424

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

3232
$testSet =
33-
'0 1:1 2:1 '.PHP_EOL.
34-
'0 1:2 2:1 '.PHP_EOL.
35-
'0 1:3 2:2 '.PHP_EOL.
36-
'0 1:4 2:5 '.PHP_EOL
33+
'0 1:1.000000 2:1.000000 '.PHP_EOL.
34+
'0 1:2.000000 2:1.000000 '.PHP_EOL.
35+
'0 1:3.000000 2:2.000000 '.PHP_EOL.
36+
'0 1:4.000000 2:5.000000 '.PHP_EOL
3737
;
3838

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

0 commit comments

Comments
 (0)