<jupyter_start><jupyter_text>Reading data<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import pickle

from sklearn.metrics import accuracy_score
from sklearn.utils import shuffle

from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegressionCV
from sklearn.ensemble import RandomForestClassifier

from sklearn.model_selection import GridSearchCV, train_test_split, cross_val_score
data = pd.read_csv("../data/Emotion_Data_v2.csv")
emotions = {'neutral': 0,
            'calm': 1,
            'happy': 2, 
            'sad': 3, 
            'angry': 4, 
            'fearful': 5, 
            'disgust': 6,
            'surprised': 7, 
            'boredom': 8}
data["Emotion"] = data["Emotion"].map(emotions)
names = data["File Name"]
data = data.drop("File Name", axis = 1)
data
y = data["Emotion"]
data = data.drop("Emotion", axis = 1)
X_train, X_test, y_train, y_test = train_test_split(data, y, test_size = 0.3)
X_train.shape, X_test.shape, y_train.shape, y_test.shape<jupyter_output><empty_output><jupyter_text>Judging by the TSNE Random Forest and Logistic Regression may work nice. Also, let's check KNN  KNN<jupyter_code>knn = KNeighborsClassifier(n_neighbors = 103, n_jobs = -1)
# params = {
#     "n_neighbors": np.arange(15, 151, 2)
# }
# knn = GridSearchCV(knn, params, n_jobs = -1, verbose = 3)
knn.fit(X_train, y_train)
# knn.best_params_
accuracy_score(knn.predict(X_test), y_test)<jupyter_output><empty_output><jupyter_text>Not bad for KNN, but think that other models will work better  Random Forest<jupyter_code>rf = RandomForestClassifier(n_estimators = 201, oob_score = True, n_jobs = -1, random_state = 11, \
                            max_depth = 15, verbose = 3)
# params = {
#     "n_estimators": [101, 201, 301],
#     "max_depth": np.arange(12, 20),
#     "min_samples_split": np.arange(5, 9)
#     "min_samples_leaf": np.arange(3, 7),
# }
# 
# rf = GridSearchCV(rf, params, n_jobs = -1, verbose = 3)
cross_val_score(rf, X_train, y_train)
rf.fit(X_train, y_train)
np.arange(5, 12)
# rf.best_params_
accuracy_score(rf.predict(X_test), y_test)
coefs = pd.DataFrame({"feature": X_train.columns,
             "coef": rf.feature_importances_}).sort_values(by = "coef", ascending = False)
coefs
sns.scatterplot(x = coefs.index, y = coefs["coef"]);<jupyter_output><empty_output><jupyter_text>We see kinda uniform distribution, but all coefs are near to 0. That's nice, but accuracy is not the best  Logistic Regression without scaling<jupyter_code>logit = LogisticRegressionCV(Cs = [10, 5, 1, 0.1, 0.01, 0.001], class_weight = "balanced", \
                             random_state = 11, verbose = 3, n_jobs = -1)
logit.fit(X_train, y_train)
logit.C_
accuracy_score(logit.predict(X_test), y_test)
coefs = pd.DataFrame({"feature": X_train.columns,
             "coef": logit.coef_[0]}).sort_values(by = "coef", ascending = False)
coefs
sns.scatterplot(x = coefs.index, y = coefs["coef"]);<jupyter_output><empty_output><jupyter_text>0s. Need to use scaling  Logistic Regression with scaling<jupyter_code>from sklearn.preprocessing import StandardScaler
# logit = LogisticRegressionCV(class_weight = "balanced", random_state = 11, verbose = 3, n_jobs = -1)
# params = {
#     "C": [10, 5, 1, 0.1, 0.01, 0.001]
# }
# logit = GridSearchCV(logit, params, n_jobs = -1, verbose = 3)
logit = LogisticRegressionCV(Cs = [10, 5, 1, 0.1, 0.01, 0.001], class_weight = "balanced", \
                             random_state = 11, verbose = 3, n_jobs = -1)
logit.fit(StandardScaler().fit_transform(X_train), y_train)
accuracy_score(logit.predict(StandardScaler().fit_transform(X_test)), y_test)
coefs = pd.DataFrame({"feature": X_train.columns,
             "coef": logit.coef_[0]}).sort_values(by = "coef", ascending = False)
coefs
sns.scatterplot(x = coefs.index, y = coefs["coef"]);<jupyter_output><empty_output><jupyter_text>Feature importance is lower, better to use Random Forest  MLP<jupyter_code># net = MLPClassifier(hidden_layer_sizes = tuple([50] * 10), alpha = 0.01, learning_rate = "constant", \
#                     random_state = 11, verbose = 10)
# params = {
#     "alpha": [0.0001, 0.001, 0.01, 0.1, 1]
# }

# net = GridSearchCV(net, params, n_jobs = -1, verbose = 10)
# net.fit(X_train, y_train)
# accuracy_score(net.predict(X_test), y_test)
# pd.DataFrame({"feature": X_train.columns,
#              "coef": np.mean(net.best_estimator_.coefs_[0])})<jupyter_output><empty_output><jupyter_text>SVM<jupyter_code>from sklearn.svm import SVC
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
svc = SVC(verbose = 5, random_state = 11)

params = {"C": np.arange(8.5, 9.5, 0.1)}

svc = GridSearchCV(svc, params, n_jobs = -1)
svc.fit(X_train_scaled, y_train)
svc.best_params_
accuracy_score(svc.predict(X_test), y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13817519724863445, 'fraction_numerical': 0.031155168925753592, 'mean_word_length': 4.333333333333333, 'pattern_counts': {'":': 16, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '42304', 'license': 'MIT', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1854, 'n_words': 397, 'path': 'model/main.ipynb', 'repo_name': 'ReAlex1902/Emotion_recognition', 'size': '4943'}