forked from jorgecasas/php-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a SvmDataset class for SVM-Light (or LibSVM) format files (#237)
* Add data loader for svm format * Add tests for error cases * Set proper exception messages * Add documents * Add error checking code for invalid column format * Add missing documents
- Loading branch information
Showing
22 changed files
with
398 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# SvmDataset | ||
|
||
Helper class that loads data from SVM-Light format file. It extends the `ArrayDataset`. | ||
|
||
### Constructors Parameters | ||
|
||
* $filepath - (string) path to the file | ||
|
||
``` | ||
$dataset = new SvmDataset('dataset.svm'); | ||
``` | ||
|
||
See [ArrayDataset](array-dataset.md) for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phpml\Dataset; | ||
|
||
use Phpml\Exception\DatasetException; | ||
use Phpml\Exception\FileException; | ||
|
||
class SvmDataset extends ArrayDataset | ||
{ | ||
public function __construct(string $filePath) | ||
{ | ||
[$samples, $targets] = self::readProblem($filePath); | ||
|
||
parent::__construct($samples, $targets); | ||
} | ||
|
||
private static function readProblem(string $filePath): array | ||
{ | ||
$handle = self::openFile($filePath); | ||
|
||
$samples = []; | ||
$targets = []; | ||
$maxIndex = 0; | ||
while (($line = fgets($handle)) !== false) { | ||
[$sample, $target, $maxIndex] = self::processLine($line, $maxIndex); | ||
$samples[] = $sample; | ||
$targets[] = $target; | ||
} | ||
|
||
fclose($handle); | ||
|
||
foreach ($samples as &$sample) { | ||
$sample = array_pad($sample, $maxIndex + 1, 0); | ||
} | ||
|
||
return [$samples, $targets]; | ||
} | ||
|
||
private static function openFile(string $filePath) | ||
{ | ||
if (!file_exists($filePath)) { | ||
throw FileException::missingFile(basename($filePath)); | ||
} | ||
|
||
$handle = fopen($filePath, 'rb'); | ||
if ($handle === false) { | ||
throw FileException::cantOpenFile(basename($filePath)); | ||
} | ||
|
||
return $handle; | ||
} | ||
|
||
private static function processLine(string $line, int $maxIndex): array | ||
{ | ||
$columns = self::parseLine($line); | ||
|
||
$target = self::parseTargetColumn($columns[0]); | ||
$sample = array_fill(0, $maxIndex + 1, 0); | ||
|
||
$n = count($columns); | ||
for ($i = 1; $i < $n; ++$i) { | ||
[$index, $value] = self::parseFeatureColumn($columns[$i]); | ||
if ($index > $maxIndex) { | ||
$maxIndex = $index; | ||
$sample = array_pad($sample, $maxIndex + 1, 0); | ||
} | ||
|
||
$sample[$index] = $value; | ||
} | ||
|
||
return [$sample, $target, $maxIndex]; | ||
} | ||
|
||
private static function parseLine(string $line): array | ||
{ | ||
$line = explode('#', $line, 2)[0]; | ||
$line = rtrim($line); | ||
$line = str_replace("\t", ' ', $line); | ||
|
||
$columns = explode(' ', $line); | ||
|
||
return $columns; | ||
} | ||
|
||
private static function parseTargetColumn(string $column): float | ||
{ | ||
if (!is_numeric($column)) { | ||
throw DatasetException::invalidTarget($column); | ||
} | ||
|
||
return (float) $column; | ||
} | ||
|
||
private static function parseFeatureColumn(string $column): array | ||
{ | ||
$feature = explode(':', $column, 2); | ||
if (count($feature) != 2) { | ||
throw DatasetException::invalidValue($column); | ||
} | ||
|
||
$index = self::parseFeatureIndex($feature[0]); | ||
$value = self::parseFeatureValue($feature[1]); | ||
|
||
return [$index, $value]; | ||
} | ||
|
||
private static function parseFeatureIndex(string $index): int | ||
{ | ||
if (!is_numeric($index) || !ctype_digit($index)) { | ||
throw DatasetException::invalidIndex($index); | ||
} | ||
|
||
if ((int) $index < 1) { | ||
throw DatasetException::invalidIndex($index); | ||
} | ||
|
||
return (int) $index - 1; | ||
} | ||
|
||
private static function parseFeatureValue(string $value): float | ||
{ | ||
if (!is_numeric($value)) { | ||
throw DatasetException::invalidValue($value); | ||
} | ||
|
||
return (float) $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 1:2.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
1 1:2.3 | ||
0 1:4.56 | ||
1 1:78.9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
1 1:2 2:4 3:6 4:8 | ||
2 1:3 2:5 3:7 4:9 | ||
0 1:1.2 2:3.4 3:5.6 4:7.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
0 1:2 # This is a comment. | ||
1 1:34 # This # is # : # also # a # comment # . |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
1 1:2.3 | ||
|
||
0 1:4.56 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 0:2.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 12345 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 1:2.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 1:xyz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1:2.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 x:2.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
A 1:2.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
0 2:3.45 | ||
1 5:6.789 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 1:23 2:45 # comments |
Oops, something went wrong.