Skip to content

Commit e5aaf4b

Browse files
authored
Create train.py
1 parent f6f3576 commit e5aaf4b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

cli/train.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
# Licensed under the MIT license.
3+
4+
from sklearn.datasets import load_diabetes
5+
from sklearn.linear_model import Ridge
6+
from sklearn.metrics import mean_squared_error
7+
from sklearn.model_selection import train_test_split
8+
from azureml.core.run import Run
9+
from sklearn.externals import joblib
10+
import os
11+
import numpy as np
12+
13+
os.makedirs('./outputs', exist_ok=True)
14+
15+
X, y = load_diabetes(return_X_y=True)
16+
17+
run = Run.get_submitted_run()
18+
19+
X_train, X_test, y_train, y_test = train_test_split(X, y,
20+
test_size=0.2,
21+
random_state=0)
22+
data = {"train": {"X": X_train, "y": y_train},
23+
"test": {"X": X_test, "y": y_test}}
24+
25+
# list of numbers from 0.0 to 1.0 with a 0.05 interval
26+
alphas = np.arange(0.0, 1.0, 0.05)
27+
28+
for alpha in alphas:
29+
# Use Ridge algorithm to create a regression model
30+
reg = Ridge(alpha=alpha)
31+
reg.fit(data["train"]["X"], data["train"]["y"])
32+
33+
preds = reg.predict(data["test"]["X"])
34+
mse = mean_squared_error(preds, data["test"]["y"])
35+
run.log('alpha', alpha)
36+
run.log('mse', mse)
37+
38+
model_file_name = 'ridge_{0:.2f}.pkl'.format(alpha)
39+
# save model in the outputs folder so it automatically get uploaded
40+
with open(model_file_name, "wb") as file:
41+
joblib.dump(value=reg, filename=os.path.join('./outputs/',
42+
model_file_name))
43+
44+
print('alpha is {0:.2f}, and mse is {1:0.2f}'.format(alpha, mse))

0 commit comments

Comments
 (0)