Function for Hinge Loss for Single Point | Linear Algebra using Python

Linear Algebra using Python | Function for Hinge Loss for Single Point: Here, we are going to learn about the function for hinge loss for single point and its implementation in Python.
Submitted by Anuj Singh, on June 06, 2020

Hinge Loss is a loss function used in Machine Learning for training classifiers. The hinge loss is a maximum margin classification loss function and a major part of the SVM algorithm.

The hinge loss function is given by:

LossH = max(0,(1-Y*y))

Where, Y is the Label and, y = 𝜭.x

This is the general Hinge Loss function and in this tutorial, we are going to define a function for calculating the Hinge Loss for a Single point with given 𝜭. Functions provide the reproducibility and Modularity to the code and therefore we dedicated a separate tutorial for Hinge Loss for Single Point.

Python Function for Hinge Loss for Single Point

# Linear Algebra Learning Sequence
# Hinge Loss using linear algebra

import numpy as np

# Defining a function for Hingle Loss for Single Point
def hingeforsingle(feature, theta, label):
    y = np.matmul(theta/10, feature)
    hingeloss = np.max([0.0, (1 - label*y)])
    return hingeloss

# Main code
feature = np.array([2,4,4,3,6,9,7,4])
theta = np.array([3,3,3,3,-3,-3,-3,-3])

print('Given point with 8 features : ', feature)
print('Theta : ', theta)

label = 1

hingeloss = hingeforsingle(feature, theta, label)
print("\nThe hinge loss for the given point is :", hingeloss)

Output:

Given point with 8 features :  [2 4 4 3 6 9 7 4]
Theta :  [ 3  3  3  3 -3 -3 -3 -3]

The hinge loss for the given point is : 4.8999999999999995


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.