机器学习逻辑回归实现多分类与前向神经传播实现多分类

import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio
import matplotlib
import scipy.optimize as opt
from sklearn.metrics import classification_report
def load_data(path,transpose=True):
    data=sio.loadmat(path)#用以读取matlab类型的数据需要用到scipy.io
    y=data.get('y')#用以获取数值中的y值
    y=y.reshape(y.shape[0])#用以重定义y矩阵的行列值
    X=data.get('X')
    if transpose:
        X=np.array([im.reshape((20,20)) for im in X])#T在此用以将X数组转置,也就是将X中手写数据预先进行转置和拉伸。
        X=np.array([im.reshape(400) for im in X])#这里恢复了手写数据的维度
    return X,y
X,y=load_data('ex3data1.mat')
print(X.shape)
print(y.shape)
def plot_an_image(image):
    fig,ax=plt.subplots(figsize=(1,1))#figsize决定着图像的大小表示
    ax.matshow(image.reshape((20,20)),cmap=matplotlib.cm.binary)#mateshow为视图函数,cmap为选择颜色,由于选择时拉直了图片
    # 所以现在 重新定义一下维度
    plt.xticks([])#去掉刻度
    plt.yticks([])
pick_one=np.random.randint(0,5000)#随机选择一个0-5000的数
plot_an_image(X[pick_one,:])
plt.show()
print('this should be {}'.format(y[pick_one]))
def plot_100_image(X):
    sample_index=np.random.choice(len(X),100)
    images=X[sample_index,:]
    fig,ax=plt.subplots(ncols=10,nrows=10,figsize=(8,8 ),sharex=True,sharey=True)#cols列,rows行,sharex共享X属性
    for r in range(10):
        for c in range(10):
            ax[r,c].imshow(images[10*r+c].reshape((20,20)),cmap='gray_r')#ax[r,c]为选择图像中的第r行第c列的空格填充。
    plt.xticks([])  # 去掉刻度
    plt.yticks([])
plot_100_image(X)
plt.show()
raw_x,raw_y=load_data('ex3data1.mat')
#开始准备数据 np.dot(A,B),A@B向量内积,乘积

def sigmod(z):#普通的sigmod函数用在分类算法里较为常见
    return 1/(1+np.exp(-z))

def regularized_cost(theta,X,y,punish=1):#正则化的代价函数,以此减少过度拟合的情况
    A=sigmod(X@theta)
    first=y*np.log(A)
    second=(1-y)*np.log(1-A)
    reg=theta[1:]@theta[1:]
    return -np.sum(first+second)/len(X)+reg*(punish/(2*len(X)))

def gridentdecent(theta,X,y):
    return (1/len(X))*(X.T@(sigmod(X@theta)-y))
    #关于批量梯度下降的向量化的成因 https://blog.csdn.net/lgb_love/article/details/81456955

def regularized_grident(theta,X,y,punish=1):#正则化的梯度下降函数,以此减少过度拟合问题
    theta_n=theta[1:]#因为第一项是不需要正则化的,所有不算第一行
    regularized_theta=theta_n*(punish/len(X))
    regularized_theta=np.insert(regularized_theta,0,values=0,axis=0)#为了之后与X相乘的时候维度能一样,所以加上一列
    return gridentdecent(theta,X,y)+regularized_theta
def logical_regression(X,y,K,punish=1):#逻辑回归,区别于线性回归用于分类算法
    theta=np.zeros((K,X.shape[1]))#X的shape为(5000,401),所以为了分类出1-10的数字,要传建一个shape为(10,401)的theta来判断是哪一个数字
    for i in range(1,K+1):#因为判断的数字是1-10,所有i从1开始
        theta_i=np.zeros(X.shape[1],)#要注意theta_i是送到minimize中作为初始值的,维度设定为(n,)的一维向量
        res=opt.minimize(fun=regularized_cost,x0=theta_i,args=(X,y==i,1),method='TNC',jac=regularized_grident,options={'disp': True})
        theta[i-1,:]=res.x#将各自数字拟合的最佳值存入theta中
    return theta
def predict(x,theta):#预测函数
    prob=sigmod(x@theta.T)#Xshape为(5000,401)theta为(10,401)
    prob_max=np.argmax(prob,axis=1)#选择每一行中10列里面数字最大也就是预测值最大的
    print(prob_max.shape)
    return prob_max+1#由于theta从0开始而预测的数字从1开始
X=np.insert(raw_x,0,values=1,axis=1)#axis=1表示插入列,不标注和axis=0表示插入行
y=raw_y.flatten()#将多维数组转为一维数组的函数
theta_final=logical_regression(X,y,10)
print(logical_regression(X,y,10))
y_pred=predict(X,theta_final)
print('Accuracy={}'.format(np.mean(y == y_pred)))

#第二部分 !!!运用前缀反馈的神经网络算法
import scipy.io as sio
data=sio.loadmat('ex3weights.mat')
print(data.keys())#data.keys()函数查看关键的数据
theta1=data['Theta1']
theta2=data['Theta2']
a1=X
z2=a1@theta1.T
a2=sigmod(z2)
a2=np.insert(a2,0,values=1,axis=1)
z3=a2@theta2.T
a3=sigmod(z3)
print(a3.shape)
y_pred=np.argmax(a3,axis=1)#由于算出来的标号值为0-9,所以要将值原地加1
y_pred=y_pred+1
acc=np.mean(y_pred==y)
print(acc)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值