介绍
在Logistic回归模型中,我们通常使用sigmoid函数将输入数据映射到概率值。sigmoid函数是一种常用的激活函数,它能够将任意实数映射到0到1之间的概率值。在Logistic回归中,我们使用sigmoid函数来预测二分类问题中的概率。
算法原理
Logistic回归模型使用线性回归模型的线性组合,并通过sigmoid函数对结果进行转换以得到概率值。其中,sigmoid函数被定义为:
$$
\sigma(z) = \frac{1}{1+e^{-z}}
$$
其中,$z$表示线性回归模型的线性组合结果。
公式推导
在Logistic回归中,我们假设有以下的线性回归模型:
$$
z = w_0 + w_1x_1 + w_2x_2 + … + w_nx_n
$$
其中,$z$表示线性回归模型的线性组合结果,$w_0, w_1, w_2, …, w_n$是模型的参数,$x_1, x_2, …, x_n$是输入特征。
我们希望将线性回归模型的结果转换为0到1之间的概率值。为了实现这一点,我们使用sigmoid函数:
$$
\hat{y} = \sigma(z) = \frac{1}{1+e^{-z}}
$$
其中,$\hat{y}$表示预测的概率值。
计算步骤
- 初始化模型参数$w_0, w_1, w_2, …, w_n$
- 对于每个训练样本$(x_1, x_2, …, x_n)$,计算线性组合结果$z$
- 将线性组合结果$z$通过sigmoid函数转换为概率值$\hat{y}$
- 根据预测的概率值$\hat{y}$和实际标签$y$,计算损失函数
- 使用梯度下降法或其他优化算法来更新模型参数$w_0, w_1, w_2, …, w_n$
- 重复步骤2-5直到满足收敛条件或达到最大迭代次数
- 使用训练好的模型进行预测
复杂Python代码示例
下面是一个使用Python实现Logistic回归模型的示例代码:
import numpy as np
import matplotlib.pyplot as plt
# 定义sigmoid函数
def sigmoid(z):
return 1 / (1 + np.exp(-z))
# 初始化模型参数
def initialize_parameters(dim):
w = np.zeros((dim, 1))
b = 0
return w, b
# 前向传播
def propagate(w, b, X, Y):
m = X.shape[1]
# 计算线性组合结果
Z = np.dot(w.T, X) + b
A = sigmoid(Z)
# 计算损失函数
cost = -1 / m artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls np.sum(Y artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls np.log(A) + (1 - Y) artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls np.log(1 - A))
# 反向传播
dw = 1 / m artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls np.dot(X, (A - Y).T)
db = 1 / m artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls np.sum(A - Y)
cost = np.squeeze(cost)
return dw, db, cost
# 梯度下降法更新参数
def optimize(w, b, X, Y, num_iterations, learning_rate, print_cost=False):
costs = []
for i in range(num_iterations):
dw, db, cost = propagate(w, b, X, Y)
w -= learning_rate artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls dw
b -= learning_rate artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls db
# 每100次迭代记录一次损失值
if i % 100 == 0:
costs.append(cost)
# 打印损失值
if print_cost and i % 100 == 0:
print("迭代 %i 次后的损失值: %f" % (i, cost))
return w, b, costs
# 预测
def predict(w, b, X):
Z = np.dot(w.T, X) + b
A = sigmoid(Z)
# 将预测概率大于0.5的设置为1,否则为0
predictions = (A > 0.5).astype(int)
return predictions
# 完整的Logistic回归模型
def logistic_regression(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False):
# 初始化参数
w, b = initialize_parameters(X_train.shape[0])
# 梯度下降法更新参数
w, b, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost=print_cost)
# 预测
train_predictions = predict(w, b, X_train)
test_predictions = predict(w, b, X_test)
# 计算准确率
train_accuracy = 100 - np.mean(np.abs(train_predictions - Y_train)) artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls 100
test_accuracy = 100 - np.mean(np.abs(test_predictions - Y_test)) artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls 100
print("训练集准确率: {} %".format(train_accuracy))
print("测试集准确率: {} %".format(test_accuracy))
# 绘制损失函数曲线
plt.plot(costs)
plt.xlabel("迭代次数 (每100次)")
plt.ylabel("损失值")
plt.title("梯度下降法")
plt.show()
return train_accuracy, test_accuracy
# 使用示例数据集
np.random.seed(0)
m = 1000
X = np.random.randn(2, m)
Y = np.random.randint(0, 2, (1, m))
X_train = X[:, :800]
Y_train = Y[:, :800]
X_test = X[:, 800:]
Y_test = Y[:, 800:]
train_accuracy, test_accuracy = logistic_regression(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.05, print_cost=True)
代码细节解释
sigmoid
函数实现了sigmoid函数的计算,将输入值转换为0到1之间的概率值。initialize_parameters
函数初始化模型参数$w$和$b$,将其设置为零向量和零标量。propagate
函数实现了前向传播的过程,包括计算线性组合结果$Z$,通过sigmoid函数计算预测概率值$A$,并计算损失函数。optimize
函数使用梯度下降法更新模型参数$w$和$b$,并返回更新后的参数和损失值。predict
函数根据线性组合结果$Z$和阈值0.5将预测概率值转换为0或1的预测值。logistic_regression
函数是完整的Logistic回归模型,包括初始化参数、梯度下降法更新参数、预测和计算准确率的过程。- 假设训练集和测试集都是二维的输入特征变量$X$,并且对应的标签$Y$是二分类问题的标签。
- 在示例中,我们使用了随机生成的数据集,并将训练集和测试集的准确率打印出来。
- 在最后,我们绘制了损失函数的曲线,用于可视化优化过程的效果。
以上是一个使用Python实现Logistic回归模型的示例,其中包含了详细的原理介绍、公式推导、算法步骤、复杂代码示例和代码细节解释。
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/821810/
转载文章受原作者版权保护。转载请注明原作者出处!