def evaluateInHouse(self, networkType): for appliance in self.applianceTypes: print('Adding results for {}'.format(appliance).ljust(40, '.')) for house in self.houses: try: network = self.getNetworkWithWeights( networkType, appliance, house) load_str = 'Loading test data for House ' + str(house) print(load_str.ljust(40, '.')) data = Dataset() data.load_house_dataframe(house) data.add_windows(house, '00 mains') mainsWindows = self.getMainsWindows(data, house, outOfHouse=False) print('Predicting'.ljust(40, '.')) predRaw = network.model.predict(mainsWindows) print('Aligning by timestep'.ljust(40, '.')) predTimestep = data.recover_reverse_diagonals(predRaw) print('Taking median'.ljust(40, '.')) predMedian = np.median(predTimestep, axis=1) print('Rescaling'.ljust(40, '.')) data.add_statistics(house) applianceLabel = self.dfLabels.loc[house, appliance] mean = data.means[house][applianceLabel] std = data.stddevs[house][applianceLabel] predMedianScaled = predMedian * std + mean print('Calculating relative MAE'.ljust(40, '.')) data.add_windows(house, applianceLabel) m = data.dataframes[house].shape[0] testStartIdx = int(-0.2 * m) true = data.dataframes[house][applianceLabel] true = true[testStartIdx:-511].as_matrix() mae = np.abs(true - predMedianScaled) relative_mae = mae.sum() / true.sum() print('Appending results'.ljust(40, '.')) row_dict = { 'network': networkType, 'appliance': appliance, 'train_house': house, 'predict_house': house, 'relative_mae': relative_mae } except Exception: traceback.print_exc() row_dict = { 'network': networkType, 'appliance': appliance, 'train_house': house, 'predict_house': house, 'relative_mae': None } print('Error on House {} {}'.format(house, appliance).ljust( 40, '.')) self.results = self.results.append(row_dict, ignore_index=True)
discova = Autoencoder() # Turn off variance for prediction discova.construct_model() discova.compile_model() path = 'weights/ae_0' + str(train_house) + '_washerdryer.hdf5' discova.model.load_weights(path) print(('Loading weights for House ' + str(train_house)).ljust(40,'.')) for test_house in range(1,7): load_str = 'Loading test data for House ' + str(test_house) print(load_str.ljust(40,'.')) applianceLabel = applianceLabels[test_house - 1] data = Dataset() data.load_house_dataframe(test_house) data.add_windows(test_house, '00 mains') data.add_windows(test_house, applianceLabel) data.add_statistics(test_house) print('Predicting'.ljust(40,'.')) predRaw = discova.model.predict(data.windows[test_house]['00 mains']) print('Aligning by timestep'.ljust(40,'.')) predTimestep = data.recover_reverse_diagonals(predRaw) print('Taking median'.ljust(40,'.')) predMedian = np.median(predTimestep, axis = 1) print('Rescaling'.ljust(40,'.')) mean = data.means[test_house][applianceLabel] std = data.stddevs[test_house][applianceLabel] predMedianScaled = predMedian * std + mean print('Calculating relative MAE'.ljust(40,'.')) true = data.dataframes[test_house][applianceLabel][511:-511].as_matrix() mae = np.abs(true - predMedianScaled) relative_mae = mae.sum() / true.sum() results.loc[train_house, test_house] = relative_mae