def gen_test_report(clf, y_test, X_test, args, sub_str='_test_report_per_cls'): #Writing test report to file y_true, y_pred = y_test, clf.predict(X_test) #ipdb.set_trace() #Producing pandas ML confusion matrix and statistical summary tmp_confusion_matrix = ConfusionMatrix(y_true, y_pred) #tmp_stat_summary=tmp_confusion_matrix.stats() tmp_confusion_matrix = tmp_confusion_matrix.to_dataframe() tmp_confusion_matrix.to_csv( os.path.join(args.output_model_dir, args.f_nm_str + '_confusion_matrix')) #Generation dictionary for analysis #with open(os.path.join(args.output_model_dir,args.f_nm_str+'_descriptive_stat.pickle')) as fb: # pickle.dump(tmp_stat_summary,fb) file_nm_test_report = args.f_nm_str + sub_str #Generating report on test data for analysis test_report_raw = classification_report(y_true, y_pred, output_dict=True) test_report_df = pd.DataFrame(test_report_raw).transpose() #Writing best model to file directory for models print(test_report_raw) test_report_df.to_csv( os.path.join(args.output_model_dir, file_nm_test_report))
for idx, row in df.iterrows(): img = image.load_img(row['img_path'], target_size=(size, size)) img = image.img_to_array(img) img = preprocess_input(np.expand_dims(img, axis=0)) pred = m.predict(img) res = classes[np.argmax(pred)] pred = pred[0].tolist() df.loc[idx, 'predictions'] = str([round(i, 3) for i in pred]) df.loc[idx, 'pred'] = res df.to_csv(op_csv, index=False) # Metrics print(accuracy_score(df['labels'].tolist(), df['pred'].tolist())) print( classification_report(df['labels'].tolist(), df['pred'].tolist(), target_names=classes)) cm = ConfusionMatrix(df['labels'].tolist(), df['pred'].tolist()).to_dataframe() # Save metrics cm.to_csv(os.path.join(os.path.dirname(op_csv), 'confusion_matrix.csv'), index=False) fig = plt.figure() plt.matshow(cm) plt.title('Confusion Matrix') plt.colorbar() plt.ylabel('True Label') plt.xlabel('Predicted Label') plt.savefig(os.path.join(os.path.dirname(op_csv), 'confusion_matrix.jpg'))