Skip to content

Commit 715b0e7

Browse files
authored
Correct SVM Use (#346)
* Correct SVM Use * Update OneClassSVM.php * Update OneClassSVM.php
1 parent 85d7d4b commit 715b0e7

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/AnomalyDetectors/OneClassSVM.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function __construct(
7979
new ExtensionIsLoaded('svm'),
8080
new ExtensionMinimumVersion('svm', '0.2.0'),
8181
])->check();
82+
8283

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

185-
$this->model = $this->svm->train($dataset->samples());
186+
$data = [];
187+
188+
foreach ($dataset->samples() as $sample) {
189+
array_unshift($sample, 1);
190+
$data[] = $sample;
191+
}
192+
193+
$this->model = $this->svm->train($data);
186194
}
187195

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

214-
return $this->model->predict($sample) !== 1.0 ? 0 : 1;
222+
$sampleWithOffset = [];
223+
224+
foreach ($sample as $key => $value) {
225+
$sampleWithOffset[$key + 1] = $value;
226+
}
227+
228+
return $this->model->predict($sampleWithOffset) == 1 ? 0 : 1;
215229
}
216230

217231
/**

0 commit comments

Comments
 (0)