-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtraining.py
More file actions
70 lines (57 loc) · 1.89 KB
/
training.py
File metadata and controls
70 lines (57 loc) · 1.89 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Python script meant to train an ML model.
"""
import pandas as pd
import os
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
import logging
import json
import sys
from joblib import dump
from common_functions import preprocess_data
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
with open('config.json', 'r') as f:
"""
Load config.json and get path variables.
"""
config = json.load(f)
dataset_csv_path = os.path.join(config['output_folder_path'])
model_path = os.path.join(config['output_model_path'])
def train_model():
"""
Function for training the model.
"""
df = pd.read_csv(os.path.join(dataset_csv_path, "finaldata.csv"))
df_x, df_y, encoder = preprocess_data(df, None)
x_train, x_test, y_train, y_test = train_test_split(df_x, df_y, test_size=0.20)
# Use this logistic regression for training
model = LogisticRegression(
C=1.0,
class_weight=None,
dual=False,
fit_intercept=True,
intercept_scaling=1,
l1_ratio=None,
max_iter=100,
multi_class='ovr',
n_jobs=None,
penalty='l2',
random_state=0,
solver='liblinear',
tol=0.0001,
verbose=0,
warm_start=False
)
# Fit the logistic regression to your data
model.fit(x_train, y_train)
print(model.score(x_train, y_train))
print(model.score(x_test, y_test))
# Write the trained model to your workspace in a file called trainedmodel.pkl
dump(model, os.path.join(model_path, "trainedmodel.pkl"))
dump(encoder, os.path.join(model_path, "encoder.pkl"))
if __name__ == "__main__":
logging.info("Running Training!")
train_model()
logging.info("Artifacts output written in practicemodels/trainedmodel.pkl")
logging.info("Artifacts output written in practicemodels/encoder.pkl")