Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from sklearn.model_selection import train_test_split
- from sklearn.tree import DecisionTreeClassifier
- from sklearn.metrics import confusion_matrix, accuracy_score
- from sklearn.metrics import make_scorer, f1_score
-
- # Load the dataset
- data = np.load('C:/Users/print15207/MATLAB Drive/Print HVDC/Smartgrid CW/train_dataset.npy', allow_pickle=True)
-
- # Extract features and labels
- x = data.item()['feature']
- y = data.item()['label']
-
- x1=x[:4800] #Only classify between class 0 (normal measurement) and class 1 (FDI attack measurement)
- y1=y[:4800]
-
- # Split the data into training and testing sets
- x_train, x_test, y_train, y_test = train_test_split(x1, y1, test_size=0.325, random_state=42)
-
- # Print the shape of training and testing sets
- print("Training set size:", x_train.shape)
- print("Testing set size:", x_test.shape)
-
- # Setting maximum depth and minimum samples per leaf
- pruned_model = DecisionTreeClassifier(max_depth=10, min_samples_leaf=4, random_state=42)
- pruned_model.fit(x_train, y_train)
- # Make predictions on the test set
- test_predict = pruned_model.predict(x_test)
-
- # Evaluate the model
- accuracy = accuracy_score(y_test, test_predict)
- # y_test set is the true value and test_predict set is the predicted value
- print("accuracy on test set: ", accuracy)
-
- # Evaluate the model using TPR and FPR
- conf_matrix = confusion_matrix(y_test, test_predict)
- print("Confusion Matrix:")
- print(conf_matrix)
-
- TN, FP, FN, TP = conf_matrix.ravel()
-
- # Calculate TPR and FPR
- TPR = TP / (TP + FN)
- FPR = FP / (FP + TN)
-
- # Print or use the metrics
- print("True Positive Rate (TPR):", TPR)
- print("False Positive Rate (FPR):", FPR)
-
- # Define F1 score as the evaluation metric for hyperparameter tuning
- scorer = make_scorer(f1_score)
- test_f1_score = f1_score(y_test, test_predict)
- print("F1 score on test set: ",test_f1_score)
-
-
- #Result with elapsed time: 2 seconds:
- #Training set size: (3240, 34)
- #Testing set size: (1560, 34)
- #accuracy on test set: 0.9846153846153847
- #Confusion Matrix:
- [[789 12]
- [ 12 747]]
- #True Positive Rate (TPR): 0.9841897233201581
- #False Positive Rate (FPR): 0.0149812734082397
- #F1 score on test set: 0.9841897233201581
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement