Skip to content

Commit

Permalink
Add removeColumns function to ArrayDataset (#249)
Browse files Browse the repository at this point in the history
* Add removeColumns function to ArrayDataset

* Add removeColumns to docs

* Fix cs
  • Loading branch information
akondas authored Mar 3, 2018
1 parent cbd9f5f commit 8976047
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/machine-learning/datasets/array-dataset.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
```

Expand All @@ -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]]
```
19 changes: 19 additions & 0 deletions src/Dataset/ArrayDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,23 @@ public function getTargets(): array
{
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);
}
}
11 changes: 11 additions & 0 deletions tests/Dataset/ArrayDatasetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ public function testArrayDataset(): void
$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());
}
}

0 comments on commit 8976047

Please sign in to comment.