问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

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

创作时间:
作者:
@小白创作中心

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

引用
CSDN
1.
https://blog.csdn.net/qq_57063846/article/details/145606814

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

一、Sigmoid函数的绘制

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

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

代码实现

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

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实现逻辑回归,并对损失函数进行可视化:

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函数为逻辑回归提供了非线性映射能力,而损失函数的最小化则是模型优化的核心目标。通过可视化损失函数,我们可以直观地理解模型参数对模型性能的影响,从而更好地调整模型。

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

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号