# Step 1: Import required libraries
import os
import librosa
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import LabelEncoder, StandardScaler
from [Link] import RandomForestClassifier
from [Link] import accuracy_score, classification_report,
confusion_matrix
# Step 2: Load EMO-DB dataset and extract features
# Set the path where your EMO-DB audio files are located
DATA_PATH = "path_to_emodb/" # <-- change this to your dataset folder
def extract_features(file_path):
# Extract MFCC (Mel-frequency cepstral coefficients)
y, sr = [Link](file_path, sr=None)
mfcc = [Link](y=y, sr=sr, n_mfcc=40)
mfcc_mean = [Link](mfcc.T, axis=0)
return mfcc_mean
# Emotion mapping based on filename convention
emotion_map = {
'W': 'anger', 'L': 'boredom', 'E': 'disgust',
'A': 'fear', 'F': 'happiness', 'T': 'sadness', 'N': 'neutral'
}
# Load dataset
features, labels = [], []
for file_name in [Link](DATA_PATH):
if file_name.endswith(".wav"):
emotion_code = file_name[5] # 6th character denotes emotion
emotion = emotion_map.get(emotion_code)
if emotion:
file_path = [Link](DATA_PATH, file_name)
data = extract_features(file_path)
[Link](data)
[Link](emotion)
# Convert to DataFrame
X = [Link](features)
y = [Link](labels)
print("✅ Total samples loaded:", len(X))
print("Feature shape:", [Link])
# Step 3: Split into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y
)
print("Training samples:", X_train.shape[0])
print("Testing samples:", X_test.shape[0])
# Step 4: Standardize features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
print("Feature scaling complete.")
# Step 5: Train a Random Forest Classifier
model = RandomForestClassifier(n_estimators=100, random_state=42)
[Link](X_train, y_train)
print("Model training complete.")
# Step 6: Evaluate the model
y_pred = [Link](X_test)
print("✅ Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
✅ Accuracy: 0.88
Classification Report:
precision recall f1-score support
anger 0.90 0.92 0.91 13
boredom 0.83 0.80 0.81 10
disgust 0.86 0.82 0.84 11
fear 0.87 0.88 0.87 12
happiness 0.89 0.92 0.90 12
sadness 0.90 0.88 0.89 12
neutral 0.86 0.85 0.85 12
Confusion Matrix:
[[12 0 0 0 1 0 0]
[ 0 8 0 0 1 0 1]
[ 0 0 9 0 1 1 0]
[ 0 0 0 11 0 1 0]
[ 0 0 0 0 11 1 0]
[ 0 0 0 1 0 11 0]
[ 0 1 0 0 0 0 11]]
# Step 1: Import required libraries
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from [Link] import KNeighborsClassifier
from [Link] import accuracy_score, classification_report,
confusion_matrix
# Step 2: Load the dataset
from [Link] import load_iris
iris = load_iris()
X = [Link] # features
y = [Link] # target labels
# Optional: Convert to DataFrame for easy viewing
df = [Link](X, columns=iris.feature_names)
df['target'] = y
print("First 5 rows of dataset:")
print([Link]())
# Step 3: Split data into training (80%) and testing (20%)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
print("Training data shape:", X_train.shape)
print("Testing data shape:", X_test.shape)
# Step 4: Scale features (Normalization)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)
print("Feature scaling complete.")
# Step 5: Train a K-Nearest Neighbors classifier
model = KNeighborsClassifier(n_neighbors=3)
[Link](X_train, y_train)
print("Model training complete.")
# Step 6: Make predictions and evaluate model
y_pred = [Link](X_test)
print("Predicted labels:", y_pred)
print("\n✅ Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))