-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetrics.py
More file actions
51 lines (40 loc) · 1.81 KB
/
Metrics.py
File metadata and controls
51 lines (40 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import tensorflow as tf
#
# Generated by ChatGPT
#
def specificity(y_true, y_pred):
neg_y_true = 1 - y_true
neg_y_pred = 1 - tf.round(y_pred)
true_negatives = tf.reduce_sum(tf.cast(neg_y_true * neg_y_pred, tf.float32))
possible_negatives = tf.reduce_sum(tf.cast(neg_y_true, tf.float32))
specificity = true_negatives / (possible_negatives + tf.keras.backend.epsilon())
return specificity
#
# Generated by ChatGPT
#
def f1_score(y_true, y_pred):
# Convert predictions to binary values
y_pred_binary = tf.round(y_pred)
# True Positives, False Positives, and False Negatives
tp = tf.reduce_sum(tf.cast(y_true * y_pred_binary, tf.float32))
fp = tf.reduce_sum(tf.cast((1 - y_true) * y_pred_binary, tf.float32))
fn = tf.reduce_sum(tf.cast(y_true * (1 - y_pred_binary), tf.float32))
# Calculate Precision and Recall
precision = tp / (tp + fp + tf.keras.backend.epsilon())
recall = tp / (tp + fn + tf.keras.backend.epsilon())
# Calculate F1 score
f1_score = 2 * ((precision * recall) / (precision + recall + tf.keras.backend.epsilon()))
return f1_score
def weighted_f1_score(y_true, y_pred):
# Convert predictions to binary values
y_pred_binary = tf.round(y_pred)
# True Positives, False Positives, and False Negatives
tp = tf.reduce_sum(tf.cast(y_true * y_pred_binary, tf.float32))
fp = tf.reduce_sum(tf.cast((1 - y_true) * y_pred_binary, tf.float32))
fn = tf.reduce_sum(tf.cast(y_true * (1 - y_pred_binary), tf.float32))
# Calculate Precision and Recall
precision = tp / (tp + fp + tf.keras.backend.epsilon())
recall = tp / (tp + fn + tf.keras.backend.epsilon())
# Calculate F1 score
f1_score = 1.5 * ((precision * recall) / (0.5*precision + recall + tf.keras.backend.epsilon()))
return f1_score