def _daily_predict(self, series_id, consumption, weekdays, dates): is_day_off = self._get_is_day_off(weekdays, series_id, dates) is_day_off = is_day_off[-self._input_days:] org_key = ''.join([str(i) for i in is_day_off]) pred = [] for offset in range(7): weekday = weekdays[-1] date = dates[-1] for _ in range(offset + 1): weekday = _get_next_weekday(weekday) date = _get_next_date(date, offset + 1) key = org_key + str(self._is_day_off(weekday, series_id, date)) while 1: if key in self.train_data['daily'][offset]: break else: # print(key, 'not found') key = key[1:] if not len(key): msg = 'Key not found: %s\tWindow: %s\tOffset: %s' % ( org_key, 'daily', offset) raise KeyError(msg) x = consumption[-(len(key) - 1) * 24:] x = group_sum(x, 24) x = np.expand_dims(x, axis=0) weights = self.train_data['daily'][offset][key]['weights'] # print(consumption.shape, x.shape, weights.shape, key) pred.append(x.dot(weights)[0]) return np.array(pred)
def _hourly_predict(self, series_id, consumption, weekdays, dates): is_day_off = self._get_is_day_off(weekdays, series_id, dates) is_day_off = is_day_off[-self._input_days:] is_day_off.append( self._is_day_off(_get_next_weekday(weekdays[-1]), series_id, _get_next_date(dates[-1]))) key = ''.join([str(i) for i in is_day_off]) # print(key, weekdays) while 1: if key in self.train_data['hourly'][0]: break else: # print(key, 'not found') key = key[1:] if not len(key): raise KeyError('Empty key') consumption = consumption[-(len(key) - 1) * 24:] x = np.zeros((24, len(key) - 1)) for i in range(len(key) - 1): x[:, i] = consumption[i * 24:(i + 1) * 24] pred = [] for offset in range(24): weights = self.train_data['hourly'][offset][key]['weights'] pred.append(x[offset:offset + 1].dot(weights)[0]) return np.array(pred)
def _prepare_is_day_off(window, df, metadata, series_id): is_day_off = df.is_holiday.values[::24].tolist() current_date = df.timestamp.values[-1] current_weekday = df.weekday.values[-1] for _ in range(WINDOW_TO_PRED_DAYS[window]): current_date = _get_next_date(current_date) current_weekday = _get_next_weekday(current_weekday) current_day_is_off = _is_day_off(series_id, current_weekday, metadata) current_day_is_off = current_day_is_off or _is_holiday(current_date) is_day_off.append(current_day_is_off) is_day_off = np.array(is_day_off, dtype=np.float32) is_day_off[is_day_off == 0] = -1 is_day_off = np.expand_dims(is_day_off, axis=0) return is_day_off
def _prepare_future_weekday(window, df): weekday = [] current_weekday = df.weekday.values[-1] for _ in range(WINDOW_TO_PRED_DAYS[window]): current_weekday = _get_next_weekday(current_weekday) weekday.append(current_weekday) weekday = [_weekday_ohe(day) for day in weekday] weekday = np.array(weekday, dtype=np.float32) if window == 'hourly': weekday = np.repeat(weekday, 24, axis=0) elif window == 'weekly': weekday = weekday[::7] weekday = np.expand_dims(weekday, axis=0) return weekday
def _prepare_future_day_off(window, df, metadata, series_id): is_day_off = [] current_date = df.timestamp.values[-1] current_weekday = df.weekday.values[-1] for _ in range(WINDOW_TO_PRED_DAYS[window]): current_date = _get_next_date(current_date) current_weekday = _get_next_weekday(current_weekday) current_day_is_off = _is_day_off(series_id, current_weekday, metadata) current_day_is_off = current_day_is_off or _is_holiday(current_date) is_day_off.append(current_day_is_off) is_day_off = np.array(is_day_off, dtype=np.float32) if window == 'hourly': is_day_off = np.repeat(is_day_off, 24, axis=0) if window == 'weekly': is_day_off = np.reshape(is_day_off, (2, -1)) else: is_day_off = np.expand_dims(is_day_off, axis=1) is_day_off = np.expand_dims(is_day_off, axis=0) return is_day_off