ML Project – Facial Expression Recognition using Gradient Boosting

Machine Learning courses with 100+ Real-time projects Start Now!!

Program 1

Facial Expression Dataset

import pandas as pd
import numpy as np
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.preprocessing import LabelEncoder
import matplotlib.pyplot as plt
import seaborn as sns

# Generate synthetic facial expression data (for demonstration)
np.random.seed(42)
n_samples = 1000
data = {
    'eyebrow_angle': np.random.normal(20, 5, n_samples),
    'eye_openness': np.random.normal(0.6, 0.1, n_samples),
    'mouth_curve': np.random.normal(0, 1, n_samples),
    'cheek_raise': np.random.normal(0.5, 0.2, n_samples),
    'expression': np.random.choice(['happy', 'sad', 'angry'], size=n_samples)
}
df = pd.DataFrame(data)
df.head()

# Encode labels
le = LabelEncoder()
df['expression'] = le.fit_transform(df['expression'])  # happy=0, sad=1, angry=2
df.head()

# Features and labels
X = df.drop('expression', axis=1) # Independed vairable (Input)
y = df['expression'] # Depended variabels(output)
X.head()
y.head()

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Model
model = GradientBoostingClassifier()
model.fit(X_train, y_train)

y_pred = model.predict(X_test)
y_pred
model.score(X_test,y_pred)

# Confusion matrix and report
cm = confusion_matrix(y_test, y_pred)
cm

# Save dataset
csv_path = "D://scikit_data/facial_expression_data.csv"
df.to_csv(csv_path, index=False)

# Plot confusion matrix
plt.figure(figsize=(6, 4))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
            xticklabels=le.classes_, yticklabels=le.classes_)
plt.xlabel("Predicted")
plt.ylabel("Actual")
plt.title("Confusion Matrix - Facial Expression Recognition")
plt.tight_layout()
plt.show()

# happy=0, sad=1, angry=2

 

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *