-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_train.py
More file actions
76 lines (66 loc) · 2.1 KB
/
Copy pathmodel_train.py
File metadata and controls
76 lines (66 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import pickle
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import (
accuracy_score,
classification_report,
confusion_matrix,
)
DATASET_PATH = "data.pickle"
LABEL_MAP_PATH = "label_map.pickle"
MODEL_PATH = "model.p"
print("Loading dataset...")
if not os.path.exists(DATASET_PATH):
raise FileNotFoundError(f"Dataset file '{DATASET_PATH}' not found.")
with open(DATASET_PATH, "rb") as f:
data_dict = pickle.load(f)
X = np.array(data_dict["data"])
y = np.array(data_dict["labels"])
print("Loading label map...")
if not os.path.exists(LABEL_MAP_PATH):
raise FileNotFoundError(f"Label map file '{LABEL_MAP_PATH}' not found.")
with open(LABEL_MAP_PATH, "rb") as f:
raw_label_map = pickle.load(f)
label_map = {v: k for k, v in raw_label_map.items()}
print(f"Loaded {len(label_map)} class labels.")
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=42
)
print("Training RandomForest model...")
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
print("Evaluating model...")
y_pred = model.predict(X_test)
acc = accuracy_score(y_test, y_pred)
print(f"Accuracy: {acc:.2f}")
unique_labels = np.unique(np.concatenate([y_test, y_pred])).tolist()
target_names = [str(label_map[i]) for i in unique_labels]
print("Classification Report:")
print(
classification_report(
y_test, y_pred, labels=unique_labels, target_names=target_names
)
)
conf_matrix = confusion_matrix(y_test, y_pred, labels=unique_labels)
plt.figure(figsize=(12, 10))
sns.heatmap(
conf_matrix,
annot=True,
fmt="d",
cmap="Blues",
xticklabels=target_names,
yticklabels=target_names,
)
plt.title("Confusion Matrix")
plt.xlabel("Predicted Label")
plt.ylabel("True Label")
plt.tight_layout()
plt.show()
print("Saving model and label map into 'model.p'...")
with open(MODEL_PATH, "wb") as f:
pickle.dump({"model": model, "label_map": label_map}, f)
print("Done.")