Add removeColumns function to ArrayDataset (#249)

* Add removeColumns function to ArrayDataset

* Add removeColumns to docs

* Fix cs
This commit is contained in:
Arkadiusz Kondas 2018-03-03 16:04:21 +01:00 committed by GitHub
parent cbd9f5fde1
commit 8976047cbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 0 deletions

View File

@ -8,6 +8,8 @@ Helper class that holds data as PHP `array` type. Implements the `Dataset` inter
* $labels - (array) of labels
```
use Phpml\Dataset\ArrayDataset;
$dataset = new ArrayDataset([[1, 1], [2, 1], [3, 2], [4, 1]], ['a', 'a', 'b', 'b']);
```
@ -19,3 +21,21 @@ To get samples or labels you can use getters:
$dataset->getSamples();
$dataset->getTargets();
```
### Remove columns
You can remove columns by index numbers, for example:
```
use Phpml\Dataset\ArrayDataset;
$dataset = new ArrayDataset(
[[1,2,3,4], [2,3,4,5], [3,4,5,6], [4,5,6,7]],
['a', 'a', 'b', 'b']
);
$dataset->removeColumns([0,2]);
// now from each sample column 0 and 2 are removed
// [[2,4], [3,5], [4,6], [5,7]]
```

View File

@ -40,4 +40,23 @@ class ArrayDataset implements Dataset
{
return $this->targets;
}
/**
* @param int[] $columns
*/
public function removeColumns(array $columns): void
{
foreach ($this->samples as &$sample) {
$this->removeColumnsFromSample($sample, $columns);
}
}
private function removeColumnsFromSample(array &$sample, array $columns): void
{
foreach ($columns as $index) {
unset($sample[$index]);
}
$sample = array_values($sample);
}
}

View File

@ -26,4 +26,15 @@ class ArrayDatasetTest extends TestCase
$this->assertEquals($samples, $dataset->getSamples());
$this->assertEquals($labels, $dataset->getTargets());
}
public function testRemoveColumns(): void
{
$dataset = new ArrayDataset(
[[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], [4, 5, 6, 7]],
['a', 'a', 'b', 'b']
);
$dataset->removeColumns([0, 2]);
$this->assertEquals([[2, 4], [3, 5], [4, 6], [5, 7]], $dataset->getSamples());
}
}