This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Neptune-Scikit-learn.py
174 lines (98 loc) · 4.3 KB
/
Neptune-Scikit-learn.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Scikit-learn + Neptune
# Before you start
## Install dependencies
# Scikit-learn regression
## Step 1: Create and fit random forest regressor
parameters = {'n_estimators': 70,
'max_depth': 7,
'min_samples_split': 3}
from sklearn.datasets import load_boston
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
rfr = RandomForestRegressor(**parameters)
X, y = load_boston(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=28743)
rfr.fit(X_train, y_train)
## Step 2: Initialize Neptune
import neptune
neptune.init('shared/sklearn-integration', api_token='ANONYMOUS')
## Step 3: Create an Experiment
neptune.create_experiment(params=parameters,
name='regression-example',
tags=['RandomForestRegressor', 'regression'])
## Step 4: Log regressor summary
from neptunecontrib.monitoring.sklearn import log_regressor_summary
log_regressor_summary(rfr, X_train, X_test, y_train, y_test)
## Step 5: Stop Neptune experiment after logging summary
neptune.stop()
## Explore results
# Scikit-learn classification
## Step 1: Create and fit gradient boosting classifier
parameters = {'n_estimators': 120,
'learning_rate': 0.12,
'min_samples_split': 3,
'min_samples_leaf': 2}
from sklearn.datasets import load_digits
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import train_test_split
gbc = GradientBoostingClassifier(**parameters)
X, y = load_digits(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=28743)
gbc.fit(X_train, y_train)
## Step 2: Initialize Neptune
import neptune
neptune.init('shared/sklearn-integration', api_token='ANONYMOUS')
## Step 3: Create an Experiment
neptune.create_experiment(params=parameters,
name='classification-example',
tags=['GradientBoostingClassifier', 'classification'])
## Step 4: Log classifier summary
from neptunecontrib.monitoring.sklearn import log_classifier_summary
log_classifier_summary(gbc, X_train, X_test, y_train, y_test)
## Step 5: Stop Neptune experiment after logging summary
neptune.stop()
## Explore Results
# Scikit-learn KMeans clustering
## Step 1: Create KMeans object and example data
parameters = {'n_init': 11,
'max_iter': 270}
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
km = KMeans(**parameters)
X, y = make_blobs(n_samples=579, n_features=17, centers=7, random_state=28743)
## Step 2: Initialize Neptune
import neptune
neptune.init('shared/sklearn-integration', api_token='ANONYMOUS')
## Step 3: Create an Experiment
neptune.create_experiment(params=parameters,
name='clustering-example',
tags=['KMeans', 'clustering'])
## Step 4: Log KMeans clustering summary
from neptunecontrib.monitoring.sklearn import log_kmeans_clustering_summary
log_kmeans_clustering_summary(km, X, n_clusters=17)
## Step 5: Stop Neptune experiment after logging summary
neptune.stop()
## Explore Results
# Other logging options
## Before you start: create and fit gradient boosting classifier
from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier()
X, y = load_digits(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=28743)
rfc.fit(X_train, y_train)
## Log estimator parameters
from neptunecontrib.monitoring.sklearn import log_estimator_params
neptune.create_experiment(name='estimator-params')
log_estimator_params(rfc) # log estimator parameters here
neptune.stop()
## Log model
from neptunecontrib.monitoring.sklearn import log_pickled_model
neptune.create_experiment(name='pickled-model')
log_pickled_model(rfc, 'my_model') # log pickled model parameters here.
# path to file in the Neptune artifacts is ``model/<my_model>``.
neptune.stop()
## Log confusion matrix
from neptunecontrib.monitoring.sklearn import log_confusion_matrix_chart
neptune.create_experiment(name='confusion-matrix-chart')
log_confusion_matrix_chart(rfc, X_train, X_test, y_train, y_test) # log confusion matrix chart
neptune.stop()