-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
55 lines (38 loc) · 1.32 KB
/
train.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
import pandas as pd
import json
import os
from joblib import dump
from sklearn.model_selection import StratifiedKFold, cross_val_score
from sklearn.linear_model import LogisticRegression
# Set path to inputs
PROCESSED_DATA_DIR = os.environ["PROCESSED_DATA_DIR"]
train_data_file = 'train.csv'
train_data_path = os.path.join(PROCESSED_DATA_DIR, train_data_file)
# Read data
df = pd.read_csv(train_data_path, sep=",")
# Split data into dependent and independent variables
X_train = df.drop('income', axis=1)
y_train = df['income']
# Model
logit_model = LogisticRegression(max_iter=10000)
logit_model = logit_model.fit(X_train, y_train)
# Cross validation
cv = StratifiedKFold(n_splits=3)
val_logit = cross_val_score(logit_model, X_train, y_train, cv=cv).mean()
# Validation accuracy to JSON
train_metadata = {
'validation_acc': val_logit
}
# Set path to output (model)
MODEL_DIR = os.environ["MODEL_DIR"]
model_name = 'logit_model.joblib'
model_path = os.path.join(MODEL_DIR, model_name)
# Serialize and save model
dump(logit_model, model_path)
# Set path to output (metadata)
RESULTS_DIR = os.environ["RESULTS_DIR"]
train_results_file = 'train_metadata.json'
results_path = os.path.join(RESULTS_DIR, train_results_file)
# Serialize and save metadata
with open(results_path, 'w') as outfile:
json.dump(train_metadata, outfile)