<jupyter_start><jupyter_text><jupyter_code>import librosa
%matplotlib inline
import matplotlib.pyplot as plt
import librosa.display
from IPython.display import Audio
import numpy as np
import tensorflow as tf
from matplotlib.pyplot import specgram
import pandas as pd
from sklearn.metrics import confusion_matrix
import IPython.display as ipd  # To play sound in the notebook
import os # interface with underlying OS that python is running on
import sys
import warnings
# ignore warnings 
if not sys.warnoptions:
    warnings.simplefilter("ignore")
warnings.filterwarnings("ignore", category=DeprecationWarning)
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import tensorflow.keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv1D, MaxPooling1D, AveragePooling1D
from tensorflow.keras.layers import Input, Flatten, Dropout, Activation, BatchNormalization, Dense
from sklearn.model_selection import GridSearchCV
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.regularizers import l2
import seaborn as sns
import keras
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.keras.utils import to_categorical
from sklearn.metrics import classification_report
from google.colab import drive
drive.mount('/content/drive')
k = pd.read_csv('/content/drive/MyDrive/mfcc_iemocap/folder1/mfcc (2).csv')
k.columns
y = k.drop(columns={'gender','emotion'})
y.mel_spectrogram.replace('[','')
y['mel_spectrogram'] =  y['mel_spectrogram'].apply(lambda x: x.replace('[','').replace(']','')) 
y
y.replace('\s+', ', ',regex=True,inplace=True)
y
y.to_csv('comma_mfcc.csv', index=False)
y['mel_spectrogram']
y = y.mel_spectrogram.str.split(', ', expand=True)
y
n=np.array(y)
t=n[:,0:40]
t
p=pd.DataFrame(t)
p.to_csv('mfcc_iemocap.csv', index=False)
l = pd.read_csv('/content/mfcc_iemocap.csv')
l
m=pd.get_dummies(k['gender'],drop_first=True)
m
x=pd.concat([l,m],axis=1)
x
y=k['emotion']
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3)<jupyter_output><empty_output><jupyter_text>**SVM with Different Kernals**<jupyter_code>from sklearn.svm import SVC
classifier=SVC(kernel="linear")
classifier.fit(x_train,y_train)
y_predict=classifier.predict(x_test)
from sklearn.metrics import accuracy_score,f1_score,classification_report
accuracy=accuracy_score(y_test,y_predict)
f1_sc=f1_score(y_test,y_predict,average='micro')
repo=classification_report(y_test,y_predict)
repo

accuracy
from sklearn.svm import SVC
classifier=SVC(kernel="poly")
classifier.fit(x_train,y_train)
y_predict=classifier.predict(x_test)
from sklearn.metrics import accuracy_score,f1_score,classification_report
accuracy=accuracy_score(y_test,y_predict)
f1_sc=f1_score(y_test,y_predict,average='micro')
repo=classification_report(y_test,y_predict)
repo
accuracy
from sklearn.svm import SVC
classifier=SVC(kernel="rbf")
classifier.fit(x_train,y_train)
y_predict=classifier.predict(x_test)
from sklearn.metrics import accuracy_score,f1_score,classification_report
accuracy=accuracy_score(y_test,y_predict)
f1_sc=f1_score(y_test,y_predict,average='micro')
repo=classification_report(y_test,y_predict)
repo

accuracy<jupyter_output><empty_output><jupyter_text>**RandomForestClassifier**<jupyter_code>from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score,f1_score,classification_report
accuracy=[]
f1_sc=[]
rang=[175,180,185,450,500,550,600,650,700,750,800,850,900,950,1000,1050]
for i in rang:
  ran=RandomForestClassifier(n_estimators=i)
  ran.fit(x_train,y_train)
  y_pre=ran.predict(x_test)
  accuracy.append(accuracy_score(y_test,y_pre))
  f1_sc.append(f1_score(y_test,y_pre,average='micro'))
np.max(accuracy)
np.max(f1_sc)
plt.figure(figsize=(10,6))
plt.plot(rang,accuracy,marker='o')
plt.title('accuracy vs n_estimators')
plt.xlabel('number of decision trees')
plt.ylabel('accuracy')
plt.figure(figsize=(10,6))
plt.plot(rang,f1_sc,marker='o')
plt.title('accuracy vs n_estimators')
plt.xlabel('number of decision trees')
plt.ylabel('f1_score')<jupyter_output><empty_output><jupyter_text>**Classification Report for 600 trees**<jupyter_code>ran=RandomForestClassifier(n_estimators=850)
ran.fit(x_train,y_train)
y_pre=ran.predict(x_test)
repo=classification_report(y_test,y_pre)
repo
from sklearn.externals import joblib
joblib.dump(ran,'RandomForest1')

x_test
x_test.to_csv('x_test(modified)', index=False)
y_test
y_test.to_csv('y_test(modified)', index=False)
x_test[0:1:]
rf=joblib.load('RandomForest1')
rf.predict(x_test[1:2:])
rf=joblib.load('RandomForest1(modified)')
rf.predict(x_test[1:2:])<jupyter_output><empty_output><jupyter_text>**KNN**<jupyter_code>from sklearn.neighbors import KNeighborsClassifier 
accuracy=[]
f1_sc=[]
from sklearn.metrics import accuracy_score,f1_score,classification_report



for i in range(1,50):
  knn=KNeighborsClassifier(n_neighbors=i)
  knn.fit(x_train,y_train)
  pred_i=knn.predict(x_test)
  acc=accuracy_score(y_test,pred_i)
  f1=f1_score(y_test,pred_i,average='micro')
  accuracy.append(acc)
  f1_sc.append(f1)<jupyter_output><empty_output><jupyter_text>**Classification Report for K=1**<jupyter_code>knn=KNeighborsClassifier(n_neighbors=1)
knn.fit(x_train,y_train)
pred_y=knn.predict(x_test)
repo=classification_report(y_test,pred_y)
repo

plt.figure(figsize=(10,6))
plt.plot(range(1,50),accuracy,marker='o',)
plt.title('knn for different k')
plt.xlabel('value of k')
plt.ylabel('accuracy')
plt.figure(figsize=(10,6))
plt.plot(range(1,50),f1_sc,marker='o')
plt.title('knn for different k')
plt.xlabel('value of k')
plt.ylabel('f1_score')
np.max(accuracy)
np.max(f1_sc)<jupyter_output><empty_output><jupyter_text>**LogisticRegression**<jupyter_code>from sklearn.linear_model import LogisticRegression
logmodel=LogisticRegression()
logmodel.fit(x_train,y_train)

from sklearn.metrics import accuracy_score,classification_report
pred_y=logmodel.predict(x_test)
accuracy=accuracy_score(y_test,pred_y)
repo=classification_report(y_test,pred_y)
repo

y1=pd.get_dummies(k['emotion'])
x1=x
x_train,x_test,y_train,y_test=train_test_split(x1,y1,test_size=0.3)
y_train
x_train

import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LeakyReLU,PReLU,ELU
from keras.layers import Dropout
classifier=Sequential()
classifier.add(Dense(units=40,kernel_initializer='he_uniform',activation='relu',input_dim=41))
classifier.add(Dense(units=40,kernel_initializer='he_uniform',activation='relu'))
classifier.add(Dense(units=40,kernel_initializer='he_uniform',activation='relu'))
classifier.add(Dense(units=40,kernel_initializer='he_uniform',activation='relu'))
classifier.add(Dense(units=40,kernel_initializer='he_uniform',activation='relu'))
classifier.add(Dense(units=8,kernel_initializer='glorot_uniform',activation='sigmoid'))
classifier.compile(optimizer='Adamax',loss='categorical_crossentropy',metrics=['Accuracy'])


model=classifier.fit(x_train,y_train,validation_split=0.33,batch_size=10,epochs=100)<jupyter_output>Epoch 1/100
68/68 [==============================] - 1s 9ms/step - loss: 41.7986 - accuracy: 0.0943 - val_loss: 4.9196 - val_accuracy: 0.1062
Epoch 2/100
68/68 [==============================] - 0s 5ms/step - loss: 4.4654 - accuracy: 0.1112 - val_loss: 3.4459 - val_accuracy: 0.1149
Epoch 3/100
68/68 [==============================] - 0s 5ms/step - loss: 3.6372 - accuracy: 0.1131 - val_loss: 4.7409 - val_accuracy: 0.1149
Epoch 4/100
68/68 [==============================] - 0s 5ms/step - loss: 3.7040 - accuracy: 0.1123 - val_loss: 3.2772 - val_accuracy: 0.1119
Epoch 5/100
68/68 [==============================] - 0s 5ms/step - loss: 3.4911 - accuracy: 0.1135 - val_loss: 3.3285 - val_accuracy: 0.1141
Epoch 6/100
68/68 [==============================] - 0s 5ms/step - loss: 3.2514 - accuracy: 0.1138 - val_loss: 2.8260 - val_accuracy: 0.1100
Epoch 7/100
68/68 [==============================] - 0s 5ms/step - loss: 3.2209 - accuracy: 0.1140 - val_loss: 4.1125 - val_accuracy: 0.1130
Epoch 8/100
[...]
{'fraction_non_alphanumeric': 0.1533284385707293, 'fraction_numerical': 0.044909446891825744, 'mean_word_length': 9.06527093596059, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, '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': '610356', 'license': 'Unlicense', 'n_tokens_mistral': 3346, 'n_tokens_neox': 3072, 'n_words': 478, 'path': 'SVM--RandomForest--LogisticRegression--KNN_mfcc_iemocap.ipynb', 'repo_name': 'jatinkchaudhary/Speech-Processing-Final-Year-Project-', 'size': '8172'}