Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions keras/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ def matthews_correlation(y_true, y_pred):
tp = K.sum(y_pos * y_pred_pos)
tn = K.sum(y_neg * y_pred_neg)

fp = K.sum(1 - y_neg * y_pred_pos)
fn = K.sum(1 - y_pos * y_pred_neg)
fp = K.sum(y_neg * y_pred_pos)
fn = K.sum(y_pos * y_pred_neg)

numerator = (tp * tn - fp * fn)
denominator = K.sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn))

Expand Down
12 changes: 12 additions & 0 deletions tests/keras/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ def test_metrics():
assert K.eval(output).shape == ()


def test_matthews_correlation():
y_true = K.variable(np.array([0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0]))
y_pred = K.variable(np.array([1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0]))

# Calculated using sklearn.metrics.matthews_corrcoef
actual = -0.14907119849998601

calc = K.eval(metrics.matthews_correlation(y_true, y_pred))
epsilon = 1e-05
assert actual - epsilon <= calc <= actual + epsilon


def test_sparse_metrics():
for metric in all_sparse_metrics:
y_a = K.variable(np.random.randint(0, 7, (6,)), dtype=K.floatx())
Expand Down