-
Notifications
You must be signed in to change notification settings - Fork 0
/
6_confusion_matrix_lstm.py
131 lines (96 loc) · 4.64 KB
/
6_confusion_matrix_lstm.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.preprocessing import LabelEncoder
from keras.models import Sequential
from keras.layers import LSTM, Dense
from keras.utils import to_categorical
import os
# Load the training data from the Excel file
train_data = pd.read_excel("D:/MCA/4th sem/SER3/output/Actor_01/train_test_data.xlsx", sheet_name='Training')
# Encode the target variable
label_encoder = LabelEncoder()
train_data['Emotion'] = label_encoder.fit_transform(train_data['Emotion'])
# Store the mapping of encoded labels to original labels
label_mapping = dict(zip(range(len(label_encoder.classes_)), label_encoder.classes_))
# Separate features (MFCCs) and target variable (Emotion)
X_train = train_data.drop(columns=['Audio File', 'Emotion'])
y_train = train_data['Emotion']
# Reshape X_train to 3D tensor for LSTM input
X_train_lstm = X_train.values.reshape(X_train.shape[0], X_train.shape[1], 1)
# Convert target variable to categorical
y_train_categorical = to_categorical(y_train)
# Define the LSTM model
model = Sequential()
model.add(LSTM(128, input_shape=(X_train_lstm.shape[1], X_train_lstm.shape[2])))
model.add(Dense(len(label_encoder.classes_), activation='softmax'))
# Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model to the training data
model.fit(X_train_lstm, y_train_categorical, epochs=10, batch_size=32)
# Load the testing data from the Excel file
test_data = pd.read_excel("D:/MCA/4th sem/SER3/output/Actor_01/train_test_data.xlsx", sheet_name='Testing')
# Encode the target variable
test_data['Emotion'] = label_encoder.transform(test_data['Emotion'])
# Separate features (MFCCs) and target variable (Emotion)
X_test = test_data.drop(columns=['Audio File', 'Emotion'])
y_test = test_data['Emotion']
# Reshape X_test to 3D tensor for LSTM input
X_test_lstm = X_test.values.reshape(X_test.shape[0], X_test.shape[1], 1)
# Convert target variable to categorical
y_test_categorical = to_categorical(y_test)
# Predict probabilities for each class for the testing data
y_pred_probabilities = model.predict(X_test_lstm)
# Get the predicted class labels
y_pred = y_pred_probabilities.argmax(axis=1)
# Convert the predicted and true labels back to their original emotion names
y_pred_emotions = [label_mapping[label] for label in y_pred]
y_true_emotions = [label_mapping[label] for label in y_test]
# Generate classification report
report = classification_report(y_true_emotions, y_pred_emotions, output_dict=True)
# Convert report to DataFrame
report_df = pd.DataFrame(report).transpose()
# Save classification report to Excel file
report_df.to_excel(os.path.join("D:/MCA/4th sem/SER3/output/Actor_01", "classify_report_lstm.xlsx"))
# Create confusion matrix
conf_matrix = confusion_matrix(y_true_emotions, y_pred_emotions)
# Calculate accuracy
accuracy = conf_matrix.diagonal().sum() / conf_matrix.sum()
# Calculate misclassification rate
misclassification_rate = 1 - accuracy
# Calculate precision for each class
precision = conf_matrix.diagonal() / conf_matrix.sum(axis=0)
# Handle division by zero
precision = np.nan_to_num(precision, nan=0)
# Calculate sensitivity (recall) for each class
sensitivity = conf_matrix.diagonal() / conf_matrix.sum(axis=1)
# Calculate specificity for each class
specificity = [(conf_matrix.sum() - conf_matrix[:, i].sum() - conf_matrix[i, :].sum() + conf_matrix[i, i]) /
(conf_matrix.sum() - conf_matrix[:, i].sum()) for i in range(conf_matrix.shape[0])]
# Calculate mean precision, sensitivity, and specificity
mean_precision = np.mean(precision)
mean_sensitivity = sensitivity.mean()
mean_specificity = sum(specificity) / len(specificity)
# Create a DataFrame for better visualization
labels = sorted(set(y_true_emotions) | set(y_pred_emotions))
conf_matrix_df = pd.DataFrame(conf_matrix, index=labels, columns=labels)
# Plot confusion matrix
plt.figure(figsize=(10, 8))
sns.heatmap(conf_matrix_df, annot=True, fmt='d', cmap='Blues')
plt.xlabel('Predicted Label')
plt.ylabel('True Label')
plt.title('Confusion Matrix (LSTM)')
# Save confusion matrix plot as an image file
plt.savefig(os.path.join("D:/MCA/4th sem/SER3/output/Actor_01", "confusion_matrix_lstm.png"))
# Show the confusion matrix plot
plt.show()
# Describe the confusion matrix
print("\nConfusion Matrix (LSTM):\n", conf_matrix_df)
# Print the metrics
print("\nAccuracy:", accuracy)
print("Misclassification Rate:", misclassification_rate)
print("\nMean Precision:", mean_precision)
print("Mean Sensitivity (Recall):", mean_sensitivity)
print("Mean Specificity:", mean_specificity)