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

model.py 1.2 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
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. from sklearn.linear_model import LogisticRegression
  4. from sklearn import metrics
  5. train = pd.read_csv('data/heart_train.csv')
  6. test = pd.read_csv('data/heart_test.csv')
  7. # Train the Model
  8. clf = LogisticRegression(penalty='l2', C=0.1)
  9. clf.fit(train.drop('target', axis = 1), train['target'])
  10. y_pred = clf.predict(test.drop('target', axis = 1))
  11. y_pred_proba = clf.predict_proba(test.drop('target', axis = 1))[::,1]
  12. # Test the model using AOC-ROC Graph
  13. def auc_roc_plot(y_test, y_pred_proba):
  14. fpr, tpr, _ = metrics.roc_curve(y_test, y_pred_proba)
  15. auc = metrics.roc_auc_score(y_test, y_pred_proba)
  16. plt.plot(fpr,tpr,label="data 1, auc="+str(auc))
  17. plt.legend(loc=4)
  18. plt.title('AUC-ROC plot')
  19. plt.savefig('model_results.png', dpi=120)
  20. def accuracy(y_test, y_pred):
  21. """
  22. Calculuates accuracy y_test and y_preds.
  23. """
  24. return metrics.accuracy_score(y_test, y_pred)
  25. auc_roc_plot(test['target'],y_pred_proba)
  26. accuracy = accuracy(test['target'], y_pred)
  27. print('Accuracy Score: ',accuracy)
  28. # Write metrics to file
  29. with open('metrics.txt', 'w') as outfile:
  30. outfile.write(f'\nAccuracy Score = {accuracy}.')
Tip!

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

Comments

Loading...