Skip to content

Instantly share code, notes, and snippets.

@nsorros
Created February 14, 2022 10:45
Show Gist options
  • Save nsorros/a5481b4a702074616f66a6a789da118f to your computer and use it in GitHub Desktop.
Save nsorros/a5481b4a702074616f66a6a789da118f to your computer and use it in GitHub Desktop.
def confusion_matrix(y_test, y_pred):
tp = y_test.dot(y_pred)
fp = y_pred.sum() - tp
fn = y_test.sum() - tp
tn = y_test.shape[0] - tp - fp - fn
return np.array([[tn, fp], [fn, tp]])
def f(Y_pred_proba, Y_test, mlcm, k, thresholds):
y_pred_proba = np.array(Y_pred_proba[:,k].todense()).ravel()
y_test = np.array(Y_test[:,k].todense()).ravel()
threshold = thresholds[k]
y_pred = y_pred_proba > threshold
cmk = confusion_matrix(y_test, y_pred)
mlcm[k,:,:] = cmk
cm = mlcm.sum(axis=0)
tn, fp, fn, tp = cm.ravel()
f1 = tp / ( tp+ (fp+fn) / 2)
return f1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment