逻辑回归如何进行模型的训练和优化

介绍

逻辑回归是分类问题中常用的机器学习算法之一。它使用逻辑函数将输入特征映射到一个概率值,并基于该概率进行分类预测。本文将详细介绍逻辑回归模型的训练和优化过程。

算法原理

逻辑回归模型采用sigmoid函数作为激活函数,将线性回归的结果映射到一个范围在0到1之间的概率值。sigmoid函数的定义如下:

$$
g(z) = \frac{1}{1 + e^{-z}}
$$

逻辑回归模型的预测函数为:

$$
h_\theta(x) = g(\theta^Tx) = \frac{1}{1 + e^{-\theta^Tx}}
$$

其中,$\theta$是模型的参数向量,$x$是输入特征向量。

公式推导

逻辑回归使用最大似然估计法来估计模型参数。假设给定训练集$D = {(x^{(i)}, y^{(i)})}_{i=1}^m$,其中$x^{(i)}$是第$i$个样本的特征向量,$y^{(i)}$是样本的真实标签(0或1)。模型参数$\theta$的似然函数定义如下:

$$
L(\theta) = \prod_{i=1}^m h_\theta(x^{(i)})^{y^{(i)}} \cdot (1 – h_\theta(x^{(i)}))^{1 – y^{(i)}}
$$

为了方便计算,通常使用对数似然函数:

$$
l(\theta) = \log(L(\theta)) = \sum_{i=1}^m y^{(i)}\log(h_\theta(x^{(i)})) + (1 – y^{(i)})\log(1 – h_\theta(x^{(i)}))
$$

目标是最大化对数似然函数,即最小化损失函数$J(\theta)$的负值:

$$
J(\theta) = -\frac{1}{m} \cdot l(\theta) = -\frac{1}{m} \sum_{i=1}^m y^{(i)}\log(h_\theta(x^{(i)})) + (1 – y^{(i)})\log(1 – h_\theta(x^{(i)}))
$$

为了找到使损失函数最小化的参数$\theta$,可以使用梯度下降法进行优化。

计算步骤

  1. 初始化模型参数$\theta$,可以采用随机初始化或者将所有参数初始化为0;
  2. 使用梯度下降法迭代更新参数$\theta$,直到收敛或达到最大迭代次数:
  3. 计算预测值$h_\theta(x)$;
  4. 根据梯度下降的更新公式更新参数$\theta$;
  5. 返回训练好的模型参数$\theta$。

Python代码示例

下面是一个使用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) 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(h) - (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 - h))
 return cost

def gradient_descent(X, y, theta, alpha, num_iters):
 m = len(y)
 J_history = []

 for _ in range(num_iters):
 h = sigmoid(X @ theta)
 gradient = (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 (X.T @ (h - y))
 theta -= alpha 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 gradient
 J_history.append(compute_cost(X, y, theta))

 return theta, J_history

# 虚拟数据集
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
y = np.array([0, 0, 1, 1])

# 在X前添加一列1作为截距
X = np.insert(X, 0, 1, axis=1)

# 初始化参数
theta = np.zeros(X.shape[1])

# 设置超参数
alpha = 0.1
num_iters = 100

# 梯度下降优化
theta, J_history = gradient_descent(X, y, theta, alpha, num_iters)

# 绘制损失函数的迭代曲线
plt.plot(range(num_iters), J_history)
plt.xlabel('Iterations')
plt.ylabel('Cost')
plt.title('Cost function convergence')
plt.show()

# 打印训练得到的参数
print('Optimized parameters:', theta)

代码解释:

  1. sigmoid()函数计算sigmoid函数的值;
  2. compute_cost()函数计算损失函数的值;
  3. gradient_descent()函数使用梯度下降法更新参数;
  4. 定义虚拟数据集Xy
  5. X前添加一列1,以便计算截距;
  6. 初始化参数theta
  7. 设置学习率alpha和迭代次数num_iters
  8. 使用梯度下降法进行训练,返回训练得到的参数theta和损失函数的迭代曲线J_history
  9. 绘制损失函数的迭代曲线;
  10. 打印训练得到的参数theta

希望以上示例能帮助你理解逻辑回归模型的训练和优化过程。

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/824159/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球