Skip to content

Visualisation for SVM (boudaries) - #13

Merged
AdrienSkr merged 2 commits into
mainfrom
svm_classifier
Dec 13, 2025
Merged

Visualisation for SVM (boudaries)#13
AdrienSkr merged 2 commits into
mainfrom
svm_classifier

Conversation

@RobinBecard

Copy link
Copy Markdown
Owner

Implémentation de la visualisation

Copilot AI review requested due to automatic review settings December 13, 2025 03:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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_boundary method 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"):

Copilot AI Dec 13, 2025

Copy link

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".

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Collaborator

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

Comment thread src/models/svm_classifier.py Outdated
Comment thread src/models/svm_classifier.py Outdated
Comment on lines +44 to +54
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

Copilot AI Dec 13, 2025

Copy link

Choose a reason for hiding this comment

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

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.

Suggested change
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."
)

Copilot uses AI. Check for mistakes.
print(" -> Tip: Use PCA to reduce to 2D or select only 2 columns.")
return

h = 0.02 # Step size

Copilot AI Dec 13, 2025

Copy link

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.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Collaborator

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

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'])

Copilot AI Dec 13, 2025

Copy link

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.

Suggested change
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'])

Copilot uses AI. Check for mistakes.
Comment on lines +39 to +41
Visualizes the decision boundary, margins, and support vectors.

NOTE: This method strictly requires 2D feature data.

Copilot AI Dec 13, 2025

Copy link

Choose a reason for hiding this comment

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

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).

Suggested change
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 uses AI. Check for mistakes.

Copilot AI commented Dec 13, 2025

Copy link
Copy Markdown

@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>

Copilot AI commented Dec 13, 2025

Copy link
Copy Markdown

@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.

@AdrienSkr
AdrienSkr merged commit 466b9df into main Dec 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants