def __calculate_features(self, data, feature_file_name, window=DEFAULT_WINDOW): """ Caculate time features and save as libsvm format. :param data: the time series to detect of :param feature_file_name: the file to use :param window: the length of window """ features = [] for index in data: if is_standard_time_series(index["data"], window): temp = [] temp.append( feature_service.extract_features(index["data"], window)) temp.append(index["flag"]) features.append(temp) try: ret_code, ret_data = self.__save_libsvm_format( features, feature_file_name) except Exception as ex: ret_code = CAL_FEATURE_ERR ret_data = str(ex) return ret_code, ret_data
def __calculate_features(self, data, window=180): """ Caculate time features. :param data: the time series to detect of :param window: the length of window """ features = [] for index in data: if is_standard_time_series(index["data"], window): temp = [] temp.append(feature_service.extract_features(index["data"], window)) temp.append(index["flag"]) features.append(temp) return features
def predict(self, X, window=180, model_name=DEFAULT_MODEL): """ :param X: the time series to detect of :type X: pandas.Series :param window: the length of window :param model_name: Use a xgboost model to predict a particular sample is an outlier or not. :return 1 denotes normal, 0 denotes abnormal. """ if is_standard_time_series(X, window): ts_features = [] features = [10] features.extend(feature_service.extract_features(X, window)) ts_features.append(features) res_pred = xgb.DMatrix(np.array(ts_features)) bst = xgb.Booster({'nthread': 4}) bst.load_model(model_name) xgb_ret = bst.predict(res_pred) if xgb_ret[0] < self.threshold: value = 0 else: value = 1 return [value, xgb_ret[0]] else: return [0, 0]
def predict(self, X, window=DEFAULT_WINDOW, model_name=DEFAULT_MODEL): """ Predict if a particular sample is an outlier or not. :param X: the time series to detect of :param type X: pandas.Series :param window: the length of window :param type window: int :param model_name: the model to use :param type model_name: string :return 1 denotes normal, 0 denotes abnormal """ if is_standard_time_series(X): ts_features = feature_service.extract_features(X, window) ts_features = np.array([ts_features]) load_model = pickle.load(open(model_name, "rb")) gbdt_ret = load_model.predict_proba(ts_features)[:, 1] if gbdt_ret[0] < self.threshold: value = 0 else: value = 1 return [value, gbdt_ret[0]] else: return [0, 0]