Skip to content

Commit

Permalink
alors peut être ????
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathu-lmn committed Dec 11, 2024
1 parent 9e42b3c commit 8f57eca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
16 changes: 10 additions & 6 deletions predictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, f1_score
from sklearn.metrics import classification_report, f1_score
# from sklearn.model_selection import StratifiedKFold
except ImportError:
print("You need to install scikit-learn")
Expand Down Expand Up @@ -102,11 +102,14 @@
model = models[name]
param_dist = param_distributions[name]

def scoringfunc(estimator, X, y):
return f1_score(y, estimator.predict(X))

randomized_search = RandomizedSearchCV(
estimator=model,
param_distributions=param_dist,
n_iter=NUM_TRIALS,
scoring='f1',
scoring=scoringfunc,
cv=3,
random_state=42,
n_jobs=-1,
Expand All @@ -119,18 +122,19 @@
model.set_params(**best)
model.fit(X_train, y_train.values.ravel())

train_accuracy = accuracy_score(y_train, model.predict(X_train))
if DEBUG:
print(classification_report(y_test, model.predict(X_test)))

f1_test = f1_score(y_test, model.predict(X_test))

signature = infer_signature(X_train, model.predict(X_train))

with mlflow.start_run(run_name=name) as run:
mlflow.log_params(best)
mlflow.sklearn.log_model(model, "model", signature=signature)
model_uri = mlflow.get_artifact_uri("model")


mlflow.evaluate(
model=model_uri,
model="runs:/" + run.info.run_id + "/model",
data=evalData,
targets='label',
model_type='classifier',
Expand Down
21 changes: 19 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import random
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report, f1_score

mlflow.set_tracking_uri("https://mlflow.docsystem.xyz")
model = mlflow.pyfunc.load_model(model_uri="models:/attackdetection/latest")
# load each models in the RandomSearch experiment
model = mlflow.sklearn.load_model("runs:/12498379aa574e01b6da4cff313479dd/model")

LABELS_NUM = ["BENIGN", "SUS"]

Expand All @@ -27,6 +29,21 @@
X_train = pd.DataFrame(X_train, columns=features.columns)
X_test = pd.DataFrame(X_test, columns=features.columns)

X_train = X_train.reset_index(drop=True)
y_train = y_train.reset_index(drop=True)
X_test = X_test.reset_index(drop=True)
y_test = y_test.reset_index(drop=True)


# calculate the model's metrics
def calculate_metrics():
y_pred = model.predict(X_test)
print(classification_report(y_test, y_pred))

print(f1_score(y_test, y_pred))

calculate_metrics()

good_predictions, bad_predictions = 0, 0

def predict_row(row: int):
Expand All @@ -41,7 +58,7 @@ def predict_row(row: int):

return LABELS_NUM[y_test.iloc[row].values[0]] == prediction

while True:
while False:
for i in tqdm(range(10000)):
j = random.randint(0, len(X_test) - 1)
is_good_prediction = predict_row(j)
Expand Down

0 comments on commit 8f57eca

Please sign in to comment.