-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Adds hinge loss function algorithm #10628
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a33bc05
Adds exponential moving average algorithm
PoojanSmart acdc0a2
code clean up
PoojanSmart 1ce781f
spell correction
PoojanSmart 7b09aeb
Modifies I/O types of function
PoojanSmart 040379f
Replaces generator function
PoojanSmart 6edf6e5
Resolved mypy type error
PoojanSmart 68c4bb8
readibility of code and documentation
PoojanSmart ce8ecce
Update exponential_moving_average.py
cclauss ee88f0e
Merge branch 'TheAlgorithms:master' into master
PoojanSmart 4d46080
Merge branch 'TheAlgorithms:master' into master
PoojanSmart 1ea82fa
Adds hinge loss function
PoojanSmart c24a3da
suggested doc and refactoring changes
PoojanSmart 0fb7274
refactoring
PoojanSmart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
Hinge Loss | ||
|
||
Description: | ||
Compute the Hinge loss used for training SVM (Support Vector Machine). | ||
|
||
Formula: | ||
loss = max(0, 1 - true * pred) | ||
|
||
Reference: https://en.wikipedia.org/wiki/Hinge_loss | ||
|
||
Author: Poojan Smart | ||
Email: [email protected] | ||
""" | ||
|
||
import numpy as np | ||
|
||
|
||
def hinge_loss(y_true: np.ndarray, pred: np.ndarray) -> float: | ||
""" | ||
Calculate the hinge loss for y_true and pred for binary classification. | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Args: | ||
y_true: Array of actual values (ground truth) encoded as -1 and 1. | ||
tianyizheng02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pred: Array of predicted values. | ||
|
||
Returns: | ||
Hinge loss | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Examples: | ||
>>> y_true = np.array([-1, 1, 1, -1, 1]) | ||
>>> pred = np.array([-4, -0.3, 0.7, 5, 10]) | ||
>>> hinge_loss(y_true, pred) | ||
1.52 | ||
>>> y_true = np.array([-1, 1, 1, -1, 1, 1]) | ||
>>> pred = np.array([-4, -0.3, 0.7, 5, 10]) | ||
>>> hinge_loss(y_true, pred) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Length of predicted and actual array must be same. | ||
""" | ||
|
||
if len(y_true) != len(pred): | ||
raise ValueError("Length of predicted and actual array must be same.") | ||
|
||
intermidiate_result = 1.0 - (y_true * pred) | ||
intermidiate_result[intermidiate_result < 0] = 0 | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
loss = np.mean(intermidiate_result) | ||
return loss | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
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.
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.
Please rename for consistency