一、引入库和数据集
import numpy as np
import scipy.io as io
import matplotlib.pyplot as plt
data=io.loadmat('C:/Users/Administrator/Desktop/新建文件夹/ex3data1.mat') #type(data)-----dict
data.keys() #字典data的键有哪些,以列表的形式给出
#dict_keys(['__header__', '__version__', '__globals__', 'X', 'y'])
X=data['X'] #字典data对应键为'X'的值
y=data['y']
X.shape #(5000,400)
y.shape #(5000, 1)
np.unique(y)
#array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype=uint8)
二、数据可视化
def plot_100_images():
random_sample=np.random.choice(range(0,5000),100) #range(X.shape[0])
images=X[random_sample,:] #(100,10)
y_images=y.ravel()[random_sample]
fig,ax_array=plt.subplots(figsize=(8,8),nrows=10,ncols=10,sharex=True,sharey=True)
for row in range(10):
for col in range(10):
ax_array[row,col].matshow((images[row*10+col].reshape(20,20)).T)
plt.xticks([])
plt.yticks([])
plt.show()
三、引入权重
data2=io.loadmat('C:/Users/Administrator/Desktop/新建文件夹/ex3weights.mat')
data2.keys()
#dict_keys(['__header__', '__version__', '__globals__', 'Theta1', 'Theta2'])
theta1=data2['Theta1']
theta2=data2['Theta2']
theta1.shape #(25,401)
theta2.shape #(10,26)
X=np.insert(X,0,1,axis=1) #(5000,401)
y=y.ravel() #(5000,)
四、用神经网络对已经训练好的权重进行预测
1.前向传播
def sigmoid(z):
return 1/(1+np.exp(-z))
a1=X #(5000,401)
z2=a1@theta1.T #(5000,25)
a2=sigmoid(z2) #(#(5000,25))
'''增加偏置项'''
a2=np.insert(a2,0,1,axis=1) #(5000,26)
z3=a2@theta2.T #(5000,10)
a3=sigmoid(z3) #(5000,10)
2.预测
y_argmax=np.argmax(a3,axis=1)
y_pred=y_argmax+1
accuracy=np.mean(y_pred==y)
print('accuracy={}'.format(accuracy))
#accuracy=0.9752
可以发现,神经网络的预测精度要比logistic回归多分类的预测精度高!