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

linear_svc.py 1.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
  1. import _pickle as cpickle
  2. import os
  3. import matplotlib.pyplot as plt
  4. import pandas as pd
  5. import plac
  6. from dagshub import dagshub_logger
  7. from sklearn.metrics import plot_confusion_matrix
  8. from sklearn.svm import LinearSVC
  9. @plac.annotations(
  10. data_path=("Path to source data", "option", "i", str),
  11. out_path=("Path to save trained Model", "option", "o", str)
  12. )
  13. def main(data_path='data/features/', out_path='data/models/svc/'):
  14. train = pd.read_csv(f'{data_path}train.csv')
  15. test = pd.read_csv(f'{data_path}test.csv')
  16. X_train, y_train = train.drop(columns=['class']), train['class']
  17. X_test, y_test = test.drop(columns=['class']), test['class']
  18. model = LinearSVC()
  19. model.fit(X_train, y_train)
  20. if not os.path.isdir(out_path):
  21. os.makedirs(out_path)
  22. with open(f'{out_path}model.pkl', 'wb+') as fp:
  23. cpickle.dump(model, fp)
  24. cmd = plot_confusion_matrix(model, X_test, y_test, cmap=plt.cm.Reds)
  25. cmd.figure_.savefig(f'{out_path}confusion_matrix.svg', format='svg')
  26. c_matrix = cmd.confusion_matrix
  27. accuracy = model.score(X_test, y_test)
  28. print(f'Finished Training LinearSVC Model:\nStats:')
  29. print(f'\tConfusion Matrix:\n{c_matrix}')
  30. print(f'\tModel Accuracy: {accuracy}')
  31. with dagshub_logger(metrics_path=f'{out_path}metrics.csv', hparams_path=f'{out_path}params.yml') as logger:
  32. logger.log_hyperparams(penalty='l2')
  33. logger.log_metrics(accuracy=accuracy, confusion_matrics=c_matrix)
  34. if __name__ == '__main__':
  35. plac.call(main)
Tip!

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

Comments

Loading...