-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_gesture_model.py
More file actions
38 lines (30 loc) · 1.22 KB
/
train_gesture_model.py
File metadata and controls
38 lines (30 loc) · 1.22 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
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
import joblib
# Load cleaned combined CSV and force 'label' column to be string
df = pd.read_csv("combined_clean_gesture_data.csv", dtype={'label': str})
# Separate features and labels
X = df.drop(columns=["label"])
y = df["label"].astype(str).str.upper() # ensure uniform string format
# Encode labels
label_encoder = LabelEncoder()
y_encoded = label_encoder.fit_transform(y)
# Split dataset
X_train, X_test, y_train, y_test = train_test_split(
X, y_encoded, test_size=0.2, random_state=42, stratify=y_encoded
)
# Train model
model = RandomForestClassifier(n_estimators=100, random_state=42)
model.fit(X_train, y_train)
# Evaluate
y_pred = model.predict(X_test)
print(f"\n✅ Accuracy: {accuracy_score(y_test, y_pred)*100:.2f}%\n")
print("Classification report:")
print(classification_report(y_test, y_pred, target_names=label_encoder.classes_))
# Save model and encoder
joblib.dump(model, "gesture_model.pkl")
joblib.dump(label_encoder, "gesture_labels.pkl")
print("✅ Model and encoder saved.")