<jupyter_start><jupyter_text>Feedfoward PropagationRecognize the handwritten digits (from 0 to 9) by using neural network.This section aims to implement the feedfoward propagation algorithm to use trained weights to predict.<jupyter_code>import numpy as np
from scipy.io import loadmat
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>1 Load data and parametersTrained weights of neural network are stored in **ex3weights.mat**.<jupyter_code>def load_parameter(path):
    data = loadmat(path)
    return data['Theta1'], data['Theta2']
theta_1, theta_2 = load_parameter("ex3weights.mat")
print(theta_1.shape)
print(theta_2.shape)<jupyter_output>(25, 401)
(10, 26)<jupyter_text>Training examples are stored in **ex3data1.mat**.<jupyter_code>def load_data(path):
    data = loadmat(path)
    return data['X'], data['y'].ravel()
X, y = load_data("ex3data1.mat")
print(X.shape)
print(y.shape)<jupyter_output>(5000, 400)
(5000,)<jupyter_text>2 Model representationThe neural network model is shown as follow.  3 Feedfoward propagation and predict<jupyter_code>def sigmoid(z):
    return 1 / (np.exp(-z) + 1)
def foward_propagation(weights, X):
    a = X
    
    for theta in weights:
        a = np.insert(a, 0, values=np.ones(a.shape[0]), axis=1)
        z = a @ theta.T
        a = sigmoid(z)
        
    return a
def predict(res):
    pred = np.argmax(res, axis=1) + 1
    pred[pred == 10] = 0
    return pred
res = foward_propagation((theta_1, theta_2), X)
y_pred = predict(res)
y_pred
y[y == 10] = 0
y_ans = y
y
print(classification_report(y_ans, y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.98      0.99      0.99       500
           1       0.97      0.98      0.98       500
           2       0.98      0.97      0.98       500
           3       0.98      0.96      0.97       500
           4       0.97      0.97      0.97       500
           5       0.97      0.98      0.98       500
           6       0.98      0.99      0.98       500
           7       0.98      0.97      0.97       500
           8       0.98      0.98      0.98       500
           9       0.97      0.96      0.96       500

   micro avg       0.98      0.98      0.98      5000
   macro avg       0.98      0.98      0.98      5000
weighted avg       0.98      0.98      0.98      5000
{'fraction_non_alphanumeric': 0.0970954356846473, 'fraction_numerical': 0.09211618257261411, 'mean_word_length': 2.01375, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '92471', 'license': 'MIT', 'n_tokens_mistral': 939, 'n_tokens_neox': 825, 'n_words': 225, 'path': 'exercise/ex3_multi_classification/2_feedfoward_propagation.ipynb', 'repo_name': 'Zx55/Coursera-AndrewNg-ML', 'size': '2410'}