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

fraud_svm_test.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  1. import pandas as pd
  2. import numpy as np
  3. from nltk.tokenize import word_tokenize
  4. from nltk import pos_tag
  5. from nltk.corpus import stopwords
  6. from nltk.stem import WordNetLemmatizer
  7. from sklearn.preprocessing import LabelEncoder
  8. from collections import defaultdict
  9. from nltk.corpus import wordnet as wn
  10. from sklearn.feature_extraction.text import TfidfVectorizer
  11. from sklearn import model_selection, naive_bayes, svm
  12. from sklearn.metrics import accuracy_score
  13. from sklearn.metrics import roc_curve, auc
  14. from sklearn.metrics import plot_confusion_matrix
  15. import matplotlib.pyplot as plt
  16. from sklearn.model_selection import GridSearchCV
  17. from sklearn.model_selection import train_test_split
  18. import pickle
  19. from sklearn.metrics import confusion_matrix
  20. import json
  21. x_test = pd.read_csv('Data/x_test.csv')
  22. x_test = x_test.iloc[1: , :]
  23. y_test = pd.read_csv('Data/y_test.csv')
  24. x_test = x_test.iloc[: , 1:]
  25. y_test=y_test.iloc[: , 1:]
  26. Pkl_Filename = "Models/Fraud_SVM.pkl"
  27. with open(Pkl_Filename, 'rb') as file:
  28. SVM = pickle.load(file)
  29. predictions_SVM = SVM.predict(x_test)
  30. test_fpr, test_tpr, te_thresholds = roc_curve(y_test, predictions_SVM)
  31. print(test_fpr)
  32. print(test_tpr)
  33. plt.subplots(1, figsize=(10,10))
  34. plt.title('Receiver Operating Characteristic')
  35. a=plt.plot(test_fpr, test_tpr)
  36. plt.plot([0, 1], ls="--")
  37. plt.plot([0, 0], [1, 0] , c=".7"), plt.plot([1, 1] , c=".7")
  38. plt.ylabel('True Positive Rate')
  39. plt.xlabel('False Positive Rate')
  40. plt.show()
  41. plt.savefig('auc.png')
  42. # Use accuracy_score function to get the accuracy
  43. print("SVM Accuracy Score -> ",accuracy_score(predictions_SVM, y_test)*100)
  44. accuracy=accuracy_score(predictions_SVM, y_test)*100
  45. print(accuracy)
  46. #a={"Accuracy": accuracy, "fpr": test_fpr,"tpr": test_tpr}
  47. #b=a.tolist()
  48. data = {'accuracy':accuracy,'fpr':test_fpr.tolist(),'tpr':test_tpr.tolist()}
  49. with open('Output/Accuracy.json', 'w') as f:
  50. json.dump(data,f, sort_keys=True, indent=4, separators=(',', ': '))
  51. #Accuracy=accuracy_score(predictions_SVM, y_test)*100
  52. #a={"Accuracy": Accuracy, "fpr": test_fpr,"tpr": test_tpr}
  53. #b=a.tolist()
  54. #data = {'accuracy':accuracy,'fpr':test_fpr.tolist(),'tpr':test_tpr.tolist()}
  55. '''
  56. with open('Output/Accuracy.json', 'w') as f:
  57. json.dump(data,f, indent=4, separators=(',', ': '))
  58. '''
  59. with open('plots.json', 'w') as fd:
  60. json.dump(
  61. {
  62. "plots": [
  63. {"fpr": fp, "tpr": tp, "threshold": t}
  64. for fp, tp, t in zip(test_fpr.tolist(), test_tpr.tolist(), te_thresholds.tolist())
  65. ]
  66. },
  67. fd,
  68. indent=4,
  69. )
  70. #a=plt.plot_roc_curve(test_fpr, test_tpr, label=" AUC TEST ="+str(auc(test_fpr, test_tpr)))
  71. #plt.legend()
  72. #plt.xlabel("True Positive Rate")
  73. #plt.ylabel("False Positive Rate")
  74. #plt.title("AUC(ROC curve)")
  75. plt.plot(test_fpr, test_tpr, label=" AUC TEST ="+str(auc(test_fpr, test_tpr)))
  76. plt.legend()
  77. plt.xlabel("True Positive Rate")
  78. plt.ylabel("False Positive Rate")
  79. plt.title("AUC(ROC curve)")
  80. plot_confusion_matrix(SVM, x_test, y_test, normalize='true')
  81. plt.savefig('confusion_mat.png')
  82. '''
  83. a=confusion_matrix(y_test,predictions_SVM)
  84. b=a.tolist()
  85. with open('Output/Confusion_matrix.json', 'w') as f:
  86. json.dump(b, f)
  87. '''
Tip!

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

Comments

Loading...