feat: add Support Vector Classifier (SVC) implementation #1005
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR adds a Support Vector Classifier (SVC) implementation to the machine learning module. The SVC is a powerful binary classification algorithm that finds the optimal hyperplane to separate two classes.
Features Implemented
Algorithm Details
The implementation uses the dual formulation of the SVM optimization problem:
Maximize:
Subject to:
Where:
Solver Implementation:
Production Note: For large-scale or production use, integrate with a proper QP solver (OSQP, Clarabel) or use SMO algorithm.
Checklist
cargo fmt --allcargo clippy --all-targets --all-features -- -D warningsTesting
All tests pass successfully:
cargo test support_vector_classifierTest coverage includes:
Code Quality
cargo fmtclone_from()for efficient cloningDependencies
Added to
Cargo.toml:Note: The repository already uses
nalgebrafor linear algebra operations. This PR addsndarraywhich is specifically needed for the SVC implementation and may be useful for future machine learning algorithms.Examples
All usage examples are included in the documentation (doctests). These demonstrate:
View documentation:
Run doc tests:
cargo test --docExample Code
Example 1: Linear Kernel (Linearly Separable Data)
Example 2: RBF Kernel (Non-linearly Separable Data)
Example 3: Soft Margin Classification
Examples Output
Future Enhancements
Potential improvements for future PRs:
References
Note to Reviewers: The optimization uses a simplified gradient descent approach for educational clarity. While this works well for small datasets, a production implementation would benefit from a proper QP solver. I'm open to suggestions on whether to integrate a more robust solver in this PR or leave it for a future enhancement.