# -*- coding: utf-8 -*- __author__ = 106360 import numpy import pandas as pd import seaborn as sns from sklearn.metrics import mean_squared_error import scipy import matplotlib.pyplot as plt def train_model(trainX, trainY, testX, testY, model, epochs=200, batch_size=16,show_plot=True): history = model.fit(trainX, trainY, validation_data=(testX, testY), epochs=epochs, batch_size=batch_size) if show_plot: acc = history.history['mean_absolute_percentage_error'] val_acc = history.history['val_mean_absolute_percentage_error'] loss = history.history['loss'] val_loss = history.history['val_loss'] epochs_range = range(epochs) plt.figure(figsize=(8, 8)) plt.subplot(1, 2, 1) plt.plot(epochs_range, acc, label='Training mean_absolute_percentage_error') plt.plot(epochs_range, val_acc, label='Validation mean_absolute_percentage_error') plt.legend(loc='upper right') plt.title('Training and Validation \% error') plt.subplot(1, 2, 2) plt.plot(epochs_range, loss, label='Training Loss') plt.plot(epochs_range, val_loss, label='Validation Loss') plt.legend(loc='upper right') plt.title('Training and Validation Loss') plt.show() return model def evaluate_regression_model(model,testX, testY,normalizer,show_plot=True): predicted_Y = model.predict(testX) test_Y_unnormalized = normalizer[-1].inverse_transform(testY) predicted_Y_unnormalized = normalizer[-1].inverse_transform(predicted_Y) if show_plot: plot_regression(test_Y_unnormalized,predicted_Y_unnormalized) def plot_regression(gt_array, pred_array, filename='',show_plot=True): # fig, ax = plt.subplots(1, 1) x, y = pd.Series(numpy.squeeze(gt_array), name="ground_truth_val"), pd.Series(numpy.squeeze(pred_array), name="predicted_val") g= sns.jointplot(x=x, y=y, marker='.') # sns.regplot(x=x, y=y, ax=ax, marker=None, scatter=None) # g = sns.jointplot(x, y, kind="hex", color="#5d5d60", joint_kws={'gridsize': 40, 'bins': 'log'}, xlim=(0, 1), ylim=(0, 1), stat_func=None) sns.regplot(pd.Series(numpy.arange(0, 1.001, 0.01)), pd.Series(numpy.arange(0, 1.001, 0.01)), ax=g.ax_joint, scatter=True) sns.regplot(x, y, ax=g.ax_joint, scatter=True) slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(numpy.squeeze(gt_array), numpy.squeeze(pred_array)) rms = numpy.sqrt(mean_squared_error(pred_array, gt_array)) if show_plot: figure_title = 'rms=%.2f, r_value = %.2f, p_value = %.2f, y=%.2f*x + %.2f ' % (rms, r_value, p_value, slope, intercept) g.fig.suptitle(figure_title) # fig.suptitle(figure_title) print(figure_title) plt.tight_layout() plt.show() if filename!='': g.fig.savefig(filename, dpi=600) return slope, intercept, r_value, p_value, std_err if __name__ == "__main__": pass