Skip to content

Commit

Permalink
Correct SVM Use (#346)
Browse files Browse the repository at this point in the history
* Correct SVM Use

* Update OneClassSVM.php

* Update OneClassSVM.php
  • Loading branch information
LouisAUTHIE authored Nov 2, 2024
1 parent 85d7d4b commit 715b0e7
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/AnomalyDetectors/OneClassSVM.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function __construct(
new ExtensionIsLoaded('svm'),
new ExtensionMinimumVersion('svm', '0.2.0'),
])->check();


if ($nu < 0.0 or $nu > 1.0) {
throw new InvalidArgumentException('Nu must be between'
Expand Down Expand Up @@ -182,7 +183,14 @@ public function train(Dataset $dataset) : void
new SamplesAreCompatibleWithEstimator($dataset, $this),
])->check();

$this->model = $this->svm->train($dataset->samples());
$data = [];

foreach ($dataset->samples() as $sample) {
array_unshift($sample, 1);
$data[] = $sample;
}

$this->model = $this->svm->train($data);
}

/**
Expand Down Expand Up @@ -211,7 +219,13 @@ public function predictSample(array $sample) : int
throw new RuntimeException('Estimator has not been trained.');
}

return $this->model->predict($sample) !== 1.0 ? 0 : 1;
$sampleWithOffset = [];

foreach ($sample as $key => $value) {
$sampleWithOffset[$key + 1] = $value;
}

return $this->model->predict($sampleWithOffset) == 1 ? 0 : 1;
}

/**
Expand Down

0 comments on commit 715b0e7

Please sign in to comment.