def value(self, element, window=False): """ element: element是由int构成的list,如[1,1,1,1,1] """ k = "value/@%s/%s%d" % (self.ts, element, window) res_v = get_Cache(k) if res_v is not None: return res_v data_array = self._data.values n_samples, _ = data_array.shape #这里要小心,value可能是整数 # data_array = np.array(data_array, dtype=int) ts_array = self._data["timestamp"].values dim_array = self._data[DIM_LIST].values value_array = self._data["value"].values slice_data_array = sclice_dim_value(element, ts_array, dim_array, value_array) if slice_data_array is None: slice_data_array = np.array([self.ts, 0]).reshape(-1, 2) ser = resample_array(slice_data_array, self.timestamp_list) if window: res = ser else: res = ser[-1, 1] set_Cache(k, res) return res
def predict(self, s_dimension_value, vol=False, window=3, frac=0.2): k = "predict/@%s/%s%d" % (self.ts, s_dimension_value, window) res_p = get_Cache(k) if res_p is not None: return res_p s_dimension_value = np.array(s_dimension_value, dtype=int).reshape(-1, 5) ser = self.value(s_dimension_value, window=True) # if ser.empty: if ser.size == 0: pred, stand, real = 0, 0, 0 else: ser_len = ser.shape[0] # win_size = min(window+1, ser_len) x = np.arange(ser_len).tolist() y = ser[:,1] filtered = lowess(y, x, is_sorted=True, frac=frac, it=2) pred = filtered[:, 1][-1] real = ser[-1, 1] if ser_len <= 1: stand = 0 else: # stand = ser.std() stand = np.std(filtered[:, 1]) if not vol: res_pred = pred else: # print("pred, stand", pred, stand) res_pred = (pred, stand, real) set_Cache(k, res_pred) return res_pred
def get_LEAF_contribution_vector(self): LEAF_DIMENSION_LIST = self.LEAF_DIMENSION_LIST k = "%s/contribution" % (self.ts) v_vector = get_Cache(k) if v_vector is None: v_vector = [self.contribution(i) for i in LEAF_DIMENSION_LIST] set_Cache(k, v_vector) assert len(LEAF_DIMENSION_LIST) == len(v_vector) return v_vector