Advertisement
print623

CW_task4_Random Forest

Dec 20th, 2023 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | Cybersecurity | 0 0
  1. import numpy as np
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.tree import DecisionTreeClassifier
  4. from sklearn.metrics import confusion_matrix, accuracy_score
  5. from sklearn.metrics import make_scorer, f1_score
  6. # Load the dataset
  7. data = np.load('C:/Users/print15207/MATLAB Drive/Print HVDC/Smartgrid CW/train_dataset.npy', allow_pickle=True)
  8. # Extract features and labels
  9. x = data.item()['feature']
  10. y = data.item()['label']
  11. x1=x[:4800] #Only classify between class 0 (normal measurement) and class 1 (FDI attack measurement)
  12. y1=y[:4800]
  13. # Split the data into training and testing sets
  14. x_train, x_test, y_train, y_test = train_test_split(x1, y1, test_size=0.315, random_state=42)
  15. # Print the shape of training and testing sets
  16. print("Training set sizee:", x_train.shape)
  17. print("Testing set size:", x_test.shape)
  18. from sklearn.ensemble import RandomForestClassifier
  19. # Initialize a Random Forest model
  20. rf_model = RandomForestClassifier(n_estimators=77, random_state=42)
  21. # Train the Random Forest model
  22. rf_model.fit(x_train, y_train)
  23. # Make predictions
  24. rf_test_predict = rf_model.predict(x_test)
  25. # Make predictions on the test set
  26. test_predict = rf_model.predict(x_test)
  27. # Evaluate the model
  28. accuracy = accuracy_score(y_test, test_predict)
  29. # y_test set is the true value and test_predict set is the predicted value
  30. print("accuracy on test set: ", accuracy)
  31. # Evaluate the model using TPR and FPR
  32. conf_matrix = confusion_matrix(y_test, test_predict)
  33. print("Confusion Matrix:")
  34. print(conf_matrix)
  35. TN, FP, FN, TP = conf_matrix.ravel()
  36. # Calculate TPR and FPR
  37. TPR = TP / (TP + FN)
  38. FPR = FP / (FP + TN)
  39. # Print or use the metrics
  40. print("True Positive Rate (TPR):", TPR)
  41. print("False Positive Rate (FPR):", FPR)
  42. # Define F1 score as the evaluation metric for hyperparameter tuning
  43. scorer = make_scorer(f1_score)
  44. test_f1_score = f1_score(y_test, test_predict)
  45. print("F1 score on test set: ",test_f1_score)
  46. #Result with elapsed time: 5 seconds:
  47. #Training set sizee: (3288, 34)
  48. #Testing set size: (1512, 34)
  49. #accuracy on test set:  0.998015873015873
  50. #Confusion Matrix:
  51. [[772   3]
  52.  [  0 737]]
  53. #True Positive Rate (TPR): 1.0
  54. #False Positive Rate (FPR): 0.003870967741935484
  55. #F1 score on test set:  0.997968855788761
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement