从零探索逻辑回归:Sigmoid 函数与损失函数可视化

从零探索逻辑回归:Sigmoid 函数与损失函数可视化

逻辑回归是机器学习中一种经典的分类算法,广泛应用于二分类问题。它通过 Sigmoid 函数将线性回归模型的输出映射到 (0, 1) 区间,从而实现对样本的分类。本文将通过两个 Jupyter Notebook 项目,详细探讨逻辑回归中的 Sigmoid 函数以及损失函数的可视化。

一、Sigmoid 函数的绘制

Sigmoid 函数是逻辑回归的核心,其数学表达式为:

这个函数的输出值在 0 和 1 之间,呈现出一个 S 形曲线。在逻辑回归中,Sigmoid 函数将线性组合的输出转换为概率值,从而实现分类。

代码实现

以下代码展示了如何使用 Python 和 matplotlib 绘制 Sigmoid 函数的图像:

Python复制

import numpy as np
import math
import matplotlib.pyplot as plt

def sigmoid(x):
    a = []
    for item in x:
        a.append(1.0 / (1.0 + math.exp(-item)))
    return a

x = np.arange(-10, 10, 0.1)
y = sigmoid(x)
plt.plot(x, y)
plt.title("Sigmoid Function")
plt.xlabel("Input (z)")
plt.ylabel("Output (σ(z))")
plt.grid(True)
plt.show()

运行结果

运行上述代码后,你将看到一个 S 形曲线,如下图所示:

从图中可以看出,当输入值 z 接近 0 时,Sigmoid 函数的输出接近 0.5;当 z 趋向于正无穷时,输出接近 1;当 z 趋向于负无穷时,输出接近 0。这种特性使得 Sigmoid 函数非常适合用于二分类任务。

二、逻辑回归与损失函数的可视化

逻辑回归的目标是找到最优的模型参数 θ,使得模型的损失函数最小。损失函数通常采用对数损失函数(Log Loss),其公式为:

其中,m 是样本数量,y(i) 是样本的真实标签,σ(z(i)) 是模型的预测值。

代码实现

以下代码展示了如何使用 Python 和 sklearn 实现逻辑回归,并对损失函数进行可视化:

Python复制

from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from mpl_toolkits.mplot3d import Axes3D
from sklearn.preprocessing import scale

# 加载数据集
data = load_breast_cancer()
X, y = scale(data['data'][:, :2]), data['target']  # 只取前两个特征,便于可视化

# 训练逻辑回归模型
lr = LogisticRegression(fit_intercept=False)
lr.fit(X, y)

# 获取模型参数
theta1 = lr.coef_[0, 0]
theta2 = lr.coef_[0, 1]

# 定义预测函数和损失函数
def p_theta_function(features, w1, w2):
    z = w1 * features[0] + w2 * features[1]
    return 1 / (1 + np.exp(-z))

def loss_function(samples_features, samples_labels, w1, w2):
    result = 0
    for features, label in zip(samples_features, samples_labels):
        p_result = p_theta_function(features, w1, w2)
        loss_result = -1 * label * np.log(p_result) - (1 - label) * np.log(1 - p_result)
        result += loss_result
    return result

# 生成参数空间
theta1_space = np.linspace(theta1 - 0.6, theta1 + 0.6, 50)
theta2_space = np.linspace(theta2 - 0.6, theta2 + 0.6, 50)

# 计算损失函数值
result1_ = np.array([loss_function(X, y, i, theta2) for i in theta1_space])
result2_ = np.array([loss_function(X, y, theta1, i) for i in theta2_space])

# 绘制损失函数的二维图像
fig1 = plt.figure(figsize=(12, 8))

plt.subplot(2, 2, 1)
plt.plot(theta1_space, result1_)
plt.title("Loss vs. Theta1")
plt.xlabel("Theta1")
plt.ylabel("Loss")

plt.subplot(2, 2, 2)
plt.plot(theta2_space, result2_)
plt.title("Loss vs. Theta2")
plt.xlabel("Theta2")
plt.ylabel("Loss")

# 绘制损失函数的等高线图
theta1_grid, theta2_grid = np.meshgrid(theta1_space, theta2_space)
loss_grid = np.array([loss_function(X, y, i, j) for i, j in zip(np.ravel(theta1_grid), np.ravel(theta2_grid))]).reshape(theta1_grid.shape)

plt.subplot(2, 2, 3)
plt.contour(theta1_grid, theta2_grid, loss_grid, levels=30)
plt.title("Contour Plot of Loss Function")
plt.xlabel("Theta1")
plt.ylabel("Theta2")

# 绘制损失函数的三维图像
fig2 = plt.figure()
ax = fig2.add_subplot(111, projection='3d')
ax.plot_surface(theta1_grid, theta2_grid, loss_grid, cmap='viridis')
ax.set_title("3D Surface Plot of Loss Function")
ax.set_xlabel("Theta1")
ax.set_ylabel("Theta2")
ax.set_zlabel("Loss")

plt.show()

运行结果

运行上述代码后,你将看到以下图像:

  1. Loss vs. Theta1Loss vs. Theta2:展示了损失函数随 θ1​ 和 θ2​ 的变化趋势。

  2. Contour Plot of Loss Function:等高线图,展示了损失函数在 θ1​ 和 θ2​ 平面上的分布。

  3. 3D Surface Plot of Loss Function:三维曲面图(jupyter暂时未绘制,换pycharm即可)

从图中可以看出,损失函数在某些区域达到最小值,这些区域对应着最优的模型参数 θ1​ 和 θ2​。

三、总结

通过上述两个项目,我们从基础的 Sigmoid 函数绘制,到复杂的损失函数可视化,逐步深入地探索了逻辑回归的实现和优化过程。Sigmoid 函数为逻辑回归提供了非线性映射能力,而损失函数的最小化则是模型优化的核心目标。通过可视化损失函数,我们可以直观地理解模型参数对模型性能的影响,从而更好地调整模型。

源码地址:https://gitee.com/jinwaifei/artificial-intelligence-project/tree/master/%E9%80%BB%E8%BE%91%E5%9B%9E%E5%BD%92

希望本文能帮助你更好地理解逻辑回归及其背后的数学原理。如果你对逻辑回归的其他方面感兴趣,比如正则化、梯度下降等,欢迎继续探索和学习!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值