作业三 多分类和神经网络

这篇博客展示了如何运用逻辑回归和神经网络解决多分类问题。首先,通过逻辑回归实现了一对多分类,然后用神经网络进行分类,并分别计算了它们的预测准确性。在逻辑回归中,定义了sigmoid激活函数、代价函数和梯度下降;在神经网络部分,实现了前向传播和反向传播。最后,两个模型都在MNIST数据集上进行了测试,得到了接近98%的准确率。
摘要由CSDN通过智能技术生成

多分类法

#逻辑回归解决多分类问题
import numpy as np
import matplotlib.pyplot as plt
import scipy.io as sio
from scipy.optimize import minimize#优化函数

data = sio.loadmat('D:\桌面\Coursera-ML-AndrewNg-Notes-master\code\ex3-neural network\ex3data1.mat')
#print(data)#字典格式的数据

def plot_an_image(x):#选取一张图片
    pick_one = np.random.randint(5000)
    image = x[pick_one,:]
    fig,ax = plt.subplots(figsize = (1,1))
    ax.imshow(image.reshape(20,20).T,cmap='gray_r')
    plt.xticks([])
    plt.yticks([])#去掉x,y轴的刻度
    plt.show()

def plot_100_image(x):
    sample_index = np.random.choice(5000,100)
    images = x[sample_index,:]
    fig,ax = plt.subplots(ncols=10,nrows=10,figsize=(8,8),sharex=True,sharey=True)

    for i in range(10):
        for j in range(10):
            ax[i,j].imshow(images[10 * i + j].reshape(20,20).T,cmap='gray_r')
    plt.xticks([])
    plt.yticks([])#去掉x,y轴的刻度
    plt.show()

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

def costFunction(theta,X,y,lamda):
    inner = sigmoid(X @ theta)
    first = y * np.log(inner)
    second = (1-y) * np.log(1-inner)
    #reg = np.sum(np.power(theta[1:],2)) * (lamda/(2 * len(X)))
    reg = theta[1:] @ theta[1:] * (lamda/(2 * len(X)))

    return -np.sum(first + second)/len(X) + reg

def gradient_reg(theta,X,y,lamda):
    reg = theta[1:]*(lamda/len(X))
    reg = np.insert(reg,0,values=0,axis=0)
    #theta = theta - alpha * (X.T @ sigmoid( X @ theta -y))/len(X) - reg
    first = (X.T @ (sigmoid(X @ theta) -y))/len(X)

    return first + reg

def one_vs_all(X,y,lamda,K):
    n=X.shape[1]
    theta_all = np.zeros((K,n))
    for i in range(1,K+1):
        theta_i = np.zeros(n,)
        res = minimize(fun=costFunction,x0=theta_i,args=(X,y == i,lamda),method='TNC',jac=gradient_reg)
        theta_all[i-1,:] = res.x

    return theta_all

def predict(X,theta_final):
    h=sigmoid(X @ theta_final.T)#(5000,401),(10,401)
    h_argmax = np.argmax(h,axis = 1)
    return h_argmax + 1

raw_X = data['X']
raw_y = data['y']
#print(raw_X.shape,raw_y.shape)
#plot_an_image(raw_X)
#plot_100_image(raw_X)
X = np.insert(raw_X,0,values=1,axis=1)
#print(X.shape)
y = raw_y.flatten()

lamda = 1
K = 10
theta_final = one_vs_all(X,y,lamda,K)
#print(theta_final)

y_predict = predict(X,theta_final)
acc = np.mean(y_predict == y)
print ('accuracy = {0}%'.format(acc * 100))

神经网络法

import numpy as np
import scipy.io as sio

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

data = sio.loadmat('D:\桌面\Coursera-ML-AndrewNg-Notes-master\code\ex3-neural network\ex3data1.mat')
raw_X = data['X']
raw_y = data['y']

X = np.insert(raw_X,0,values=1,axis=1)
y = raw_y.flatten()
#print(X.shape,y.shape)

weight = sio.loadmat('D:\桌面\Coursera-ML-AndrewNg-Notes-master\code\ex3-neural network\ex3weights.mat')
#print(weight.keys())
theta1 = weight['Theta1']
theta2 = weight['Theta2']
#print(theta1.shape,theta2.shape)#(25,401),(10,26)

z2 = X @ theta1.T
a2 = sigmoid(z2)
#print(a2.shape)#(5000,25)
a2 = np.insert(a2,0,values=1,axis=1)
#print(a2.shape)#(5000,26)

z3 = a2 @ theta2.T
a3 = sigmoid(z3)#(5000,10)

y_pred = np.argmax(a3, axis=1) + 1#(5000,)
accuracy = np.mean(y_pred == y)
print ('accuracy = {0}%'.format(accuracy * 100))  # accuracy = 97.52%

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值