import os
import numpy as np
from matplotlib import pyplot
from scipy import optimize
# will be used to load MATLAB mat datafile format
from scipy.io import loadmat

#  training data stored in arrays X, y
data = loadmat(os.path.join('Data', 'ex3data1.mat'))
X, y = data['X'], data['y'].ravel()

# set the zero digit to 0, rather than its mapped 10 in this dataset
# This is an artifact due to the fact that this dataset was used in 
# MATLAB where there is no index 0
y[y == 10] = 0

# get number of examples in dataset
m = y.size
# print(m) print(X.shape)

# randomly permute examples, to be used for visualizing one 
# picture at a time
indices = np.random.permutation(m)

# Randomly select 100 data points to display
rand_indices = np.random.choice(m, 100, replace=False)
sel = X[rand_indices, :]

def displayData(X, example_width=None, figsize=(10, 10)):
    """
    Displays 2D data stored in X in a nice grid.
    """
    # Compute rows, cols
    if X.ndim == 2:
        m, n = X.shape
    elif X.ndim == 1:
        n = X.size
        m = 1
        X = X[None]  # Promote to a 2 dimensional array
    else:
        raise IndexError('Input X should be 1 or 2 dimensional.')

    example_width = example_width or int(np.round(np.sqrt(n)))
    example_height = n / example_width

    # Compute number of items to display
    display_rows = int(np.floor(np.sqrt(m)))
    display_cols = int(np.ceil(m / display_rows))

    fig, ax_array = pyplot.subplots(display_rows, display_cols, figsize=figsize)
    fig.subplots_adjust(wspace=0.025, hspace=0.025)

    ax_array = [ax_array] if m == 1 else ax_array.ravel()

    for i, ax in enumerate(ax_array):
        ax.imshow(X[i].reshape(example_width, example_width, order='F'),
                  cmap='Greys', extent=[0, 1, 0, 1])
        ax.axis('off')
# displayData(sel)
# pyplot.show()

# Setup the parameters you will use for this exercise
input_layer_size  = 400  # 20x20 Input Images of Digits
hidden_layer_size = 25   # 25 hidden units
num_labels = 10          # 10 labels, from 0 to 9

# Load the .mat file, which returns a dictionary 
weights = loadmat(os.path.join('Data', 'ex3weights.mat'))

# get the model weights from the dictionary
# Theta1 has size 25 x 401 
# Theta2 has size 10 x 26
Theta1, Theta2 = weights['Theta1'], weights['Theta2']

# swap first and last columns of Theta2, due to legacy from MATLAB indexing, 
# since the weight file ex3weights.mat was saved based on MATLAB indexing
Theta2 = np.roll(Theta2, 1, axis=0)

# X = np.concatenate([np.ones((m, 1)), X], axis=1)
# z2 = np.dot(X, Theta1[1])
# z3 = np.dot(Theta1, X.T)
# print(Theta1[1].shape)

def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))

def predict(Theta1, Theta2, X):
    # Make sure the input has two dimensions
    if X.ndim == 1:
        X = X[None]  # promote to 2-dimensions
    m = X.shape[0]
    num_labels = Theta2.shape[0]
    
    p = np.zeros(X.shape[0])
    X = np.concatenate([np.ones((m, 1)), X], axis=1)
    #=====
    # A = np.zeros((Theta1.shape[0], m))
    # for n in range(Theta1.shape[0]):
    #     A[n] = sigmoid(np.dot(X, Theta1[n]))
    # print(A[0])
    #=====
    a_2 = sigmoid(np.dot(X, Theta1.T))
    a_2 = np.concatenate([np.ones((m, 1)), a_2], axis=1)
    a_3 = sigmoid(np.dot(a_2, Theta2.T))
    for idx in range(m):
        predict_label = np.argmax(a_3[idx,:])
        p[idx] = predict_label
    return p

pred = predict(Theta1, Theta2, X)
print('Training Set Accuracy: {:.1f}%'.format(np.mean(pred == y) * 100))

if indices.size > 0:
    i, indices = indices[0], indices[1:]
    displayData(X[i, :], figsize=(4, 4))
    pyplot.show()
    pred = predict(Theta1, Theta2, X[i, :])
    print('Neural Network Prediction: {}'.format(*pred))
else:
    print('No more images to display!')
8b1bebb86d64eab74a989c8a3897d41d90b0572c
<mask token>


def displayData(X, example_width=None, figsize=(10, 10)):
    """
    Displays 2D data stored in X in a nice grid.
    """
    if X.ndim == 2:
        m, n = X.shape
    elif X.ndim == 1:
        n = X.size
        m = 1
        X = X[None]
    else:
        raise IndexError('Input X should be 1 or 2 dimensional.')
    example_width = example_width or int(np.round(np.sqrt(n)))
    example_height = n / example_width
    display_rows = int(np.floor(np.sqrt(m)))
    display_cols = int(np.ceil(m / display_rows))
    fig, ax_array = pyplot.subplots(display_rows, display_cols, figsize=figsize
        )
    fig.subplots_adjust(wspace=0.025, hspace=0.025)
    ax_array = [ax_array] if m == 1 else ax_array.ravel()
    for i, ax in enumerate(ax_array):
        ax.imshow(X[i].reshape(example_width, example_width, order='F'),
            cmap='Greys', extent=[0, 1, 0, 1])
        ax.axis('off')


<mask token>


def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))


<mask token>

<mask token>


def displayData(X, example_width=None, figsize=(10, 10)):
    """
    Displays 2D data stored in X in a nice grid.
    """
    if X.ndim == 2:
        m, n = X.shape
    elif X.ndim == 1:
        n = X.size
        m = 1
        X = X[None]
    else:
        raise IndexError('Input X should be 1 or 2 dimensional.')
    example_width = example_width or int(np.round(np.sqrt(n)))
    example_height = n / example_width
    display_rows = int(np.floor(np.sqrt(m)))
    display_cols = int(np.ceil(m / display_rows))
    fig, ax_array = pyplot.subplots(display_rows, display_cols, figsize=figsize
        )
    fig.subplots_adjust(wspace=0.025, hspace=0.025)
    ax_array = [ax_array] if m == 1 else ax_array.ravel()
    for i, ax in enumerate(ax_array):
        ax.imshow(X[i].reshape(example_width, example_width, order='F'),
            cmap='Greys', extent=[0, 1, 0, 1])
        ax.axis('off')


<mask token>


def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))


def predict(Theta1, Theta2, X):
    if X.ndim == 1:
        X = X[None]
    m = X.shape[0]
    num_labels = Theta2.shape[0]
    p = np.zeros(X.shape[0])
    X = np.concatenate([np.ones((m, 1)), X], axis=1)
    a_2 = sigmoid(np.dot(X, Theta1.T))
    a_2 = np.concatenate([np.ones((m, 1)), a_2], axis=1)
    a_3 = sigmoid(np.dot(a_2, Theta2.T))
    for idx in range(m):
        predict_label = np.argmax(a_3[idx, :])
        p[idx] = predict_label
    return p


<mask token>
print('Training Set Accuracy: {:.1f}%'.format(np.mean(pred == y) * 100))
if indices.size > 0:
    i, indices = indices[0], indices[1:]
    displayData(X[i, :], figsize=(4, 4))
    pyplot.show()
    pred = predict(Theta1, Theta2, X[i, :])
    print('Neural Network Prediction: {}'.format(*pred))
else:
    print('No more images to display!')

<mask token>
data = loadmat(os.path.join('Data', 'ex3data1.mat'))
X, y = data['X'], data['y'].ravel()
y[y == 10] = 0
m = y.size
indices = np.random.permutation(m)
rand_indices = np.random.choice(m, 100, replace=False)
sel = X[rand_indices, :]


def displayData(X, example_width=None, figsize=(10, 10)):
    """
    Displays 2D data stored in X in a nice grid.
    """
    if X.ndim == 2:
        m, n = X.shape
    elif X.ndim == 1:
        n = X.size
        m = 1
        X = X[None]
    else:
        raise IndexError('Input X should be 1 or 2 dimensional.')
    example_width = example_width or int(np.round(np.sqrt(n)))
    example_height = n / example_width
    display_rows = int(np.floor(np.sqrt(m)))
    display_cols = int(np.ceil(m / display_rows))
    fig, ax_array = pyplot.subplots(display_rows, display_cols, figsize=figsize
        )
    fig.subplots_adjust(wspace=0.025, hspace=0.025)
    ax_array = [ax_array] if m == 1 else ax_array.ravel()
    for i, ax in enumerate(ax_array):
        ax.imshow(X[i].reshape(example_width, example_width, order='F'),
            cmap='Greys', extent=[0, 1, 0, 1])
        ax.axis('off')


input_layer_size = 400
hidden_layer_size = 25
num_labels = 10
weights = loadmat(os.path.join('Data', 'ex3weights.mat'))
Theta1, Theta2 = weights['Theta1'], weights['Theta2']
Theta2 = np.roll(Theta2, 1, axis=0)


def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))


def predict(Theta1, Theta2, X):
    if X.ndim == 1:
        X = X[None]
    m = X.shape[0]
    num_labels = Theta2.shape[0]
    p = np.zeros(X.shape[0])
    X = np.concatenate([np.ones((m, 1)), X], axis=1)
    a_2 = sigmoid(np.dot(X, Theta1.T))
    a_2 = np.concatenate([np.ones((m, 1)), a_2], axis=1)
    a_3 = sigmoid(np.dot(a_2, Theta2.T))
    for idx in range(m):
        predict_label = np.argmax(a_3[idx, :])
        p[idx] = predict_label
    return p


pred = predict(Theta1, Theta2, X)
print('Training Set Accuracy: {:.1f}%'.format(np.mean(pred == y) * 100))
if indices.size > 0:
    i, indices = indices[0], indices[1:]
    displayData(X[i, :], figsize=(4, 4))
    pyplot.show()
    pred = predict(Theta1, Theta2, X[i, :])
    print('Neural Network Prediction: {}'.format(*pred))
else:
    print('No more images to display!')

import os
import numpy as np
from matplotlib import pyplot
from scipy import optimize
from scipy.io import loadmat
data = loadmat(os.path.join('Data', 'ex3data1.mat'))
X, y = data['X'], data['y'].ravel()
y[y == 10] = 0
m = y.size
indices = np.random.permutation(m)
rand_indices = np.random.choice(m, 100, replace=False)
sel = X[rand_indices, :]


def displayData(X, example_width=None, figsize=(10, 10)):
    """
    Displays 2D data stored in X in a nice grid.
    """
    if X.ndim == 2:
        m, n = X.shape
    elif X.ndim == 1:
        n = X.size
        m = 1
        X = X[None]
    else:
        raise IndexError('Input X should be 1 or 2 dimensional.')
    example_width = example_width or int(np.round(np.sqrt(n)))
    example_height = n / example_width
    display_rows = int(np.floor(np.sqrt(m)))
    display_cols = int(np.ceil(m / display_rows))
    fig, ax_array = pyplot.subplots(display_rows, display_cols, figsize=figsize
        )
    fig.subplots_adjust(wspace=0.025, hspace=0.025)
    ax_array = [ax_array] if m == 1 else ax_array.ravel()
    for i, ax in enumerate(ax_array):
        ax.imshow(X[i].reshape(example_width, example_width, order='F'),
            cmap='Greys', extent=[0, 1, 0, 1])
        ax.axis('off')


input_layer_size = 400
hidden_layer_size = 25
num_labels = 10
weights = loadmat(os.path.join('Data', 'ex3weights.mat'))
Theta1, Theta2 = weights['Theta1'], weights['Theta2']
Theta2 = np.roll(Theta2, 1, axis=0)


def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))


def predict(Theta1, Theta2, X):
    if X.ndim == 1:
        X = X[None]
    m = X.shape[0]
    num_labels = Theta2.shape[0]
    p = np.zeros(X.shape[0])
    X = np.concatenate([np.ones((m, 1)), X], axis=1)
    a_2 = sigmoid(np.dot(X, Theta1.T))
    a_2 = np.concatenate([np.ones((m, 1)), a_2], axis=1)
    a_3 = sigmoid(np.dot(a_2, Theta2.T))
    for idx in range(m):
        predict_label = np.argmax(a_3[idx, :])
        p[idx] = predict_label
    return p


pred = predict(Theta1, Theta2, X)
print('Training Set Accuracy: {:.1f}%'.format(np.mean(pred == y) * 100))
if indices.size > 0:
    i, indices = indices[0], indices[1:]
    displayData(X[i, :], figsize=(4, 4))
    pyplot.show()
    pred = predict(Theta1, Theta2, X[i, :])
    print('Neural Network Prediction: {}'.format(*pred))
else:
    print('No more images to display!')

import os
import numpy as np
from matplotlib import pyplot
from scipy import optimize
# will be used to load MATLAB mat datafile format
from scipy.io import loadmat

#  training data stored in arrays X, y
data = loadmat(os.path.join('Data', 'ex3data1.mat'))
X, y = data['X'], data['y'].ravel()

# set the zero digit to 0, rather than its mapped 10 in this dataset
# This is an artifact due to the fact that this dataset was used in 
# MATLAB where there is no index 0
y[y == 10] = 0

# get number of examples in dataset
m = y.size
# print(m) print(X.shape)

# randomly permute examples, to be used for visualizing one 
# picture at a time
indices = np.random.permutation(m)

# Randomly select 100 data points to display
rand_indices = np.random.choice(m, 100, replace=False)
sel = X[rand_indices, :]

def displayData(X, example_width=None, figsize=(10, 10)):
    """
    Displays 2D data stored in X in a nice grid.
    """
    # Compute rows, cols
    if X.ndim == 2:
        m, n = X.shape
    elif X.ndim == 1:
        n = X.size
        m = 1
        X = X[None]  # Promote to a 2 dimensional array
    else:
        raise IndexError('Input X should be 1 or 2 dimensional.')

    example_width = example_width or int(np.round(np.sqrt(n)))
    example_height = n / example_width

    # Compute number of items to display
    display_rows = int(np.floor(np.sqrt(m)))
    display_cols = int(np.ceil(m / display_rows))

    fig, ax_array = pyplot.subplots(display_rows, display_cols, figsize=figsize)
    fig.subplots_adjust(wspace=0.025, hspace=0.025)

    ax_array = [ax_array] if m == 1 else ax_array.ravel()

    for i, ax in enumerate(ax_array):
        ax.imshow(X[i].reshape(example_width, example_width, order='F'),
                  cmap='Greys', extent=[0, 1, 0, 1])
        ax.axis('off')
# displayData(sel)
# pyplot.show()

# Setup the parameters you will use for this exercise
input_layer_size  = 400  # 20x20 Input Images of Digits
hidden_layer_size = 25   # 25 hidden units
num_labels = 10          # 10 labels, from 0 to 9

# Load the .mat file, which returns a dictionary 
weights = loadmat(os.path.join('Data', 'ex3weights.mat'))

# get the model weights from the dictionary
# Theta1 has size 25 x 401 
# Theta2 has size 10 x 26
Theta1, Theta2 = weights['Theta1'], weights['Theta2']

# swap first and last columns of Theta2, due to legacy from MATLAB indexing, 
# since the weight file ex3weights.mat was saved based on MATLAB indexing
Theta2 = np.roll(Theta2, 1, axis=0)

# X = np.concatenate([np.ones((m, 1)), X], axis=1)
# z2 = np.dot(X, Theta1[1])
# z3 = np.dot(Theta1, X.T)
# print(Theta1[1].shape)

def sigmoid(z):
    return 1.0 / (1.0 + np.exp(-z))

def predict(Theta1, Theta2, X):
    # Make sure the input has two dimensions
    if X.ndim == 1:
        X = X[None]  # promote to 2-dimensions
    m = X.shape[0]
    num_labels = Theta2.shape[0]
    
    p = np.zeros(X.shape[0])
    X = np.concatenate([np.ones((m, 1)), X], axis=1)
    #=====
    # A = np.zeros((Theta1.shape[0], m))
    # for n in range(Theta1.shape[0]):
    #     A[n] = sigmoid(np.dot(X, Theta1[n]))
    # print(A[0])
    #=====
    a_2 = sigmoid(np.dot(X, Theta1.T))
    a_2 = np.concatenate([np.ones((m, 1)), a_2], axis=1)
    a_3 = sigmoid(np.dot(a_2, Theta2.T))
    for idx in range(m):
        predict_label = np.argmax(a_3[idx,:])
        p[idx] = predict_label
    return p

pred = predict(Theta1, Theta2, X)
print('Training Set Accuracy: {:.1f}%'.format(np.mean(pred == y) * 100))

if indices.size > 0:
    i, indices = indices[0], indices[1:]
    displayData(X[i, :], figsize=(4, 4))
    pyplot.show()
    pred = predict(Theta1, Theta2, X[i, :])
    print('Neural Network Prediction: {}'.format(*pred))
else:
    print('No more images to display!')