吴恩达机器学习python作业之神经网络解决多元分类问题

参考链接:
吴恩达|机器学习作业3.1前馈神经网络_学吧学吧终成学霸的博客-CSDN博客
在这里插入图片描述
注意点:每一层的输出值都是theta * x 后加上sigmoid激励函数,而非只有输出层上才需要添加sigmoid函数。

给定的ex3weights.mat中的数据集显示该神经网络一共只有三层,第一层是输入层,特征数为401(400+bias),第二层为隐藏层,特征数为26(25+bias),第三层为输出层,每个样本会得到一个1*10的概率矩阵(array类型),根据1*10的概率矩阵得出最终的数值。

import numpy as np
from scipy import io

dt = io.loadmat("E:\机器学习\吴恩达\data_sets\ex3data1.mat")
x = dt["X"] #(5000, 400)
y = dt["y"] #(5000, 1)
cols = x.shape[1]+1

weights = io.loadmat("E:\机器学习\吴恩达\data_sets\ex3weights.mat")
theta1 = weights["Theta1"] #(25, 401)
theta2 = weights["Theta2"] #(10, 26)
#输入层features为401,隐藏层为26


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


#根据每一层的输入与这层的权重计算出这一层的输出
def layers(x,theta):
    samples = x.shape[0]
    a = np.ones((samples, 1))
    x = np.concatenate((a, x), 1)
    result_temp = x @ theta.T
    result = sigmoid(result_temp)
    return result


#根据输入层的值与权重计算出(5000,10)的输出矩阵
def prob(x,theta1,theta2):
    result_layer1 = layers(x,theta1) #(5000, 25)
    result_layer2 = layers(result_layer1,theta2) #(5000, 10)
    return result_layer2



def accuracy(estimated_y,y):
    sum = y.size #总数
    count = 0 #计算结果与实际值相等的个数
    for i in range(sum):
        if estimated_y[i] == y[i]:
            count = count + 1
    return count / sum




def calculateAccuracy(x,y,theta1,theta2):
    estimated_prob = prob(x,theta1,theta2)
    sum = y.size
    estimated_y = np.zeros(sum)
    for i in range(sum):
        estimated_y[i] = np.unravel_index(np.argmax(estimated_prob[i,:]), estimated_prob[i,:].shape)[0]+1
    print("accracy=",accuracy(estimated_y,y)*100,"%")

calculateAccuracy(x,y,theta1,theta2)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值