import numpy as np
import scipy.io as sio
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import classification_report
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)
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
```结果来看比多类别分类计算的更为准确。