-
Notifications
You must be signed in to change notification settings - Fork 0
Visualisation for SVM (boudaries) #13
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,8 @@ | ||||||||||||||||||||||||||||||||||
| from sklearn.svm import SVC | ||||||||||||||||||||||||||||||||||
| from src.models.base_classifier import BaseModel | ||||||||||||||||||||||||||||||||||
| from src.config import get_config | ||||||||||||||||||||||||||||||||||
| import numpy as np | ||||||||||||||||||||||||||||||||||
| import matplotlib.pyplot as plt | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| class SVMClassifier(BaseModel): | ||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||
|
|
@@ -30,4 +32,62 @@ def _build_model(self): | |||||||||||||||||||||||||||||||||
| probability=self.params.get('probability', default_params.get('probability', True)), | ||||||||||||||||||||||||||||||||||
| class_weight=self.params.get('class_weight', default_params.get('class_weight', None)), | ||||||||||||||||||||||||||||||||||
| random_state=self.params.get('random_state', default_params.get('random_state', 42)) | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def plot_decision_boundary(self, X, y, title="SVM Decision Boundary"): | ||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||
| Visualizes the decision boundary, margins, and support vectors. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| NOTE: This method strictly requires 2D feature data. | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+39
to
+41
|
||||||||||||||||||||||||||||||||||
| Visualizes the decision boundary, margins, and support vectors. | |
| NOTE: This method strictly requires 2D feature data. | |
| Visualizes the decision boundary, margins, and support vectors for a trained SVM model. | |
| Args: | |
| X (array-like of shape (n_samples, 2)): Feature data. Must be 2-dimensional. | |
| y (array-like of shape (n_samples,)): Target labels. | |
| title (str, optional): Title for the plot. Defaults to "SVM Decision Boundary". | |
| Returns: | |
| None | |
| Notes: | |
| - This method strictly requires 2D feature data (i.e., X must have exactly 2 features). | |
| - If the model is not trained or X is not 2D, the method prints an error message and returns early without plotting. |
Copilot
AI
Dec 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic number 0.02 for the step size should be either configurable or defined as a named constant with a comment explaining its purpose. This would improve code maintainability and allow users to adjust the visualization resolution if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot open a new pull request to apply changes based on this feedback
Copilot
AI
Dec 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic numbers -100 and 100 used as contour levels appear arbitrary and may not provide meaningful boundaries for all datasets. Consider using data-driven values (e.g., based on the min/max of the decision function values) or making these configurable parameters to ensure appropriate visualization across different data scales.
| plt.contourf(xx, yy, Z, levels=[-100, 0, 100], alpha=0.2, colors=['#FF9999', '#9999FF']) | |
| # Use data-driven contour levels for background shading | |
| z_min, z_max = Z.min(), Z.max() | |
| # Ensure 0 is between z_min and z_max for proper boundary coloring | |
| if z_min < 0 < z_max: | |
| contour_levels = [z_min, 0, z_max] | |
| else: | |
| # If 0 is outside the range, just use min and max | |
| contour_levels = [z_min, z_max] | |
| plt.contourf(xx, yy, Z, levels=contour_levels, alpha=0.2, colors=['#FF9999', '#9999FF']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The title contains a spelling error: "boudaries" should be "boundaries".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot open a new pull request to apply changes based on this feedback