Advertisement
print623

CW_task4_linear_SVM

Dec 20th, 2023 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | Cybersecurity | 0 0
  1. #1. Importing Libraries:
  2. from sklearn import svm
  3. import numpy as np
  4. from sklearn.model_selection import train_test_split
  5. from sklearn.metrics import confusion_matrix
  6. from sklearn.metrics import make_scorer, f1_score
  7. #The script imports necessary libraries, including scikit-learn for the SVM model, NumPy for numerical operations, and matplotlib for plotting.
  8. #2. Loading Training Data:
  9. data = np.load('C:/Users/print15207/MATLAB Drive/Print HVDC/Smartgrid CW/train_dataset.npy',allow_pickle=True)
  10. #The training dataset is loaded from the specified NumPy file.
  11. #3.Extracting Features and Labels:
  12. x = data.item()['feature']
  13. y = data.item()['label']
  14. #The features (x) and labels (y) are extracted from the loaded data.
  15. #4.Splitting the Data into Training and Testing Sets:
  16. x1=x[:4800] #Only classify between class 0 (normal measurement) and class 1 (FDI attack measurement)
  17. y1=y[:4800]
  18. x_train, x_test, y_train, y_test = train_test_split(x1, y1, test_size=0.319, random_state=42)
  19. #The data is split into training and testing sets using train_test_split from scikit-learn.
  20. print("Training set size: ",x_train.shape)
  21. print("Testing set size: ",x_test.shape)
  22. #5.Model Initialization and Training:
  23. model=svm.SVC(C=2,kernel='linear',gamma=10,decision_function_shape='ovo')
  24. model.fit(x_train,y_train)  
  25. #An SVM model is initialized with specified parameters and trained on the training data.
  26. #6.Evaluating the Model:
  27. #6.1 Accuracy
  28. train_score = model.score(x_train,y_train)
  29. print("Accuracy on training set: ",train_score)
  30. test_score = model.score(x_test,y_test)
  31. print("Accuracy on test set: ",test_score)
  32. #The accuracy of the model on the training and testing sets.
  33. #6.2 TPR and FPR
  34. test_predict = model.predict(x_test)
  35. #The trained model is used to make predictions on the test data.
  36. conf_matrix = confusion_matrix(y_test, test_predict)
  37. print("Confusion Matrix:")
  38. print(conf_matrix)
  39. # y_test set is the true value and test_predict set is the predicted value
  40. TN, FP, FN, TP = conf_matrix.ravel()
  41. #ravel() is used to flatten the confusion matrix into a 1D array.
  42. # Calculate TPR and FPR
  43. TPR = TP / (TP + FN)
  44. FPR = FP / (FP + TN)
  45. print("True Positive Rate (TPR):", TPR)
  46. print("False Positive Rate (FPR):", FPR)
  47. #6.3 F1 score
  48. # Define F1 score as the evaluation metric for hyperparameter tuning
  49. scorer = make_scorer(f1_score)
  50. test_f1_score = f1_score(y_test, test_predict)
  51. print("F1 score on test set: ",test_f1_score)
  52. #Result with elapsed time: 2 seconds:
  53. #Training set size:  (3268, 34)
  54. #Testing set size:  (1532, 34)
  55. #Accuracy on training set:  0.7971236230110159
  56. #Accuracy on test set:  0.8067885117493473
  57. #Confusion Matrix:
  58. [[766  19]
  59.  [277 470]]
  60. #True Positive Rate (TPR): 0.6291834002677377
  61. #False Positive Rate (FPR): 0.024203821656050957
  62. #F1 score on test set:  0.7605177993527508
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement