In [34]:
import numpy as np
import pandas as pd
import scipy.io as sio
import matplotlib.pyplot as plt
from sklearn.utils import shuffle

%matplotlib inline

First, let's unpack the data set from the ex4data1.mat, the data is available on the coursera site for the machine learning class https://www.coursera.org/learn/machine-learning tought by Andrew NG lecture 4. Also there is a number of clones that have this data file.


In [35]:
data = pd.read_csv('fer2013/fer2013.csv')
df = shuffle(df)

In [36]:
X = data['pixels']
y = data['emotion']

In [37]:
X = pd.Series([np.array(x.split()).astype(int) for x in X])
# convert one column as list of ints into dataframe where each item in array is a column
X = pd.DataFrame(np.matrix(X.tolist()))

In [5]:
df = pd.DataFrame(y)
df.loc[:,'f'] = pd.Series(-1, index=df.index)
df.groupby('emotion').count()


Out[5]:
f
emotion
0 4953
1 547
2 5121
3 8989
4 6077
5 4002
6 6198

In [6]:
# This function plots the given sample set of images as a grid with labels 
# if labels are available.
def plot_sample(S,w=48,h=48,labels=None):
    m = len(S);
  
    # Compute number of items to display
    display_rows = int(np.floor(np.sqrt(m)));
    display_cols = int(np.ceil(m / display_rows));
   
    fig = plt.figure()
    S = S.as_matrix()
    for i in range(0,m):
        arr = S[i,:]
        arr = arr.reshape((w,h))
        ax = fig.add_subplot(display_rows,display_cols , i+1)
        ax.imshow(arr, aspect='auto', cmap=plt.get_cmap('gray'))
        if labels is not None:
            ax.text(0,0, '{}'.format(labels[i]), bbox={'facecolor':'white', 'alpha':0.8,'pad':2})
        ax.axis('off')
    plt.show()
    
    print ('0=Angry', '1=Disgust', '2=Fear', '3=Happy', '4=Sad', '5=Surprise', '6=Neutral')

In [38]:
samples = X.sample(16)
plot_sample(samples,48,48,y[samples.index].as_matrix())


0=Angry 1=Disgust 2=Fear 3=Happy 4=Sad 5=Surprise 6=Neutral

Now, let use the Neural Network with 1 hidden layers. The number of neurons in each layer is X_train.shape[1] which is 400 in our example (excluding the extra bias unit).


In [43]:
from sklearn.neural_network import MLPClassifier
from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
from sklearn.metrics import roc_curve, auc
from sklearn.preprocessing import label_binarize

# CALC AUC_ROC, binarizing each lable
y_b = pd.DataFrame(label_binarize(y, classes=[0,1,2,3,4,5,6]))
n_classes = y_b.shape[1]

# since the data we have is one big array, we want to split it into training
# and testing sets, the split is 70% goes to training and 30% of data for testing
X_train, X_test, y_train, y_test = train_test_split(X, y_b, test_size=0.3)
neural_network =(100,)

In [45]:
clfs ={}
for a in [1,0.1,1e-2,1e-3,1e-4,1e-5]:
    # for this excersize we are using MLPClassifier with lbfgs optimizer (the family of quasi-Newton methods). In my simple
    # experiments it produces good quality outcome
    clf = MLPClassifier( alpha=a, hidden_layer_sizes=neural_network, random_state=1)
    clf.fit(X_train, y_train)

    # So after the classifier is trained, lets see what it predicts on the test data
    prediction = clf.predict(X_test)
       
    # Compute ROC curve and ROC area for each class
    fpr = dict()
    tpr = dict()
    roc_auc = dict()
    for i in range(n_classes):
        fpr[i], tpr[i], _ = roc_curve(y_test.as_matrix()[:,i], prediction[:,i])
        roc_auc[i] = auc(fpr[i], tpr[i])

    # Compute micro-average ROC curve and ROC area
    fpr["micro"], tpr["micro"], _ = roc_curve(y_test.as_matrix().ravel(), prediction.ravel())
    roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])
    
    print ("ROC_AUC (micro) score is {:.04f} with alpha {}".format(roc_auc["micro"], a))
    clfs[a] = clf


ROC_AUC (micro) score is 0.5000 with alpha 1
ROC_AUC (micro) score is 0.5347 with alpha 0.1
ROC_AUC (micro) score is 0.5000 with alpha 0.01
ROC_AUC (micro) score is 0.5418 with alpha 0.001
ROC_AUC (micro) score is 0.5400 with alpha 0.0001
ROC_AUC (micro) score is 0.5002 with alpha 1e-05

In [48]:
samples = X_test.sample(16)
p = clfs.get(0.001).predict(samples)
plot_sample(samples,48,48,[x.argmax(axis=0) for x in p])


0=Angry 1=Disgust 2=Fear 3=Happy 4=Sad 5=Surprise 6=Neutral

In [49]:
p=y_test.loc[samples.index].as_matrix()
plot_sample(samples,48,48,[x.argmax(axis=0) for x in p])


0=Angry 1=Disgust 2=Fear 3=Happy 4=Sad 5=Surprise 6=Neutral

In [ ]: