-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
55 lines (40 loc) · 1.22 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
55
import os
from pydataset import data
import pandas as pd
from sklearn.model_selection import train_test_split
from catboost import (
Pool,
CatBoostRegressor
)
from constant import (
CATBOOST_PARAMS,
TARGET_NAME,
ARTIFACT_DIR,
TRAIN_DATA_OUTPUT_NAME,
MODEL_OUTPUT_NAME
)
def load_data() -> pd.DataFrame:
"""Load the boston housing data set and then save the data as
artifact for testing.
"""
boston_df = data('Boston')
# save the data for testing
boston_df.to_csv(os.path.join(ARTIFACT_DIR, TRAIN_DATA_OUTPUT_NAME), index=False)
return boston_df
def train(df: pd.DataFrame) -> None:
"""Train a model and then save the model as an artifact.
"""
# split the data for training
data_x = df.drop(TARGET_NAME, axis=1)
data_y = df[[TARGET_NAME]]
train_x, test_x, train_y, test_y = train_test_split(data_x, data_y)
train_pool = Pool(train_x, train_y)
test_pool = Pool(test_x, test_y)
# train the model
model = CatBoostRegressor(**CATBOOST_PARAMS)
model.fit(train_pool, eval_set=test_pool)
# save the artifacts
model.save_model(os.path.join(ARTIFACT_DIR, MODEL_OUTPUT_NAME))
if __name__ == '__main__':
df = load_data()
train(df)