Visualisation for SVM (boudaries) - #13
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds visualization capabilities to the SVM classifier by implementing a plot_decision_boundary method that displays decision boundaries, margins, and support vectors for 2D feature data.
Key changes:
- Added numpy and matplotlib.pyplot imports for visualization
- Implemented
plot_decision_boundarymethod with 2D constraint validation - Visualization includes decision boundary, class margins, support vectors, and data points
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ) No newline at end of file | ||
| ) | ||
|
|
||
| def plot_decision_boundary(self, X, y, title="SVM Decision Boundary"): |
There was a problem hiding this comment.
The title contains a spelling error: "boudaries" should be "boundaries".
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
| print("Error: Model not trained. Call train() first.") | ||
| return | ||
|
|
||
| X = np.array(X) | ||
| y = np.array(y) | ||
|
|
||
| # Strict dimension check | ||
| if X.shape[1] != 2: | ||
| print(f"Cannot visualize SVM boundary with {X.shape[1]} dimensions.") | ||
| print(" -> Tip: Use PCA to reduce to 2D or select only 2 columns.") | ||
| return |
There was a problem hiding this comment.
Using print statements for error handling is inconsistent with Python best practices. The method should raise an appropriate exception (e.g., ValueError) instead of printing error messages and returning early. This allows calling code to handle the error appropriately and makes the API behavior more predictable.
| print("Error: Model not trained. Call train() first.") | |
| return | |
| X = np.array(X) | |
| y = np.array(y) | |
| # Strict dimension check | |
| if X.shape[1] != 2: | |
| print(f"Cannot visualize SVM boundary with {X.shape[1]} dimensions.") | |
| print(" -> Tip: Use PCA to reduce to 2D or select only 2 columns.") | |
| return | |
| raise RuntimeError("Model not trained. Call train() first.") | |
| X = np.array(X) | |
| y = np.array(y) | |
| # Strict dimension check | |
| if X.shape[1] != 2: | |
| raise ValueError( | |
| f"Cannot visualize SVM boundary with {X.shape[1]} dimensions. " | |
| "Tip: Use PCA to reduce to 2D or select only 2 columns." | |
| ) |
| print(" -> Tip: Use PCA to reduce to 2D or select only 2 columns.") | ||
| return | ||
|
|
||
| h = 0.02 # Step size |
There was a problem hiding this comment.
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.
@copilot open a new pull request to apply changes based on this feedback
| Z = self.model.decision_function(np.c_[xx.ravel(), yy.ravel()]) | ||
| Z = Z.reshape(xx.shape) | ||
|
|
||
| plt.contourf(xx, yy, Z, levels=[-100, 0, 100], alpha=0.2, colors=['#FF9999', '#9999FF']) |
There was a problem hiding this comment.
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']) |
| Visualizes the decision boundary, margins, and support vectors. | ||
|
|
||
| NOTE: This method strictly requires 2D feature data. |
There was a problem hiding this comment.
The docstring is incomplete and missing important information. It should include an Args section describing the parameters (X, y, title), a Returns section (even if it returns None, this should be explicit), and potentially a Raises section documenting when the method returns early due to errors. Additionally, it should specify what type of data X and y should be (e.g., array-like).
| 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. |
|
@Baddsu51 I've opened a new pull request, #17, to work on those changes. Once the pull request is ready, I'll request review from you. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@Baddsu51 I've opened a new pull request, #18, to work on those changes. Once the pull request is ready, I'll request review from you. |
Implémentation de la visualisation