Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain.py
More file actions
Latest commit
47 lines (36 loc) · 1.19 KB
/
Copy pathtrain.py
File metadata and controls
47 lines (36 loc) · 1.19 KB
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
importpickle
importsys
importpandasaspd
importnumpyasnp
importos
fromsklearn.linear_modelimportLinearRegression
fromsklearn.metricsimportmean_squared_error
fromsklearnimportcross_validation
fromazureml.loggingimportget_azureml_logger
# create the outputs folder
os.makedirs('./outputs', exist_ok=True)
# Initialize the logger
run_logger=get_azureml_logger()
# load data
data=pd.read_csv('mydata.csv', delimiter=',', na_values="n/a")
print ('Dataset shape: {}'.format(data.shape))
# split
train, test=sklearn.cross_validation.train_test_split(data, train_size=0.7, random_state=123)
# load features and labels, assuming the last col is the label col.
X_train=train.iloc[:, :-1]
Y_train=train.iloc[:, -1]
X_test=test.iloc[:, :-1]
Y_test=test.iloc[:, -1]
# train the model
model=sklearn.linear_model.LinearRegression()
model.fit(X_train, Y_train)
# evaluate the model
Y_pred=model.predict(X_test)
mse=mean_squared_error(Y_test, Y_pred)
print('Mean Squared Error: {}.'.format(mse))
# log MSE
run_logger.log("Mean Squared Error", mse)
# serialize the model on disk in the special 'outputs' folder
f=open('./outputs/model.pkl', 'wb')
pickle.dump(model, f)
f.close()