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

confusion_matrix.py 3.1 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  1. import itertools
  2. import os
  3. import sys
  4. import matplotlib.pyplot as plt
  5. from sklearn.model_selection import train_test_split
  6. from sklearn.metrics import confusion_matrix
  7. from sklearn.svm import SVC
  8. import numpy as np
  9. def set_trace():
  10. """A Poor mans break point"""
  11. # without this in iPython debugger can generate strange characters.
  12. from IPython.core.debugger import Pdb
  13. Pdb().set_trace(sys._getframe().f_back)
  14. def plot_confusion_matrix(cm,
  15. class_names,
  16. title='Confusion matrix',
  17. cmap=plt.cm.Blues):
  18. plt.imshow(cm, interpolation='nearest', cmap=cmap)
  19. plt.title(title)
  20. plt.colorbar()
  21. tick_marks = np.arange(len(class_names))
  22. plt.xticks(tick_marks, class_names, rotation=45)
  23. plt.yticks(tick_marks, class_names)
  24. plt.tight_layout()
  25. width, height = cm.shape
  26. for x in xrange(width):
  27. for y in xrange(height):
  28. plt.annotate(str(cm[x][y]), xy=(y, x),
  29. horizontalalignment='center',
  30. verticalalignment='center')
  31. plt.ylabel('True label')
  32. plt.xlabel('Predicted label')
  33. plt.show()
  34. def svm_classifier(x, y, validation):
  35. """Split the data into a training set and a test set."""
  36. """Run SVM Classifier."""
  37. X_train, X_test, y_train, y_test = train_test_split(x, y, random_state=0)
  38. classifier = SVC(kernel='linear', C=0.01)
  39. y_pred = classifier.fit(X_train, y_train).predict(X_test)
  40. class_names = np.array(['Criminals','Non-criminals'])
  41. cm = confusion_matrix(y_test, y_pred)
  42. plot_confusion_matrix(cm, class_names)
  43. '''
  44. def plot_confusion_matrix(cm, classes, normalize=False,title='Confusion matrix',cmap=plt.cm.Blues):
  45. if normalize:
  46. cm =cm.astype('float') / cm.sum(axis=1)[:, np.newaxix]
  47. print('Confusion matrix, without normalization')
  48. print(cm)
  49. plt.imshow(cm, interpolation='nearest', cmap=cmap)
  50. plt.title(title)
  51. plt.colorbar()
  52. tick_marks = np.arange(len(classes))
  53. plt.xticks(tick_marks, classes, rotation=45)
  54. plt.yticks(tick_marks, classes)
  55. fmt = '.2f' if normalize else 'd'
  56. thresh = cm.max() / 2.
  57. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
  58. plt.text(j, i, format(cm[i, j], fmt),
  59. horizontalalignment="center",
  60. color="white" if cm[i, j] > thresh else "black")
  61. plt.tight_layout()
  62. plt.ylabel('True label')
  63. plt.xlabel('Predicted label')
  64. '''
  65. '''
  66. # Compute confusion matrix
  67. cnf_matrix = confusion_matrix(y_test, y_pred)
  68. np.set_printoptions(precision=2)
  69. # Plot non-normalized confusion matrix
  70. plt.figure()
  71. plot_confusion_matrix(cnf_matrix, classes=class_names,
  72. title='Confusion matrix, without normalization')
  73. # Plot normalized confusion matrix
  74. plt.figure()
  75. plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
  76. title='Normalized confusion matrix')
  77. plt.show()
  78. '''
Tip!

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

Comments

Loading...