# limitations under the License.
# ==================================================================================
-import hdbscan
-import pandas as pd
-import numpy as np
import joblib
import random
import json
-
+
+
class modelling(object):
- def __init__(self,data):
+ def __init__(self, data):
""" Separating UEID and timestamp features to be mapped later after prediction """
self.time = data.MeasTimestampRF
self.id = data.UEID
- self.data = data.drop(['UEID', 'MeasTimestampRF'], axis = 1)
-
+ self.data = data.drop(['UEID', 'MeasTimestampRF'], axis=1)
+
def predict(self, name):
- """
+ """
Load the saved model and map the predicted category into Category field.
- Map UEID, MeasTimestampRF with the predicted result.
+ Map UEID, MeasTimestampRF with the predicted result.
"""
- model = joblib.load('/tmp/ad/' + name)
+ model = joblib.load('ad/' + name)
pred = model.predict(self.data)
data = self.data.copy()
- le = joblib.load('/tmp/ad/LabelEncoder')
+ le = joblib.load('ad/LabelEncoder')
data['Category'] = le.inverse_transform(pred)
data['MeasTimestampRF'] = self.time
data['UEID'] = self.id
return data
+
def compare(df):
"""
If the category of UEID is present in the segment file, it is considered as normal(0)
otherwise, the sample is considered as anomaly.
- """
- with open("/tmp/ad/ue_seg.json", "r") as json_data:
+ """
+ with open("ad/ue_seg.json", "r") as json_data:
segment = json.loads(json_data.read())
anomaly = []
for i in df.index:
- if df.loc[i, 'Category'] in segment[str(df.loc[i,'UEID'])]:
+ if df.loc[i, 'Category'] in segment[str(df.loc[i, 'UEID'])]:
anomaly.append(0)
else:
anomaly.append(1)
return anomaly
-
+
+
def HDB_PREDICT(df):
"""
- Extract all the unique UEID
+ Extract all the unique UEID
Call Predict method to get the final data for the randomly selected UEID
"""
ue_list = df.UEID.unique() # Extract unique UEIDs
- ue = random.choice(ue_list) # Randomly selected the ue list
+ ue = random.choice(ue_list) # Randomly selected the ue list
df = df[df['UEID'] == ue]
db = modelling(df)
- db_df = db.predict('RF')# Calls predict module and store the result into db_df
+ db_df = db.predict('RF') # Calls predict module and store the result into db_df
del db
-
db_df['Anomaly'] = compare(db_df)
return db_df