From c3897a33a563f7d88d40d1fb79cf9e5d8408621d Mon Sep 17 00:00:00 2001 From: Louis AUTHIE <65625876+LouisAUTHIE@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:08:37 +0100 Subject: [PATCH 1/3] Correct SVM Use --- src/AnomalyDetectors/OneClassSVM.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/AnomalyDetectors/OneClassSVM.php b/src/AnomalyDetectors/OneClassSVM.php index a7ee88df2..0bea5d6f5 100644 --- a/src/AnomalyDetectors/OneClassSVM.php +++ b/src/AnomalyDetectors/OneClassSVM.php @@ -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' @@ -182,7 +183,13 @@ public function train(Dataset $dataset) : void new SamplesAreCompatibleWithEstimator($dataset, $this), ])->check(); - $this->model = $this->svm->train($dataset->samples()); + $data = []; + + foreach ($dataset->samples() as $i => $sample) { + $data[] = array_merge([1], $sample); + } + + $this->model = $this->svm->train($data); } /** @@ -211,7 +218,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; } /** From 011b52f41296ad5c64797bc0dc419dfd75acd58e Mon Sep 17 00:00:00 2001 From: Louis AUTHIE <65625876+LouisAUTHIE@users.noreply.github.com> Date: Wed, 30 Oct 2024 21:12:48 +0100 Subject: [PATCH 2/3] Update OneClassSVM.php --- src/AnomalyDetectors/OneClassSVM.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/AnomalyDetectors/OneClassSVM.php b/src/AnomalyDetectors/OneClassSVM.php index 0bea5d6f5..2cd846e82 100644 --- a/src/AnomalyDetectors/OneClassSVM.php +++ b/src/AnomalyDetectors/OneClassSVM.php @@ -185,8 +185,10 @@ public function train(Dataset $dataset) : void $data = []; - foreach ($dataset->samples() as $i => $sample) { - $data[] = array_merge([1], $sample); + foreach ($dataset->samples() as $sample) { + $sample_array = $sample; + array_unshift($sample_array, 1); + $data[] = $sample_array; } $this->model = $this->svm->train($data); From 851df846418fafe40989804f11a52b184c6f457c Mon Sep 17 00:00:00 2001 From: Louis AUTHIE <65625876+LouisAUTHIE@users.noreply.github.com> Date: Sat, 2 Nov 2024 00:19:54 +0100 Subject: [PATCH 3/3] Update OneClassSVM.php --- src/AnomalyDetectors/OneClassSVM.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/AnomalyDetectors/OneClassSVM.php b/src/AnomalyDetectors/OneClassSVM.php index 2cd846e82..93e0a0369 100644 --- a/src/AnomalyDetectors/OneClassSVM.php +++ b/src/AnomalyDetectors/OneClassSVM.php @@ -186,9 +186,8 @@ public function train(Dataset $dataset) : void $data = []; foreach ($dataset->samples() as $sample) { - $sample_array = $sample; - array_unshift($sample_array, 1); - $data[] = $sample_array; + array_unshift($sample, 1); + $data[] = $sample; } $this->model = $this->svm->train($data);