Function for Hinge Loss for Multiple Points | Linear Algebra using Python

Linear Algebra using Python | Function for Hinge Loss for Multiple Points: Here, we are going to learn about the Function for hinge loss for multiple points and its implementation in Python.
Submitted by Anuj Singh, on June 09, 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.

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 multiple point (having one feature) with given 𝜭.

Python code for hinge loss for multiple points

# Linear Algebra Learning Sequence
# Hinge loss for Multiple Point 

import numpy as np


def hinge_loss_single(feature_vector, label, theta, theta_0):
    ydash = label*(np.matmul(theta,feature_vector) + theta_0)
    hinge = np.max([0.0, 1 - ydash*label])
    return hinge

def hinge_loss_full(feature_matrix, labels, theta, theta_0):
    tothinge = 0
    num = len(feature_matrix)
    for i in range(num):
        tothinge = tothinge + hinge_loss_single(feature_matrix[i], labels[i], theta, theta_0)
        
    hinge = tothinge
    
    return hinge

feature_matrix = np.array([[2,2], [3,3], [7,0], [14,47]])
theta = np.array([0.002,0.6])
theta_0 = 0
labels = np.array([[1], [-1], [1], [-1]])

hingell = hinge_loss_full(feature_matrix, labels, theta, theta_0)

print('Data point: ', feature_matrix)
print('\n\nCorresponding Labels: ', labels)
print('\n\n Hingle Loss for given data :', hingell)

Output:

Data point:  [[ 2  2]
 [ 3  3]
 [ 7  0]
 [14 47]]


Corresponding Labels:  [[ 1]
 [-1]
 [ 1]
 [-1]]


 Hingle Loss for given data : [0.986]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.