使用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.梯度下降