是否可以通过增加模型复杂性来避免欠拟合

问题背景

在机器学习中,欠拟合是指模型无法表达数据的复杂性,导致模型在训练集和测试集上均表现不佳的现象。欠拟合通常是由于模型过于简单,无法捕捉到数据的非线性关系或者细微的模式。而增加模型的复杂性,例如增加模型的参数、引入更高阶的特征等,可以有效地缓解欠拟合问题。

本文将介绍如何通过增加模型复杂性来避免欠拟合问题。我们将以多项式回归模型为例进行说明。

算法原理

多项式回归是一种将自变量的高次幂引入线性回归模型的方法。通过引入更高阶的特征,多项式回归可以更好地拟合非线性关系。多项式回归的数学表达式如下:

$$
y = \theta_0 + \theta_1x + \theta_2x^2 + \theta_3x^3 + … + \theta_dx^d
$$

其中,$y$是因变量,$x$是自变量,$\theta_0, \theta_1, …, \theta_d$是模型的参数,$d$是多项式的最高次数。

公式推导

假设我们有$m$个训练样本,表示为$(x^{(1)}, y^{(1)}), (x^{(2)}, y^{(2)}), …, (x^{(m)}, y^{(m)})$。我们的目标是找到一组最优的参数$\theta_0, \theta_1, …, \theta_d$,使得模型的预测值和真实值之间的误差最小化。我们可以使用最小二乘法来求解这个问题。

最小二乘法的目标是最小化损失函数$J(\theta)$,定义为预测值与真实值之间的平方误差的均值。对于多项式回归,损失函数的表达式如下:

$$
J(\theta) = \frac{1}{2m}\sum_{i=1}^{m}(h_{\theta}(x^{(i)}) – y^{(i)})^2
$$

其中,$h_{\theta}(x^{(i)})$是模型对第$i$个样本的预测值,定义为:

$$
h_{\theta}(x^{(i)}) = \theta_0 + \theta_1x^{(i)} + \theta_2(x^{(i)})^2 + \theta_3(x^{(i)})^3 + … + \theta_d(x^{(i)})^d
$$

为了最小化损失函数$J(\theta)$,我们需要对参数$\theta_0, \theta_1, …, \theta_d$进行求解,使得偏导数$\frac{\partial J(\theta)}{\partial \theta_k}$($k=0,1,…,d$)等于0。

计算步骤

  1. 初始化模型参数$\theta_0, \theta_1, …, \theta_d$为0或者随机值。
  2. 使用梯度下降法,迭代更新参数$\theta_0, \theta_1, …, \theta_d$,使得损失函数$J(\theta)$最小化。
  3. 重复步骤2直到达到迭代次数或者收敛。

Python代码示例

下面是一个使用多项式回归来拟合数据的Python代码示例。我们首先生成一个带有噪声的虚拟数据集,然后使用多项式回归来拟合这个数据集。

import numpy as np
import matplotlib.pyplot as plt

# 生成虚拟数据集
np.random.seed(0)
X = np.linspace(-2, 2, 100).reshape(-1, 1)
y = 4 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**3 - 3 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**2 + 2 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 + np.random.normal(0, 1, size=X.shape)

# 多项式回归
d = 3 # 多项式的最高次数

# 添加高阶特征
def add_higher_order_features(X, d):
 X_higher = np.ones_like(X)
 for i in range(1, d+1):
 X_higher = np.concatenate((X_higher, X**i), axis=1)
 return X_higher

X_higher = add_higher_order_features(X, d)

# 初始化参数
theta = np.zeros((d + 1, 1))

# 定义损失函数
def loss_function(X, y, theta):
 m = X.shape[0]
 h_theta = np.dot(X, theta)
 loss = np.mean((h_theta - y)**2) / 2
 return loss

# 定义梯度下降函数
def gradient_descent(X, y, theta, learning_rate, num_iterations):
 m = X.shape[0]
 losses = []
 for i in range(num_iterations):
 h_theta = np.dot(X, theta)
 error = h_theta - y
 gradient = np.dot(X.T, error) / m
 theta -= 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 gradient
 loss = loss_function(X, y, theta)
 losses.append(loss)
 return theta, losses

# 执行梯度下降算法
learning_rate = 0.01
num_iterations = 1000

theta_final, losses = gradient_descent(X_higher, y, theta, learning_rate, num_iterations)

# 绘制拟合曲线和原始数据点
X_plot = np.linspace(-2, 2, 100).reshape(-1, 1)
X_plot_higher = add_higher_order_features(X_plot, d)
y_plot = np.dot(X_plot_higher, theta_final)

plt.scatter(X, y, color='blue', label='Original Data')
plt.plot(X_plot, y_plot, color='red', label='Fitted Curve')
plt.xlabel('X')
plt.ylabel('y')
plt.legend()
plt.show()

代码细节解释

  • 我们使用numpy库生成虚拟数据集,并添加高阶特征X_higher
  • 初始化参数theta为0。
  • 定义损失函数loss_function,计算预测值与真实值之间的平方误差的均值。
  • 定义梯度下降函数gradient_descent,使用梯度下降法更新参数theta,并记录每次迭代的损失值。
  • 执行梯度下降算法,得到最优参数theta_final和损失值列表losses
  • 绘制拟合曲线和原始数据点。

通过增加模型复杂性,我们可以看到最终拟合曲线能够更好地拟合原始数据,从而避免了欠拟合的问题。

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

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

(0)

大家都在看

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