numpy代码实现逻辑回归模型

使用python的numpy模块实现逻辑回归模型的代码

使用了numpy模块,pandas模块,matplotlib模块

1.初始化参数

def initial_para(nums_feature):
    """initial the weights and bias which is zero"""
    #nums_feature是输入数据的属性数目,因此权重w是[1, nums_feature]维
    #且w和b均初始化为0
    w = np.zeros((1, nums_feature))
    b = 0
    return w, b

2.逻辑回归方程

def activation(x, w , b):
    """a linear function and then sigmoid activation function: 
    x_ = w*x +b,y = 1/(1+exp(-x_))"""
    #线性方程,输入的x是[batch, 2]维,输出是[1, batch]维,batch是模型优化迭代一次输入数据的数目
    #[1, 2] * [2, batch] = [1, batch], 所以是w * x.T(x的转置)
    #np.dot是矩阵乘法
    x_ = np.dot(w, x.T) + b
    #np.exp是实现e的x次幂
    sigmoid = 1 / (1 + np.exp(-x_))
    return sigmoid

3.梯度下降


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值