<jupyter_start><jupyter_text>Compute Hinge Loss (Empirical Risk) The empirical risk Rn is defined asRn(θ)=1nn∑t=1Loss(y(t)−θ⋅x(t))	 where (x(t),y(t)) is the tth training example (and there are n in total), and Loss is some loss function, such as hinge loss.Recall from a previous lecture that the definition of hinge loss:Lossh(z)={0if z≥11−z, otherwise.In this problem, we calculate the empirical risk with hinge loss when given specific θ and {(x(t),y(t))}t=1,...,n. Assume we have 4 training examples (i.e. n=4), where x(t)∈R3 and y(t) is a scalar. The training examples {(x(t),y(t))}t=1,2,3,4 are given as follows:<jupyter_code>def empirical_risk(feature_matrix, labels, theta):
    total_loss = []
    total_loss2 = []
    for i, x  in enumerate(feature_matrix):
        ##The loss before applying the Hinge loss function
        loss = labels[i] - (np.dot(feature_matrix[i], theta))
        total_loss.append(loss)
        ##Aplying the Hinge Loss function
        if (labels[i] - (np.dot(feature_matrix[i], theta))) >= 1:
            loss2=0 
        else:
            loss2 = 1- (labels[i] - (np.dot(feature_matrix[i], theta)))
        total_loss2.append(loss2)
        result= (sum(total_loss2)/len(feature_matrix))
    return total_loss, total_loss2, result
import numpy as np
feature_matrix= np.array([[1,0,1],
                          [1,1,1],
                          [1,1,-1],
                          [-1,1,1]])
labels= np.array([2,
                  2.7,
                  -0.7,
                  2])

theta= np.array([0, 1,2])

empirical_risk(feature_matrix, labels, theta)<jupyter_output><empty_output><jupyter_text>Compute Squared Error Loss1 point possible (graded)Now, we will calculate the empirical risk with the squared error loss. Remember that the squared error loss is given by  Loss(z)= Z squared/ 2<jupyter_code>def empirical_risk(feature_matrix, labels, theta):
    total_loss = []
    total_loss2 = []
    for i, x  in enumerate(feature_matrix):
        ##The loss before applying the Hinge loss function
        loss = np.square(labels[i] - (np.dot(feature_matrix[i], theta)))/2
        total_loss.append(loss)
        ##Aplying the Squared Error Function
    result= (sum(total_loss)/len(feature_matrix))
    return  total_loss, result
import numpy as np
feature_matrix= np.array([[1,0,1],
                          [1,1,1],
                          [1,1,-1],
                          [-1,1,1]])
labels= np.array([2,
                  2.7,
                  -0.7,
                  2])

theta= np.array([0, 1,2])

empirical_risk(feature_matrix, labels, theta)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12714122573277503, 'fraction_numerical': 0.02626570232204035, 'mean_word_length': 2.6248275862068966, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '25903', 'license': 'Unlicense', 'n_tokens_mistral': 894, 'n_tokens_neox': 851, 'n_words': 236, 'path': 'Machine-Learning-with-Python- From-LM-to-DL/Unit 2. Non Linear Classification, Linear Regression, Colaborative Filtering/01_Empirical_Risk.ipynb', 'repo_name': 'andresdelarosa1887/Public-Projects', 'size': '2627'}