Skip to content
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

Adds hinge loss function algorithm #10628

Merged
merged 13 commits into from
Oct 18, 2023
Merged

Conversation

PoojanSmart
Copy link
Contributor

Describe your change:

  • Adding Hinge loss function which is useful loss function in Support Vector Machine (SVM) for calculating loss.
  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

import numpy as np


def hinge_loss(y_true: np.ndarray, pred: np.ndarray) -> float:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def hinge_loss(y_true: np.ndarray, pred: np.ndarray) -> float:
def hinge_loss(y_true: np.ndarray, y_pred: np.ndarray) -> float:

Please rename for consistency

machine_learning/loss_functions/hinge_loss.py Show resolved Hide resolved
machine_learning/loss_functions/hinge_loss.py Outdated Show resolved Hide resolved
machine_learning/loss_functions/hinge_loss.py Outdated Show resolved Hide resolved
machine_learning/loss_functions/hinge_loss.py Outdated Show resolved Hide resolved
machine_learning/loss_functions/hinge_loss.py Outdated Show resolved Hide resolved
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 17, 2023
@cclauss
Copy link
Member

cclauss commented Oct 17, 2023

#10637 (comment) also applies here.

@PoojanSmart
Copy link
Contributor Author

#10637 (comment) also applies here.

I can combine all loss function into single file, then you can review. We will do further modularization if required based on loss function category.
But before that, Is it fine if you merge this one?
I will raise new issue and PR to compile all loss functions.

Let me know your thoughts..

@cclauss
Copy link
Member

cclauss commented Oct 17, 2023

I will allow @tianyizheng02 to review this PR because he has already had several good suggestions. On this repo, if you raise a new issue during Hacktoberfest then there likely will be several PRs to try to solve it. Just create your own PR.

@cclauss cclauss requested a review from tianyizheng02 October 17, 2023 17:13

# Raise value error when y_true (encoded labels) have any other values
# than -1 and 1
if np.array_equal(np.sort(np.unique(y_true)), np.array([-1, 1])) is False:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if np.array_equal(np.sort(np.unique(y_true)), np.array([-1, 1])) is False:
if np.any((y_true != 1) & (y_true != -1)):

This should do the same thing (check if any entry is neither 1 or -1)

@tianyizheng02 tianyizheng02 merged commit 361f64c into TheAlgorithms:master Oct 18, 2023
2 checks passed
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
* Adds exponential moving average algorithm

* code clean up

* spell correction

* Modifies I/O types of function

* Replaces generator function

* Resolved mypy type error

* readibility of code and documentation

* Update exponential_moving_average.py

* Adds hinge loss function

* suggested doc and refactoring changes

* refactoring

---------

Co-authored-by: Christian Clauss <cclauss@me.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants