-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirst_experiment.py
60 lines (43 loc) · 1.7 KB
/
first_experiment.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier
from sklearn.metrics import classification_report
import warnings
warnings.filterwarnings('ignore')
# Step 1: Create an imbalanced binary classification dataset
X, y = make_classification(n_samples=1000, n_features=10, n_informative=2, n_redundant=8,
weights=[0.9, 0.1], flip_y=0, random_state=42)
np.unique(y, return_counts=True)
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y, random_state=42)
# Define the model hyperparameters
params = {
"solver": "lbfgs",
"max_iter": 1000,
"multi_class": "auto",
"random_state": 8888,
}
# Train the model
lr = LogisticRegression(**params)
lr.fit(X_train, y_train)
# Predict on the test set
y_pred = lr.predict(X_test)
report = classification_report(y_test, y_pred)
print(report)
report_dict = classification_report(y_test, y_pred, output_dict=True)
print(report_dict)
import mlflow
mlflow.set_experiment("First Experiment")
mlflow.set_tracking_uri(uri=" http://127.0.0.1:8080")
with mlflow.start_run():
mlflow.log_params(params)
mlflow.log_metrics({
'accuracy': report_dict['accuracy'],
'recall_class_0': report_dict['0']['recall'],
'recall_class_1': report_dict['1']['recall'],
'f1_score_macro': report_dict['macro avg']['f1-score']
})
mlflow.sklearn.log_model(lr, "Logistic Regression")