Advertisement
print623

CW_task4_kernel_SVM

Dec 20th, 2023 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | Cybersecurity | 0 0
  1. #1. Importing Libraries:
  2. from sklearn import svm
  3. import numpy as np
  4. from sklearn.model_selection import GridSearchCV, 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 Validation 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 validation sets using train_test_split from scikit-learn.
  20. print("Training set size: ",x_train.shape)
  21. print("Validation set size: ",x_test.shape)
  22. #5.Model Initialization and Training:
  23. model=svm.SVC(C=10,kernel='rbf',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 validation set: ",test_score)
  32. #The accuracy of the model on the training and validation 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 validation 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 validation set: ",test_f1_score)
  52. #7.Loading and Predicting on test_feature Data:
  53. data2 = np.load('C:/Users/print15207\/MATLAB Drive/Print HVDC/Smartgrid CW/test_feature.npy',allow_pickle=True)
  54. print("test_feature size:",data2.shape)
  55. test_predict2 = model.predict(data2)
  56. print("Predictions on test feature:", test_predict2)
  57. #8.Assessing Predictions on Test Data:
  58. def assess(y_pred):
  59.     assert np.all((y_pred==0)|(y_pred==1))
  60.     assert len(y_pred.shape)==1
  61.     assert y_pred.shape[0]==1200
  62. assess(test_predict2)
  63. #The assess function checks certain conditions about the predicted labels.
  64. #9.Saving Predictions to a NumPy File:
  65. np.save(f"group_8.npy", test_predict2)
  66. #The predictions are saved to a NumPy file named "group_8.npy".
  67. #Result with elapsed time: 2 seconds:
  68. #Training set size:  (3268, 34)
  69. #Validation set size:  (1532, 34)
  70. #Accuracy on training set:  1.0
  71. #Accuracy on test set:  0.9993472584856397
  72. #Confusion Matrix:
  73. [[785   0]
  74.  [  1 746]]
  75. #True Positive Rate (TPR): 0.998661311914324
  76. #False Positive Rate (FPR): 0.0
  77. #F1 score on test set:  0.999330207635633
  78. #test_feature size: (1200, 34)
  79. #Predictions on test feature: [0. 0. 1. ... 0. 1. 1.]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement