<jupyter_start><jupyter_text>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 <NAME> lecture 4. Also there is a number of clones that have this data file.<jupyter_code>data = pd.read_csv('fer2013/fer2013.csv')
df = shuffle(df)
X = data['pixels']
y = data['emotion']
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()))
df = pd.DataFrame(y)
df.loc[:,'f'] = pd.Series(-1, index=df.index)
df.groupby('emotion').count()
# 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')
samples = X.sample(16)
plot_sample(samples,48,48,y[samples.index].as_matrix())<jupyter_output><empty_output><jupyter_text>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).<jupyter_code>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,)
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
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])
p=y_test.loc[samples.index].as_matrix()
plot_sample(samples,48,48,[x.argmax(axis=0) for x in p])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13663781799108313, 'fraction_numerical': 0.023078940466824023, 'mean_word_length': 4.311977715877437, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '523168', 'license': 'MIT', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1364, 'n_words': 379, 'path': 'face_detect/MLPClassifier.ipynb', 'repo_name': 'xsolo/machine-learning', 'size': '3816'}