-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEmotion-Recognition.py
46 lines (36 loc) · 1.4 KB
/
Emotion-Recognition.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
data = pd.read_csv('../Emotion_features.csv')
feature = data.ix[:, 'tempo':]
featureName = list(feature)
color = ['red' if l==1 else 'green' if l==2 else 'blue' if l==3 else 'orange' for l in data['label']]
for name in featureName:
feature[name] = (feature[name]-feature[name].min())/(feature[name].max()-feature[name].min())
plt.style.use('ggplot')
array = np.array(data)
features = feature.values
labels = data.ix[:, 'class'].dropna()
test_size = 0.20
random_seed = 7
train_d, test_d, train_l, test_l = train_test_split(features, labels, test_size=test_size, random_state=random_seed)
result = []
xlabel = [i for i in range(1, 11)]
for neighbors in range(1, 11):
kNN = KNeighborsClassifier(n_neighbors=neighbors)
kNN.fit(train_d, train_l)
prediction = kNN.predict(test_d)
result.append(accuracy_score(prediction, test_l)*100)
plt.figure(figsize=(10, 10))
plt.xlabel('kNN Neighbors for k=1,2...20')
plt.ylabel('Accuracy Score')
plt.title('kNN Classifier Results')
plt.ylim(0, 100)
plt.xlim(0, xlabel[len(xlabel)-1]+1)
plt.plot(xlabel, result)
plt.savefig('1-fold 10NN Result.png')
plt.show()