-
-
Notifications
You must be signed in to change notification settings - Fork 354
Add perceptron classifier to machine learning section with documentation #294
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 1 commit
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||
| # Perceptron | ||||||
|
|
||||||
| A simple linear classifier using the perceptron learning rule. This implementation supports binary and multiclass classification using one-vs-rest updates. | ||||||
|
||||||
| A simple linear classifier using the perceptron learning rule. This implementation supports binary and multiclass classification using one-vs-rest updates. | |
| A simple linear classifier using the perceptron learning rule. This implementation supports binary classification and multiclass classification with direct multiclass perceptron updates. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,167 @@ | ||||||||||||||||||||||||||||||||||||||||
| # perceptron.r | ||||||||||||||||||||||||||||||||||||||||
| # Perceptron classifier implementation in R | ||||||||||||||||||||||||||||||||||||||||
| # A simple linear classifier using the perceptron learning rule. | ||||||||||||||||||||||||||||||||||||||||
| # Supports binary and multiclass classification via one-vs-rest updates. | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
| # Supports binary and multiclass classification via one-vs-rest updates. | |
| # Supports binary classification and multiclass classification | |
| # using a standard multiclass perceptron with class score comparisons. |
Copilot
AI
Apr 10, 2026
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.
In binary mode, the model both adds an intercept column to X (when fit_intercept=TRUE) and also keeps a separate bias term that is added/updated during training/prediction. This effectively introduces two intercepts and will shift the decision boundary unexpectedly. Consider using either (a) an intercept weight via the added column with no separate bias, or (b) a separate bias with no added intercept column, and keep the approach consistent across fit/predict.
Copilot
AI
Apr 10, 2026
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.
For multiclass training/prediction, self$bias is initialized but never used in score computation or updates (scores are computed only from weights %% x_i / X_new %% t(weights)). Either incorporate bias into the multiclass score/update, or remove the unused bias field to avoid confusion.
| self$bias <- rep(0, length(self$classes)) | |
| self$bias <- NULL |
Copilot
AI
Apr 10, 2026
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.
self$classes is always stored as a vector (levels(y) returns a character vector, and encode_labels() overwrites it with character/numeric), so checking is.factor(self$classes) will never be TRUE. This branch is effectively dead code; consider removing it and simplifying label handling.
| if (is.factor(self$classes)) { | |
| labels <- c(self$classes[1], self$classes[2]) | |
| } else { | |
| labels <- self$classes | |
| } | |
| labels <- self$classes |
Copilot
AI
Apr 10, 2026
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.
In binary mode, encode_labels() recomputes and sorts unique(y) and overwrites self$classes, even though fit() already set self$classes (e.g., from factor levels). This can change the label ordering and therefore flip which class is treated as +1 vs -1. Consider using the existing self$classes (preserving factor level order) instead of re-sorting unique labels here.
| labels <- sort(unique(y)) | |
| if (length(labels) != 2) stop("Binary perceptron requires exactly two classes.") | |
| self$classes <- labels | |
| labels <- self$classes | |
| if (is.null(labels) || length(labels) == 0) { | |
| labels <- unique(y) | |
| if (length(labels) != 2) stop("Binary perceptron requires exactly two classes.") | |
| self$classes <- labels | |
| } else { | |
| if (is.factor(labels)) { | |
| labels <- as.character(labels) | |
| } | |
| if (length(labels) != 2) stop("Binary perceptron requires exactly two classes.") | |
| } | |
| if (any(!y %in% labels)) { | |
| stop("Binary perceptron received labels not present in self$classes.") | |
| } |
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 PR description says DIRECTORY.md is updated to add the Perceptron entry, but this diff also adds entries for several other existing ML algorithms (K-NN, K-Medoids, LSTM Time Series, Naive Bayes). If this is intentional, please mention it in the PR description; otherwise, consider limiting the DIRECTORY.md change to the Perceptron entry.