您好,登录后才能下订单哦!
逻辑回归(Logistic Regression)是一种广泛应用于分类问题的机器学习算法。尽管名字中带有“回归”,但它实际上是一种分类算法,常用于二分类问题。逻辑回归的核心思想是通过一个线性模型来预测样本属于某个类别的概率,然后通过一个阈值将概率转换为类别标签。
在本文中,我们将介绍如何使用Python实现梯度下降算法来求解逻辑回归模型。
逻辑回归模型的核心是sigmoid函数,它将线性模型的输出映射到[0, 1]区间,表示样本属于正类的概率。sigmoid函数的定义如下:
\[ \sigma(z) = \frac{1}{1 + e^{-z}} \]
其中,\(z\) 是线性模型的输出:
\[ z = \theta_0 + \theta_1 x_1 + \theta_2 x_2 + \dots + \theta_n x_n \]
逻辑回归的目标是通过优化损失函数来找到最佳的模型参数 \(\theta\)。常用的损失函数是交叉熵损失函数:
\[ J(\theta) = -\frac{1}{m} \sum_{i=1}^{m} [y^{(i)} \log(h_\theta(x^{(i)})) + (1 - y^{(i)}) \log(1 - h_\theta(x^{(i)}))] \]
其中,\(h_\theta(x)\) 是sigmoid函数,\(y^{(i)}\) 是样本的真实标签,\(m\) 是样本数量。
梯度下降是一种常用的优化算法,通过迭代更新模型参数来最小化损失函数。梯度下降的更新公式如下:
\[ \theta_j := \theta_j - \alpha \frac{\partial J(\theta)}{\partial \theta_j} \]
其中,\(\alpha\) 是学习率,控制参数更新的步长。
对于逻辑回归,损失函数 \(J(\theta)\) 对参数 \(\theta_j\) 的偏导数为:
\[ \frac{\partial J(\theta)}{\partial \theta_j} = \frac{1}{m} \sum_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)}) x_j^{(i)} \]
下面我们使用Python实现梯度下降算法来求解逻辑回归模型。
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def compute_cost(X, y, theta):
m = len(y)
h = sigmoid(X @ theta)
cost = (-1/m) * np.sum(y * np.log(h) + (1 - y) * np.log(1 - h))
return cost
def gradient_descent(X, y, theta, alpha, num_iters):
m = len(y)
cost_history = []
for i in range(num_iters):
h = sigmoid(X @ theta)
gradient = (1/m) * (X.T @ (h - y))
theta = theta - alpha * gradient
cost = compute_cost(X, y, theta)
cost_history.append(cost)
return theta, cost_history
假设我们有一个简单的二分类数据集:
X = np.array([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]])
y = np.array([0, 0, 1, 1, 1])
# 添加偏置项
X = np.hstack((np.ones((X.shape[0], 1)), X))
# 初始化参数
theta = np.zeros(X.shape[1])
# 设置学习率和迭代次数
alpha = 0.01
num_iters = 1000
theta, cost_history = gradient_descent(X, y, theta, alpha, num_iters)
plt.plot(range(num_iters), cost_history)
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.title('Cost Function over Iterations')
plt.show()
def predict(X, theta):
return sigmoid(X @ theta) >= 0.5
predictions = predict(X, theta)
print("Predictions:", predictions)
本文介绍了如何使用Python实现梯度下降算法来求解逻辑回归模型。我们首先介绍了逻辑回归的基本原理和梯度下降算法,然后通过Python代码实现了模型的训练和预测。通过可视化损失函数的变化,我们可以观察到模型在训练过程中逐渐收敛。
逻辑回归是一种简单但非常有效的分类算法,广泛应用于各种实际问题中。通过理解其基本原理并掌握实现方法,可以为后续学习更复杂的机器学习算法打下坚实的基础。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。