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

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

Comments

Loading...