逻辑回归更多的是从概率的层面进行建模,但是我们在实际应用中,更多的是用在二元分类型的问题。
思考:如何算“更好的拟合”,类比一元线性回归的最小二乘法。
sigmoid函数:
逻辑回归公式:
from math import exp
def prediction(row, coefficients):
y_hat = coefficients[0]
for i in range(len(row) - 1):
y_hat += coefficients[i + 1] * row[i]
return 1 / (1 + exp(-y_hat))
if __name__ == '__main__':
dataset = [[2, 2, 0],
[2, 4, 0],
[3, 3, 0],
[4, 5, 0],
[8, 1, 1],
[8.5, 3.5, 1],
[9, 1, 1],
[10, 4, 1]]
coef = [-0.5, 0.9, -1.5]
for row in dataset:
y_hat = prediction(row, coef)
print("our Expected Value is %.3f,The Predicted Value is %.3f [%d]" % (row[-1], y_hat, round(y_hat)))
17.2 梯度下降法求解逻辑回归系数
from math import exp
def prediction(row, coefficients):
y_hat = coefficients[0]
for i in range(len(row) - 1):
y_hat += coefficients[i + 1] * row[i]
return 1 / (1 + exp(-y_hat))
# using stochastic gradient descent method to calculate logistic regression coefficients
# 用随机梯度下降算法求解逻辑回归系数
def using_sgd_method_to_calculate_coefficients(training_dataset, learning_rate, n_times_epoch):
coefficients = [0.0 for i in range(len(training_dataset[0]))]
for epoch in range(n_times_epoch):
the_sum_of_error = 0
for row in training_dataset:
y_hat = prediction(row, coefficients)
error = row[-1] - y_hat # 真实值-预测值
the_sum_of_error += error ** 2
coefficients[0] = coefficients[0] + learning_rate * error * y_hat * (1.0 - y_hat)
for i in range(len(row) - 1):
coefficients[i + 1] = coefficients[i + 1] + learning_rate * error * y_hat * (1.0 - y_hat) * row[i]
print("This is epoch [%d],the learning rate we are using is [%.3f],the error is [%.3f]" % (
epoch, learning_rate, the_sum_of_error))
return coefficients
if __name__ == '__main__':
learning_rate = 0.1
n_times_epoch = 1000
dataset = [[2, 2, 0],
[2, 4, 0],
[3, 3, 0],
[4, 5, 0],
[8, 1, 1],
[8.5, 3.5, 1],
[9, 1, 1],
[10, 4, 1]]
coef = using_sgd_method_to_calculate_coefficients(dataset, learning_rate, n_times_epoch)
print(coef)



