作业三 多分类和神经网络

多分类法

#逻辑回归解决多分类问题
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%

你可能感兴趣的:(机器学习,神经网络,python,深度学习)