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

plots.py 1.6 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
  1. from itertools import cycle
  2. import matplotlib.pyplot as plt
  3. from sklearn.metrics import roc_curve, auc, confusion_matrix
  4. import seaborn as sns
  5. def confusion_matrix_plot(y_test, predictions, classes):
  6. cm = confusion_matrix(y_test, predictions)
  7. ax = plt.subplot()
  8. sns.heatmap(cm, annot=True, cmap="YlGnBu", ax=ax)
  9. ax.set_xlabel('Predicted labels')
  10. ax.set_ylabel('True labels')
  11. ax.set_title('Confusion Matrix')
  12. ax.xaxis.set_ticklabels(classes)
  13. ax.yaxis.set_ticklabels(classes)
  14. def roc_auc_multiclass(X_test, y_test, model_type=None, model=None):
  15. if model_type in ['logistic regression', 'random forest', 'kneighbors']:
  16. predictions = model.predict_proba(X_test)
  17. elif model_type == 'support vector machine':
  18. predictions = model.decision_function(X_test)
  19. n_classes = y_test.shape[1]
  20. fpr = dict()
  21. tpr = dict()
  22. roc_auc = dict()
  23. for i in range(n_classes):
  24. fpr[i], tpr[i], _ = roc_curve(y_test[:, i], predictions[:, i])
  25. roc_auc[i] = auc(fpr[i], tpr[i])
  26. colors = cycle(['blue', 'red', 'green', 'yellow', 'purple', 'orange', 'pink'])
  27. for i, color in zip(range(n_classes), colors):
  28. plt.plot(fpr[i], tpr[i], color=color, lw=1.5,
  29. label='ROC curve of class {0} (area = {1:0.2f})'
  30. ''.format(i, roc_auc[i]))
  31. plt.plot([0, 1], [0, 1], 'k--', lw=1.5)
  32. plt.xlim([-0.05, 1.0])
  33. plt.ylim([0.0, 1.05])
  34. plt.xlabel('False Positive Rate')
  35. plt.ylabel('True Positive Rate')
  36. plt.title('Receiver operating characteristic for multi-class data')
  37. plt.legend(loc="lower right")
Tip!

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

Comments

Loading...