SHD-WebsiteFingerprintingLab/part2/eval.py

36 lines
1.3 KiB
Python
Raw Normal View History

2023-01-31 20:06:27 +00:00
import json
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
def eval():
2024-02-01 21:12:05 +00:00
y_pred_full, y_test_full = [], []
2024-11-02 12:50:02 +00:00
traces=json.load(open('../traces_nomem.out'))["traces"]
labels=json.load(open('../traces_nomem.out'))["labels"]
print("Number of traces: ", len(traces))
2024-02-01 21:12:05 +00:00
# Re-train 10 times in order to reduce effects of randomness
for i in range(10):
### TODO: Exercise 2-5
### 1. Load data from traces file
### 2. Split data into X_train, X_test, y_train, y_test with train_test_split
### 3. Train classifier with X_train and y_train
### 4. Use classifier to make predictions on X_test. Save the result to a variable called y_pred
2024-11-02 12:50:02 +00:00
X_train, X_test, y_train, y_test = train_test_split(traces, labels, test_size=0.2)
clf = RandomForestClassifier(n_estimators=100)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
2023-01-31 20:06:27 +00:00
2024-02-01 21:12:05 +00:00
# Do not modify the next two lines
y_test_full.extend(y_test)
y_pred_full.extend(y_pred)
2023-01-31 20:06:27 +00:00
2024-02-01 21:12:05 +00:00
### TODO: Exercise 2-5 (continued)
### 5. Print classification report using y_test_full and y_pred_full
2024-11-02 12:50:02 +00:00
print(classification_report(y_test_full, y_pred_full))
2023-01-31 20:06:27 +00:00
if __name__ == "__main__":
2024-11-02 12:50:02 +00:00
2024-02-01 21:12:05 +00:00
eval()