Skip to content

Commit

Permalink
Correct SVM Use
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisAUTHIE authored Oct 29, 2024
1 parent 85d7d4b commit c3897a3
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 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,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);
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit c3897a3

Please sign in to comment.