forked from neptune-ai/neptune-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Organize-ML-experiments.py
53 lines (36 loc) · 1.57 KB
/
Organize-ML-experiments.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
# Organize ML experiments
# Step 1: Create a basic training script
from sklearn.datasets import load_wine
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score
from joblib import dump
data = load_wine()
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target,
test_size=0.4, random_state=1234)
params = {'n_estimators': 10,
'max_depth': 3,
'min_samples_leaf': 1,
'min_samples_split': 2,
'max_features': 3,
}
clf = RandomForestClassifier(**params)
clf.fit(X_train, y_train)
y_train_pred = clf.predict_proba(X_train)
y_test_pred = clf.predict_proba(X_test)
train_f1 = f1_score(y_train, y_train_pred.argmax(axis=1), average='macro')
test_f1 = f1_score(y_test, y_test_pred.argmax(axis=1), average='macro')
print(f'Train f1:{train_f1} | Test f1:{test_f1}')
# Step 2: Initialize Neptune
import neptune
neptune.init(project_qualified_name='shared/onboarding', # change this to your `workspace_name/project_name`
api_token='ANONYMOUS', # change this to your api token
)
# Step 3: Create an experiment and save parameters
neptune.create_experiment(name='great-idea', params=params)
# Step 4. Add tags to organize things
neptune.append_tag(['experiment-organization', 'me'])
# Step 5. Add logging of train and evaluation metrics
neptune.log_metric('train_f1', train_f1)
neptune.log_metric('test_f1', test_f1)
# Step 6. Run a few experiments with different parameters