吴恩达机器学习python代码练习三(向前传递神经网络)

import numpy as np
import scipy.io as sio
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import classification_report#这个包是评价报告

吴恩达机器学习python代码练习三(向前传递神经网络)_第1张图片
吴恩达机器学习python代码练习三(向前传递神经网络)_第2张图片

path = 'E:\吴恩达及遗传算法\机器学习\Machine_Learning_AndrewNg-master\Machine_Learning_AndrewNg-master\machine-learning-ex3\ex3data1.mat'
path_weights = 'E:\吴恩达及遗传算法\机器学习\Machine_Learning_AndrewNg-master\Machine_Learning_AndrewNg-master\machine-learning-ex3\ex3weights.mat'
data = sio.loadmat(path)#loadmat返回的结果是一个字典,其中变量名字作为keys,内部矩阵作为对应的values
data_weights = sio.loadmat(path_weights)
data.keys(),data_weights.keys(),data_weights['Theta1'].shape
(dict_keys(['__header__', '__version__', '__globals__', 'X', 'y']),
 dict_keys(['__header__', '__version__', '__globals__', 'Theta1', 'Theta2']),
 (25, 401))
x_read = data['X']
y_read = data['y']
y_read = y_read.ravel()
x_read_insert = np.insert(x_read,0,1,axis=1)#插入偏置项
x_insert_T = x_read_insert.T
theta1 = data_weights['Theta1']
theta2 = data_weights['Theta2']
x_read_insert.shape,y_read.shape
((5000, 401), (5000,))
def sigmoid(z):
    return 1/(1+np.exp(-z))
a1 = theta1.dot(x_insert_T)
z1 = sigmoid(a1)
z1_insert = np.insert(z1,0,1,axis = 0)
a1.shape,theta1.shape,theta2.shape,z1_insert.shape
((25, 5000), (25, 401), (10, 26), (26, 5000))
a2 = theta2.dot(z1_insert)
z2 = sigmoid(a2)
z2.T.shape
(5000, 10)
y_predict = np.argmax(z2.T,axis=1) + 1#预测
y_predict.shape
(5000,)
print(classification_report(y_read,y_predict))
              precision    recall  f1-score   support

           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
          10       0.98      0.99      0.99       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

```结果来看比多类别分类计算的更为准确。

你可能感兴趣的:(机器学习)