Spaces:
Configuration error
Configuration error
Upload train_model.py
Browse files- train_model.py +61 -0
train_model.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""train_model.py
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1Oq8iUsTw8pCPeB1qLiaYLCI6QRVjO05g
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
pip install joblib
|
| 11 |
+
|
| 12 |
+
import pandas as pd
|
| 13 |
+
import numpy as np
|
| 14 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 15 |
+
from sklearn.preprocessing import LabelEncoder
|
| 16 |
+
from sklearn.model_selection import train_test_split
|
| 17 |
+
import joblib
|
| 18 |
+
import os
|
| 19 |
+
|
| 20 |
+
# Sample dataset (replace with your actual data)
|
| 21 |
+
data = {
|
| 22 |
+
'face_shape': ['Oval', 'Round', 'Square', 'Oval', 'Round', 'Square'] * 10,
|
| 23 |
+
'skin_tone': ['Fair', 'Medium', 'Dark', 'Medium', 'Dark', 'Fair'] * 10,
|
| 24 |
+
'face_size': ['Small', 'Medium', 'Large', 'Medium', 'Large', 'Small'] * 10,
|
| 25 |
+
'mask_style': ['Glitter', 'Animal', 'Floral', 'Animal', 'Glitter', 'Floral'] * 10
|
| 26 |
+
}
|
| 27 |
+
df = pd.DataFrame(data)
|
| 28 |
+
|
| 29 |
+
# Initialize LabelEncoders
|
| 30 |
+
encoders = {
|
| 31 |
+
'face_shape': LabelEncoder().fit(df['face_shape']),
|
| 32 |
+
'skin_tone': LabelEncoder().fit(df['skin_tone']),
|
| 33 |
+
'face_size': LabelEncoder().fit(df['face_size']),
|
| 34 |
+
'mask_style': LabelEncoder().fit(df['mask_style'])
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
# Encode features
|
| 38 |
+
X = pd.DataFrame({
|
| 39 |
+
'face_shape': encoders['face_shape'].transform(df['face_shape']),
|
| 40 |
+
'skin_tone': encoders['skin_tone'].transform(df['skin_tone']),
|
| 41 |
+
'face_size': encoders['face_size'].transform(df['face_size'])
|
| 42 |
+
})
|
| 43 |
+
y = encoders['mask_style'].transform(df['mask_style'])
|
| 44 |
+
|
| 45 |
+
# Train-test split
|
| 46 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 47 |
+
|
| 48 |
+
# Train Random Forest
|
| 49 |
+
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 50 |
+
model.fit(X_train, y_train)
|
| 51 |
+
|
| 52 |
+
# Create model directory
|
| 53 |
+
os.makedirs('model', exist_ok=True)
|
| 54 |
+
|
| 55 |
+
# Save model and encoders
|
| 56 |
+
joblib.dump(model, 'model/random_forest.pkl')
|
| 57 |
+
joblib.dump(encoders, 'model/label_encoders.pkl')
|
| 58 |
+
|
| 59 |
+
print("Model trained and saved successfully!")
|
| 60 |
+
print(f"Test Accuracy: {model.score(X_test, y_test):.2f}")
|
| 61 |
+
|