Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

train_and_evaluate.py 2.5 KB

You have to be logged in to leave a comment. Sign In
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  1. import os
  2. import warnings
  3. import sys
  4. import pandas as pd
  5. import numpy as np
  6. from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
  7. from sklearn.model_selection import train_test_split
  8. from sklearn.linear_model import ElasticNet
  9. from get_data import read_params
  10. import argparse
  11. import joblib
  12. import json
  13. def eval_metrics(actual, pred):
  14. rmse = np.sqrt(mean_squared_error(actual, pred))
  15. mae = mean_absolute_error(actual, pred)
  16. r2 = r2_score(actual, pred)
  17. return rmse, mae, r2
  18. def train_and_evaluate(config_path):
  19. config = read_params(config_path)
  20. test_data_path = config["split_data"]["test_path"]
  21. train_data_path = config["split_data"]["train_path"]
  22. random_state = config["base"]["random_state"]
  23. model_dir = config["model_dir"]
  24. alpha = config["estimators"]["ElasticNet"]["params"]["alpha"]
  25. l1_ratio = config["estimators"]["ElasticNet"]["params"]["l1_ratio"]
  26. target = [config["base"]["target_col"]]
  27. train = pd.read_csv(train_data_path, sep=",")
  28. test = pd.read_csv(test_data_path, sep=",")
  29. train_y = train[target]
  30. test_y = test[target]
  31. train_x = train.drop(target, axis=1)
  32. test_x = test.drop(target, axis=1)
  33. lr = ElasticNet(
  34. alpha=alpha,
  35. l1_ratio=l1_ratio,
  36. random_state=random_state)
  37. lr.fit(train_x, train_y)
  38. predicted_qualities = lr.predict(test_x)
  39. (rmse, mae, r2) = eval_metrics(test_y, predicted_qualities)
  40. print("Elasticnet model (alpha=%f, l1_ratio=%f):" % (alpha, l1_ratio))
  41. print(" RMSE: %s" % rmse)
  42. print(" MAE: %s" % mae)
  43. print(" R2: %s" % r2)
  44. #####################################################
  45. scores_file = config["reports"]["scores"]
  46. params_file = config["reports"]["params"]
  47. with open(scores_file, "w") as f:
  48. scores = {
  49. "rmse": rmse,
  50. "mae": mae,
  51. "r2": r2
  52. }
  53. json.dump(scores, f, indent=4)
  54. with open(params_file, "w") as f:
  55. params = {
  56. "alpha": alpha,
  57. "l1_ratio": l1_ratio,
  58. }
  59. json.dump(params, f, indent=4)
  60. #####################################################
  61. os.makedirs(model_dir, exist_ok=True)
  62. model_path = os.path.join(model_dir, "model.joblib")
  63. joblib.dump(lr, model_path)
  64. if __name__ == "__main__":
  65. args = argparse.ArgumentParser()
  66. args.add_argument("--config", default="params.yaml")
  67. parsed_args = args.parse_args()
  68. train_and_evaluate(config_path=parsed_args.config)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...