-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_classifier.py
More file actions
39 lines (30 loc) · 1.16 KB
/
Copy pathtrain_classifier.py
File metadata and controls
39 lines (30 loc) · 1.16 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
import pickle
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import numpy as np
# Load data
try:
data_dict = pickle.load(open('./data.pickle', 'rb'))
except Exception as e:
raise RuntimeError(f"Failed to load data.pickle: {e}")
data = np.asarray(data_dict.get('data', []))
labels = np.asarray(data_dict.get('labels', []))
# Check if data is loaded correctly
if len(data) == 0 or len(labels) == 0:
raise ValueError("No data found in data.pickle! Ensure data was saved correctly.")
# Check data-label size match
if len(data) != len(labels):
raise ValueError(f"Mismatch between number of data samples ({len(data)}) and labels ({len(labels)}).")
# Split and train
x_train, x_test, y_train, y_test = train_test_split(
data, labels, test_size=0.2, shuffle=True, stratify=labels
)
model = RandomForestClassifier()
model.fit(x_train, y_train)
y_predict = model.predict(x_test)
score = accuracy_score(y_predict, y_test)
print(f'{score * 100:.2f}% of samples were classified correctly!')
# Save model
with open('model.p', 'wb') as f:
pickle.dump({'model': model}, f)