-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_classifier.py
38 lines (29 loc) · 1.15 KB
/
train_classifier.py
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
import pickle
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Load the processed data from pickle file
try:
with open('./data.pickle', 'rb') as f:
data_dict = pickle.load(f)
except FileNotFoundError:
print("The pickle file 'data.pickle' was not found. Please check the file path.")
exit(1)
# Extract features and labels
data = np.asarray(data_dict['data'])
labels = np.asarray(data_dict['labels'])
# Split the data into training and test sets
x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, shuffle=True, stratify=labels)
# Initialize the RandomForest classifier
model = RandomForestClassifier()
# Train the model on the training data
model.fit(x_train, y_train)
# Predict the labels for the test set
y_predict = model.predict(x_test)
# Calculate the accuracy of the model
score = accuracy_score(y_test, y_predict)
print(f'{score * 100:.2f}% of samples were classified correctly!')
# Save the trained model to a pickle file
with open('model.p', 'wb') as f:
pickle.dump({'model': model}, f)