If you have confusion matrix in the form of:
cmat = [[ 5, 7],
[25, 37]]
Following simple function can be made:
def myscores(smat):
tp = smat[0][0]
fp = smat[0][1]
fn = smat[1][0]
tn = smat[1][1]
return tp/(tp+fp), tp/(tp+fn)
Testing:
print("precision and recall:", myscores(cmat))
Output:
precision and recall: (0.4166666666666667, 0.16666666666666666)
Above function can also be extended to produce other scores, the formulae for which are mentioned on https://en.wikipedia.org/wiki/Confusion_matrix