Professional Documents
Culture Documents
4th Assign
4th Assign
1|Page
Question # 01: Study the different model evaluation metrics including
1. Accuracy
2. Precision
3. Recall
4. F1 score
Accuracy
Formula: (TP + TN) / (TP + TN + FP + FN)
Description: Accuracy represents the overall correctness of the model. It is the ratio of correctly
predicted instances (true positives and true negatives) to the total number of instances.
When to Use: Accuracy is suitable for balanced datasets where the classes are relatively evenly
distributed.
Precision
Formula: TP / (TP + FP)
Description: Precision is the ratio of correctly predicted positive observations to the total
predicted positives. It focuses on the accuracy of positive predictions.
When to Use: Precision is important when the cost of false positives is high. For example, in
applications like spam detection, precision focuses on minimizing the number of legitimate
instances incorrectly classified as positive (false positives).
2|Page
Recall
Formula: TP / (TP + FN)
Description: Recall is the ratio of correctly predicted positive observations to the all
observations in actual class. It is a measure of the ability of a classification model to capture all
relevant instances.
When to Use: Recall is crucial when the cost of false negatives is high. For instance, in medical
diagnosis, recall is important to ensure that the model captures as many positive cases as
possible, even if it means tolerating some false positives.
F1 Score
Formula: 2 * (Precision * Recall) / (Precision + Recall)
Description: The F1 score is the harmonic mean of precision and recall. It provides a balance
between precision and recall. F1 score reaches its best value at 1 and worst at 0.
When to Use: The F1 score is appropriate when there is an imbalance between precision and
recall, and you want to find a balance between the two. It is particularly useful when both false
positives and false negatives need to be minimized.
3|Page
Implement the Concepts in Assignment # 03
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
from keras import backend as K
# Input values
input_data = np.array([[5, 3]])
# Target value
target = np.array([[0]])
model.layers[0].set_weights([hidden_layer_weights.T, hidden_layer_biases])
# Transpose the output layer weights to match the correct shape (4, 1)
output_layer_weights_corrected = output_layer_weights.reshape((4, 1))
model.layers[1].set_weights([output_layer_weights_corrected,
output_layer_biases])
4|Page
# Forward propagation
output = model.predict(input_data)
print("Initial Output:", output)
# Metrics Calculation
accuracy = np.mean(np.round(output) == target)
precision = np.sum(np.round(output) * target) / (np.sum(np.round(output))
+ 1e-10)
recall = np.sum(np.round(output) * target) / (np.sum(target) + 1e-10)
f1_score = 2 * (precision * recall) / (precision + recall + 1e-10)
# Backward propagation
delta_output = (output - target) * output * (1 - output)
# Transpose the hidden layer output to match the correct shape
transpose_hidden_output = K.transpose(model.layers[0].output)
model.layers[1].set_weights([output_layer_weights_corrected -
learning_rate * np.dot(transpose_hidden_output, delta_output),
output_layer_biases - learning_rate *
np.sum(delta_output, axis=0)])
5|Page
# Forward propagation after backpropagation
updated_output = model.predict(input_data)
OUTPUT
1/1 [==============================] - 0s 52ms/step
Initial Output: [[0.9548363]]
Initial Loss: 0.4558561884504986
Initial Accuracy: 0.0
Initial Precision: 0.0
Initial Recall: 0.0
Initial F1 Score: 0.0
---------------------------------------------------------------------------
6|Page