mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-22 04:55:10 +00:00
Add removeColumns function to ArrayDataset (#249)
* Add removeColumns function to ArrayDataset * Add removeColumns to docs * Fix cs
This commit is contained in:
parent
cbd9f5fde1
commit
8976047cbc
@ -8,6 +8,8 @@ Helper class that holds data as PHP `array` type. Implements the `Dataset` inter
|
|||||||
* $labels - (array) of labels
|
* $labels - (array) of labels
|
||||||
|
|
||||||
```
|
```
|
||||||
|
use Phpml\Dataset\ArrayDataset;
|
||||||
|
|
||||||
$dataset = new ArrayDataset([[1, 1], [2, 1], [3, 2], [4, 1]], ['a', 'a', 'b', 'b']);
|
$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->getSamples();
|
||||||
$dataset->getTargets();
|
$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]]
|
||||||
|
```
|
||||||
|
@ -40,4 +40,23 @@ class ArrayDataset implements Dataset
|
|||||||
{
|
{
|
||||||
return $this->targets;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,4 +26,15 @@ class ArrayDatasetTest extends TestCase
|
|||||||
$this->assertEquals($samples, $dataset->getSamples());
|
$this->assertEquals($samples, $dataset->getSamples());
|
||||||
$this->assertEquals($labels, $dataset->getTargets());
|
$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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user