Logistic回归
Logistic回归与多重线性回归类似,区别就在于它们的因变量不同。多重线性回归的因变量连续,而Logistic回归的因变量是离散的,一般为二项分布,即对应二分类问题。
回归问题的常规步骤:
- 构造假设函数
。 - 构造损失函数
。 - 调整回归参数
使得损失 最小。
预测函数
常用的Logistic function是Sigmoid函数:
设数据集的特征维数为
表示对于输入
损失函数
似然函数是一种关于统计模型参数的函数。给定输出
在Logistic回归中,对于输入
因此,似然函数:
对数似然函数:
最大似然估计是求当
梯度下降法求解参数
梯度下降法更新
对于给定数据集,可以认为是有
简单实现
m = 400 ## size of train set.
n = 300 ## number of features.
alpha = 0.1 ## learning rate
D = (rng.randn(m, n), rng.randint(size = m, low = 0, high = 2))
training_steps = 1000
x = T.dmatrix('x') ## input
y = T.dvector('y') ## output
## initialize weights
w = shared(rng.randn(n), name='w')
## initialize bias term, used to avoid overfitting.
b = shared(0., name='b')
# Construct Theano expression graph
p = 1 / (1 + T.exp(-T.dot(x, w) - b)) # Probability that target = 1
prediction = p > 0.5 # The prediction thresholded
## xent = -y * T.log(p) - (1-y) * T.log(1-p) # Cross-entropy loss function
## cost = xent.mean() ## + 0.01 * (w**2).sum() # The cost to minimize
cost = T.nnet.binary_crossentropy(p, y).mean()
gw, gb = T.grad(cost, [w, b]) # Compute the gradient of the cost on w and b.
# compile
train = function(
inputs=[x, y],
outputs=[prediction, cost],
updates=[(w, w - alpha * gw), (b, b - alpha * gb)])
predict = function(inputs=[x], outputs=prediction)
# train
for i in range(training_steps):
pred, err = train(D[0], D[1])
# display arguments result:
# print(w.get_value(), b.get_value())
lables = D[1] ## original labels.
classify = predict(D[0]) ## classify result using logistic regression.
ks = len([i for i in range(0, m) if D[1][i] != classify[i]])
print("correctness: ", (m-ks)/m)