Repository navigation
Expand file tree
/
Copy pathtrain.py
More file actions
30 lines (27 loc) · 1.55 KB
/
Copy pathtrain.py
File metadata and controls
30 lines (27 loc) · 1.55 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
import argparse
import yaml
import joblib
import pandas as pd
from sklearn.linear_model import Ridge
def train(dataset_path: str, model_path: str, model_configuration: dict[str, any]): # filname yaml
df = pd.read_csv(dataset_path)
features = ['rainfall', 'mean_temperature']
X = df[features]
Y = df['disease_cases']
Y = Y.fillna(0) # set NaNs to zero (not a good solution, just for the example to work)
# chap eval writes an empty {} model_configuration_for_run.yaml when no user_options are passed.
# Fall back to sklearn's Ridge default so the model still runs in that case
alpha = float(model_configuration.get("user_option_values", {}).get("alpha", 1.0))
model = Ridge(alpha=alpha)
model.fit(X, Y)
print("Train - coefficients: ", list(zip(features, model.coef_)))
joblib.dump(model, model_path) # can use MLflow to save info
return model
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Train a minimalist forecasting model.')
parser.add_argument('csv_fn', type=str, help='Path to the CSV file containing input data.') # order matters and required args
parser.add_argument('model_fn', type=str, help='Path to save the trained model.')
parser.add_argument('model_config', type=str, help='Path to model configuration yaml.')
args = parser.parse_args()
model_config = yaml.safe_load(open(args.model_config)) # should be a dict to train that's what chap does, discuss with GK and Knut again since they said pass yaml last time
train(args.csv_fn, args.model_fn, model_config)