# Emotion-Driven Music and IoT Devices for Collaborative Exer-Games
## 5 Music Recommendation and Personalization Based on Emotions
### Music Emotion's Recognition
Different types of machine learning algorithms were considered for their building: Random Forest (RF), K-Nearest Neighbor (KNN), Support Vector Machines (SVM), Gradient Boosting (GB), and Multi-Layer Perceptron (MLP). The first three were built and analyzed during the development of the RIADA [45], while the last two were created as part of this work. Table 1  
Figure 6: Flow of Azure Durable Function for finding and labeling new songs. shows the results of the different combinations of algorithms and affective quadrants. All those models were trained using repeated five-fold cross-validation with a 70/30 data split and a randomized hyperparameter search. The best combinations are highlighted in green color. As conclusions, Random Forest models offer good accuracy results for the four quadrants and, for this reason, they were selected to be integrated in the process of music emotion recognition. Different causes influence whether the Random Forest models offer those promising recognition results. In general, it has proven to be a model highly flexible and robust. The input dataset was slightly imbalanced (i.e., the number of songs of each of four affective quadrants is different) and it consisted of categorical and continuous features. Random Forest is able to handle more effectively the impact of these two characteristics in the results than the other alternative learning algorithms. With respect to the size of the dataset classes, the use of voting applied to decision trees trained from different data subsets reduces the effects of imbalance. In addition, the decision-making based on trees instead of distance measurement techniques facilitates the handling of the categorical features included in our dataset. On the other hand, the size of the input dataset could lead to overfitting of the resulting models. Although we applied cross-validation to reduce the risk of that overfitting, Random Forest is usually less sensitive to it than the other models. The following analysis of hyperparameters was carried out for the Random Forest models using RandomizedSearchCV from Scikit-learn (similar work was performed for the other models):  
* Number of estimators: 10, 35, 60, 85, 110, 135, 160, 185, 210
* Criterion: gini or entropy
* Minimum samples per leaf: 2, 7, 12
* Minimum samples per split: 2, 5, 10
* Maximum depth of the tree: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
* Bootstrap: true or false  
The selected hyperparameters for each of four Random Forest model were as follows:  
* Sad: bootstrap = False, criterion = entropy, maximum tree depth = 30, minimum samples per leaf = 2, minimum samples per split = 5, number of estimators = 160;
* Happy: bootstrap = False, criterion = entropy, maximum tree depth = 30, minimum samples per leaf = 2, minimum samples per split = 5, number of estimators = 160;
* Angry: bootstrap = False, criterion = gini, maximum tree depth = 90, minimum samples per leaf = 2, minimum samples per split = 2, number of estimators = 185;
* Relaxed: bootstrap = False, criterion = gini, maximum tree depth = 90, minimum samples per leaf = 2, minimum samples per split = 2, number of estimators = 185. \begin{table}
\begin{tabular}{c c c c c c} \hline
**Algorithm** & **Tests** & **Happy** & **Angry** & **Sad** & **Relaxed** \\ \hline \multirow{2}{*}{Random forest} & accuracy & 0.844 & 0.899 & 0.862 & 0.945 \\  & f1 & 0.820 & 0.860 & 0.839 & 0.801 \\ \hline \multirow{2}{*}{SVM} & accuracy & 0.767 & 0.872 & 0.8036 & 0.929 \\  & f1 & 0.752 & 0.821 & 0.783 & 0.733 \\ \hline \multirow{2}{*}{KNN} & accuracy & 0.843 & 0.876 & 0.842 & 0.935 \\  & f1 & 0.822 & 0.824 & 0.816 & 0.784 \\ \hline \multirow{2}{*}{GB} & accuracy & 0.844 & 0.892 & 0.862 & 0.933 \\  & f1 & 0.823 & 0.850 & 0.839 & 0.773 \\ \hline \multirow{2}{*}{MLP} & accuracy & 0.836 & 0.879 & 0.844 & 0.938 \\  & f1 & 0.815 & 0.828 & 0.815 & 0.779 \\ \hline \end{tabular}
\end{table} 
Table 1: Results of the different models.