-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_module.py
More file actions
59 lines (43 loc) · 1.82 KB
/
Copy pathdata_module.py
File metadata and controls
59 lines (43 loc) · 1.82 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
import joblib
import pandas as pd
def export_train_test(train, test, Categorical):
"""Exports the training and testing dataframes to CSV files.
Args:
train (pd.DataFrame): The training dataframe.
test (pd.DataFrame): The testing dataframe.
Categorical (pd.Series): A series containing the names of categorical features.
"""
print('> Saving data...')
train.to_csv('./data/train_fe.csv', index=False)
test.to_csv('./data/test_fe.csv', index=False)
Categorical.to_csv('./data/categorical.csv')
print('> Data saved.\n')
def import_train_test():
"""Imports the training and testing dataframes from CSV files.
Returns:
tuple: A tuple containing the training dataframe, testing dataframe, and a series of categorical feature names.
"""
print('> Importing data...')
train = pd.read_csv('./data/train_fe.csv')
test = pd.read_csv('./data/test_fe.csv')
Categorical = pd.read_csv('./data/categorical.csv')['0']
# 3) Transform categorical
train[Categorical] = train[Categorical].fillna('nan').astype(str)
test[Categorical] = test[Categorical].fillna('nan').astype(str)
print('> Data imported.\n')
return train, test, Categorical
def export_model(model, name='./data/model_catboost.sav'):
"""Exports the trained model to a file using joblib.
Args:
model: The trained model to be saved.
name (str, optional): The file path to save the model to. Defaults to './data/model_catboost.sav'.
"""
joblib.dump(model, name)
def import_model(name = './data/model_catboost.sav'):
"""Imports a trained model from a file using joblib.
Args:
name (str, optional): The file path to load the model from. Defaults to './data/model_catboost.sav'.
Returns:
The loaded model.
"""
return joblib.load(name)