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.py 888 B

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
  1. # %load model.py
  2. import pandas as pd
  3. import numpy as np
  4. from sklearn.linear_model import LinearRegression
  5. from sklearn.metrics import mean_squared_error
  6. from joblib import dump
  7. import json
  8. df = pd.read_csv('cleaned_data.csv', index_col=0)
  9. train = df[:-2]
  10. valid = df[-2:]
  11. y_train = train['target']
  12. X_train = train.drop('target', axis=1)
  13. y_valid = valid['target']
  14. X_valid = valid.drop('target', axis=1)
  15. reg = LinearRegression().fit(X_train,y_train)
  16. y_pred = reg.predict(X_valid)
  17. mse = mean_squared_error(y_valid, y_pred)
  18. print(f'Mean Squared Error: {mse}')
  19. print(f'Coefficients: {reg.coef_}')
  20. # save model
  21. dump(reg, 'linear_regressor.joblib')
  22. # write metrics
  23. with open('mse.json', 'w+') as f:
  24. json.dump({'mse':mse}, f)
  25. with open('coefs.json', 'w+') as f:
  26. json.dump({'coefs':reg.coef_.tolist()}, f)
  27. with open('n_data.json', 'w+') as f:
  28. json.dump({'n_data':len(df)}, f)
Tip!

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

Comments

Loading...