def cm(df, model): import sklearn.metrics as metrics num_label = {'ok': 1, 'nok' : 0} Y_test = df_test['class'].copy().map(num_label).astype('int') df.reset() predictions = model.predict(df, steps=len(df), verbose=0) pred_labels= np.where(predictions>0.5, 1, 0) confusion_matrix = metrics.confusion_matrix(Y_test, pred_labels) group_names = ['TN','FP','FN','TP'] group_counts = ["{0:0.0f}".format(value) for value in confusion_matrix.flatten()] group_percentages = ["{0:.2%}".format(value) for value in confusion_matrix.flatten()/np.sum(confusion_matrix)] labels = [f"{v1}\n{v2}\n{v3}" for v1, v2, v3 in zip(group_names,group_counts,group_percentages)] labels = np.asarray(labels).reshape(2,2) ax = sns.heatmap(confusion_matrix, annot=labels, fmt='', cmap='Blues') ax.set_title('Confusion Matrix \n'); ax.set_xlabel('\nPredicted Values') ax.set_ylabel('Actual Values '); ## Ticket labels - List must be in alphabetical order ax.xaxis.set_ticklabels(['NOK','OK']) ax.yaxis.set_ticklabels(['NOK','OK']) fig1 = plt.gcf() plt.show() plt.draw() fig1.savefig('cm.png', dpi=50)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter