-
Notifications
You must be signed in to change notification settings - Fork 9
/
mlp classifier for SER.py
129 lines (102 loc) · 3.42 KB
/
mlp classifier for SER.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
import loading_data
#FIRST MODEL
model_params = {
'alpha': 0.01,
'batch_size': 256,
'epsilon': 1e-08,
'hidden_layer_sizes': (300,),
'learning_rate': 'adaptive',
'max_iter': 500,
}
# initialize Multi Layer Perceptron classifier
# with best parameters ( so far )
model = MLPClassifier(**model_params)
# train the model
print("[*] Training the model...")
model.fit(X_train, y_train)
# predict 25% of data to measure how good we are
y_pred = model.predict(X_test)
# calculate the accuracy
accuracy = accuracy_score(y_true=y_test, y_pred=y_pred)
print("Accuracy: {:.2f}%".format(accuracy*100))
"""Obtained accuracy is Accuracy: 82.07%"""
"""Printing classification report and confusion matrix"""
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
print(classification_report(y_test,y_pred))
print(confusion_matrix(y_test,y_pred))
"""
precision recall f1-score support
angry 0.94 0.86 0.90 90
happy 0.81 0.80 0.80 94
neutral 0.70 0.73 0.71 44
sad 0.81 0.86 0.83 101
accuracy 0.82 329
macro avg 0.81 0.81 0.81 329
weighted avg 0.83 0.82 0.82 329
[[77 9 2 2]
[ 5 75 5 9]
[ 0 2 32 10]
[ 0 7 7 87]]"""
"""Second mlp model after tuning the parameters"""
m_params = {
'alpha': 0.01,
'batch_size': 200,
'epsilon': 1e-08,
'hidden_layer_sizes': (300,),
'learning_rate': 'adaptive',
'max_iter': 500,
}
# initialize Multi Layer Perceptron classifier
# with best parameters ( so far )
m1 = MLPClassifier(**m_params)
# train the model
print("[*] Training the model...")
m1.fit(X_train, y_train)
# predict 25% of data to measure how good we are
y_p = m1.predict(X_test)
# calculate the accuracy
accuracy = accuracy_score(y_true=y_test, y_pred=y_p)
print("Accuracy: {:.2f}%".format(accuracy*100))
# now we save the model
# make result directory if doesn't exist yet
"""Accuracy:82%"""
m_params = {
'alpha': 0.01,
'batch_size': 200,
'epsilon': 1e-08,
'hidden_layer_sizes': (300,),
'learning_rate': 'adaptive',
'max_iter': 500,
}
# initialize Multi Layer Perceptron classifier
# with best parameters ( so far )
m1 = MLPClassifier(**m_params)
# train the model
print("[*] Training the model...")
m1.fit(X_train, y_train)
# predict 25% of data to measure how good we are
y_p = m1.predict(X_test)
# calculate the accuracy
accuracy = accuracy_score(y_true=y_test, y_pred=y_p)
print("Accuracy: {:.2f}%".format(accuracy*100))
# now we save the model
# make result directory if doesn't exist yet
#Accuracy:80%
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
print(classification_report(y_test,y_p))
print(confusion_matrix(y_test,y_p))
"""precision recall f1-score support
angry 0.93 0.89 0.91 90
happy 0.69 0.91 0.79 94
neutral 0.76 0.59 0.67 44
sad 0.86 0.71 0.78 101
accuracy 0.80 329
macro avg 0.81 0.78 0.78 329
weighted avg 0.82 0.80 0.80 329
[[80 9 1 0]
[ 4 86 1 3]
[ 0 9 26 9]
[ 2 21 6 72]]"""
"""NOTE : Refer Speechemotion_mlp.ipynb...my colab notebook for better understanding"""