Python model assessment

Assume that y is the actual value in array form, and there is a trained model, with dependent variables X in array form.

Confusion matrix #

from sklearn.metrics import confusion_matrix

confusion_matrix(y, model.predict(X))

If binary, this returns a 2x2 array, where top left is true negative, bottom left is false negative, top right is false positive, bottom right is true positive.

Generate a more detailed report with:

from sklearn.metrics import classification_report

print(classification_report(y, model.predict(X)))

Visualize with this snippet:

cm = confusion_matrix(y, model.predict(x))

fig, ax = plt.subplots(figsize=(8, 8))
ax.imshow(cm)
ax.grid(False)
ax.xaxis.set(ticks=(0, 1), ticklabels=('Predicted 0s', 'Predicted 1s'))
ax.yaxis.set(ticks=(0, 1), ticklabels=('Actual 0s', 'Actual 1s'))
ax.set_ylim(1.5, -0.5)
for i in range(2):
    for j in range(2):
        ax.text(j, i, cm[i, j], ha='center', va='center', color='red')
plt.show()

Source

Resources #