def plot_autocorrelation(self, save=False, save_name=None, inline=False): """ Plots the autocorrelation of the residuals, options to save the figure with a designated filename are available. If no filename is given, the classifier will generate one. Args: x_data : list - default is test data, can choose subset from ["train", "test", "val"] conf_level : float - confidence level in the coefficients is extracted from the regression results save : bool - defines whether the figure is saved or just shown save_name : string - filename if save is set to True """ try: if self._model == None: raise RuntimeError("No model trained. Please train a model.") fig = plt.figure(figsize=(15, 6)) residuals = self.get_data(data='Y_train') - nn_out_to_list( self._model.predict( self.get_data(data='X_train')[self._current_features])) autocorrelation_plot(residuals) x_label_setter('Lag', plt.gca()) y_label_setter('Autocorrelation', plt.gca()) if not inline: if (save_name == None and save): save_string = 'SLP_autocorrelation' + str( self.save_count) + '.png' self.save_count += 1 else: save_string = save_name if save: plt.savefig(save_string) else: plt.show() except RuntimeError as re: print(re.args[0])
def plot_autocorrelation(self, save=False, save_name=None, inline=False): """ Plots the autocorrelation of the residuals, options to save the figure with a designated filename are available. If no filename is given, the classifier will generate one. Args: x_data : list - default is test data, can choose subset from ["train", "test", "val"] conf_level : float - confidence level in the coefficients is extracted from the regression results save : bool - defines whether the figure is saved or just shown save_name : string - filename if save is set to True """ try: if self._was_regularised: raise RuntimeError( "Model was regularised, can't extract residuals.") fig = plt.figure(figsize=(15, 6)) result = self.get_result() autocorrelation_plot(result.resid) x_label_setter('Lag', plt.gca()) y_label_setter('Autocorrelation', plt.gca()) if not inline: if (save_name == None and save): save_string = 'LinReg_autocorrelation' + str( self.save_count) + '.png' self.save_count += 1 else: save_string = save_name if save: plt.savefig(save_string) else: plt.show() except RuntimeError as re: print(re.args[0])
def plot_residuals(self, save=False, save_name=None, inline=False): """ Plots the residuals against the input data to assess the distribution, options to save the figure with a designated filename are available. If no filename is given, the classifier will generate one. Args: x_data : list - default is test data, can choose subset from ["train", "test", "val"] conf_level : float - confidence level in the coefficients is extracted from the regression results save : bool - defines whether the figure is saved or just shown save_name : string - filename if save is set to True """ try: if self._model == None: raise RuntimeError("No model trained, can't extract residuals.") fig = plt.figure(figsize=(10,10)) residuals = self.get_data(data = 'Y_train') - nn_out_to_list(self._model.predict(self.get_data(data = 'X_train')[self._current_features])) Y_train_data = self.get_data(data='Y_train') sns.regplot(Y_train_data, residuals, lowess=True, line_kws={'color': 'red', 'lw': 3, 'alpha': 0.8}) x_label_setter('y training values', plt.gca()) y_label_setter('Residuals', plt.gca()) if not inline: if (save_name == None and save): save_string = 'SLP_residuals' + str(self.save_count) + '.png' self.save_count += 1 else: save_string = save_name if save: plt.savefig(save_string) else: plt.show() except RuntimeError as re: print(re.args[0])
def plot_training(self, save=False, save_name=None, _inline=False): """ Plots the evolution of the training and validation error as a function of the number of epochs. Args: save : bool - defines whether the figure is saved or just shown save_name : string - filename if save is set to True """ try: if self.history != None: plt.figure(figsize=(15,8)) ax = plt.subplot() plt.plot(self.history['loss'], label = 'Training Loss') plt.plot(self.history['val_loss'], color='red', label = 'Validation Loss') plt.legend() x_label_setter('Epoch', plt.gca()) y_label_setter('Loss', plt.gca()) else: raise ValueError("ERROR: No model trained. Please train a model.") if not _inline: if (save_name == None and save): save_string = 'SLP_forecast' + str(self.save_count) + '.png' self.save_count += 1 else: save_string = save_name if save: plt.savefig(save_string) else: plt.show() except ValueError as ve: print(ve.args[0])
def plot_forecast(self, x_data=["test"], save=False, save_name=None, _inline=False): """ Plots the selected subset of data along with a pre selected confidence interval, options to save the figure with a designated filename are available. If no filename is given, the classifier will generate one. Args: x_data : list - default is test data, can choose subset from ["train", "test", "val"] save : bool - defines whether the figure is saved or just shown save_name : string - filename if save is set to True """ data = self.get_data() temp_X = [] temp_Y = [] for _type in x_data: temp_X.append(data['X_' + _type]) temp_Y.append(data['Y_' + _type]) X_data = pd.concat(temp_X) Y_data = pd.concat(temp_Y) try: if self._model != None: X_data = X_data[self._current_features] Y_forecast = nn_out_to_list(self._model.predict(X_data)) # Y_data['Y_forecast'] = Y_forecast # Y_forecast_df = Y_data['Y_forecast'] temp_df = pd.DataFrame({'Y_act': Y_data}) temp_df['Y_forecast'] = Y_forecast plt.figure(figsize=(15, 8)) ax = plt.subplot() plt.plot('Y_act', data=temp_df, color='red', label='Actuals') plt.plot('Y_forecast', data=temp_df, color='blue', label='Prediction') plt.legend() x_label_setter('Date', plt.gca()) y_label_setter('Data Value', plt.gca()) else: raise ValueError( "ERROR: No model trained. Please train a model.") if not _inline: if (save_name == None and save): save_string = 'SLP_forecast' + str( self.save_count) + '.png' self.save_count += 1 else: save_string = save_name if save: plt.savefig(save_string) else: plt.show() except ValueError as ve: print(ve.args[0])
def plot_forecast(self, x_data=["test"], conf_levl=0.05, save=False, save_name=None, inline=False): """ Plots the selected subset of data along with a pre selected confidence interval, options to save the figure with a designated filename are available. If no filename is given, the classifier will generate one. Args: x_data : list - default is test data, can choose subset from ["train", "test", "val"] conf_level : float - confidence level in the coefficients is extracted from the regression results save : bool - defines whether the figure is saved or just shown save_name : string - filename if save is set to True """ data = self.get_data() temp_X = [] temp_Y = [] for _type in x_data: temp_X.append(data['X_' + _type]) temp_Y.append(data['Y_' + _type]) X_data = pd.concat(temp_X) Y_data = pd.concat(temp_Y) if self.__result == None: X_data = X_data[self._current_features] Y_forecast = (X_data * self.get_latest_params()).sum(axis=1) plt.figure(figsize=(15, 8)) ax = plt.subplot() Y_data.plot(ax=ax, color='red', label='Actuals') Y_forecast.plot(ax=ax, color='blue', label='Prediction') plt.legend() x_label_setter('Date', plt.gca()) y_label_setter('Data Value', plt.gca()) else: conf_int = self.__result.conf_int(alpha=conf_levl) forecast_lo = (X_data * conf_int[0]).sum(axis=1) forecast_hi = (X_data * conf_int[1]).sum(axis=1) Y_forecast = (X_data * self.get_latest_params()).sum(axis=1) plt.figure(figsize=(15, 8)) ax = plt.subplot() Y_data.plot(ax=ax, color='red', label='Actuals') Y_forecast.plot(ax=ax, color='blue', label='Prediction') plt.legend() x_label_setter('Date', plt.gca()) y_label_setter('Data Value', plt.gca()) plt.fill_between(forecast_lo.index, forecast_lo, forecast_hi, color='gray', alpha=0.4) if not inline: if (save_name == None and save): save_string = 'LinReg_forecast' + str(self.save_count) + '.png' self.save_count += 1 else: save_string = save_name if save: plt.savefig(save_string) else: plt.show()