Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct SVM Use #346

Merged
merged 3 commits into from
Nov 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we don't need this $i variable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are completely right, this is now corrected !

$data[] = array_merge([1], $sample);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder which is faster ...

array_merge([1], $sample)

or

array_unshift(1, $sample)

From what I recall, unshift is linear because it needs to reindex the array or something. I think merge is also linear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change is done ;)

}

$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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice we are "inversing" the logic here i.e. 1 is now 0, 0 is now 1. Is that intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in fact in the one class mode of libsvm, the "normal" samples are to be labelled with the 1 class. And the anomalies, are to be labelled with -1. That's why !

}

/**
Expand Down
Loading