用Python和Matplotlib画图理解L1、L2、L∞范数球:从圆形、菱形到正方形 用Python和Matplotlib画图理解L1、L2、L∞范数球从圆形、菱形到正方形在机器学习和数据科学领域范数Norm是一个基础但至关重要的数学概念。它不仅用于衡量向量的大小还在正则化、优化算法中扮演关键角色。然而对于初学者来说理解不同范数的几何意义往往是个挑战。本文将通过Python代码和Matplotlib可视化带你直观感受L1、L2和L∞范数在二维和三维空间中的几何表现——从圆形、菱形到正方形。1. 准备工作环境配置与基础概念在开始绘制范数球之前我们需要确保开发环境配置正确并简要回顾相关数学概念。1.1 安装必要的Python库确保你的Python环境已安装以下库pip install numpy matplotlib对于三维可视化我们还需要mpl_toolkits它是Matplotlib的一部分无需额外安装。1.2 范数的数学定义范数是向量空间中的一个函数赋予每个向量一个非负实数值满足以下性质非负性∥x∥ ≥ 0齐次性∥αx∥ |α|∥x∥三角不等式∥x y∥ ≤ ∥x∥ ∥y∥我们将重点关注的三种范数定义如下范数类型数学定义二维形状三维形状L1范数∥x∥₁ ΣxᵢL2范数∥x∥₂ √(Σxᵢ²)圆形球体L∞范数∥x∥∞ max(xᵢ)2. 二维范数球的可视化让我们从二维情况开始这是最直观的理解方式。2.1 绘制L2范数球圆形L2范数就是我们熟知的欧几里得距离。在二维空间中L2范数球就是圆形。import numpy as np import matplotlib.pyplot as plt theta np.linspace(0, 2*np.pi, 100) r 1 # 半径 x r * np.cos(theta) y r * np.sin(theta) plt.figure(figsize(6,6)) plt.plot(x, y, b-, linewidth2) plt.title(L2 Norm Ball (Circle)) plt.xlabel(x1) plt.ylabel(x2) plt.grid(True) plt.axis(equal) plt.show()这段代码生成了一个单位圆展示了L2范数球的形状。在正则化中L2范数对应Ridge回归倾向于产生均匀分布的权重。2.2 绘制L1范数球菱形L1范数球在二维空间中呈现为菱形这是稀疏性诱导正则化如Lasso回归的基础。# L1范数球的顶点 vertices np.array([[1,0], [0,1], [-1,0], [0,-1]]) plt.figure(figsize(6,6)) plt.plot([vertices[i][0] for i in range(4)] [vertices[0][0]], [vertices[i][1] for i in range(4)] [vertices[0][1]], r-, linewidth2) plt.title(L1 Norm Ball (Diamond)) plt.xlabel(x1) plt.ylabel(x2) plt.grid(True) plt.axis(equal) plt.show()L1范数球的尖角特性解释了为什么Lasso回归能产生稀疏解——优化过程倾向于让参数落在这些尖角上。2.3 绘制L∞范数球正方形L∞范数球在二维空间中是一个正方形这在某些约束优化问题中很有用。# L∞范数球的顶点 vertices np.array([[1,1], [-1,1], [-1,-1], [1,-1]]) plt.figure(figsize(6,6)) plt.plot([vertices[i][0] for i in range(4)] [vertices[0][0]], [vertices[i][1] for i in range(4)] [vertices[0][1]], g-, linewidth2) plt.title(L∞ Norm Ball (Square)) plt.xlabel(x1) plt.ylabel(x2) plt.grid(True) plt.axis(equal) plt.show()3. 三维范数球的可视化理解了二维情况后我们扩展到三维空间这将帮助我们建立更完整的几何直觉。3.1 绘制三维L2范数球球体from mpl_toolkits.mplot3d import Axes3D fig plt.figure(figsize(8,8)) ax fig.add_subplot(111, projection3d) u np.linspace(0, 2 * np.pi, 100) v np.linspace(0, np.pi, 100) x np.outer(np.cos(u), np.sin(v)) y np.outer(np.sin(u), np.sin(v)) z np.outer(np.ones(np.size(u)), np.cos(v)) ax.plot_surface(x, y, z, colorb, alpha0.3) ax.set_title(3D L2 Norm Ball (Sphere)) plt.show()3.2 绘制三维L1范数球八面体fig plt.figure(figsize(8,8)) ax fig.add_subplot(111, projection3d) # 定义八面体的顶点 vertices [ [1,0,0], [-1,0,0], [0,1,0], [0,-1,0], [0,0,1], [0,0,-1] ] # 定义八面体的面 faces [ [vertices[0], vertices[2], vertices[4]], [vertices[0], vertices[3], vertices[4]], [vertices[0], vertices[2], vertices[5]], [vertices[0], vertices[3], vertices[5]], [vertices[1], vertices[2], vertices[4]], [vertices[1], vertices[3], vertices[4]], [vertices[1], vertices[2], vertices[5]], [vertices[1], vertices[3], vertices[5]] ] # 绘制每个面 for face in faces: x [point[0] for point in face] y [point[1] for point in face] z [point[2] for point in face] ax.plot_trisurf(x, y, z, colorr, alpha0.3) ax.set_title(3D L1 Norm Ball (Octahedron)) plt.show()3.3 绘制三维L∞范数球立方体fig plt.figure(figsize(8,8)) ax fig.add_subplot(111, projection3d) # 定义立方体的顶点 vertices [ [1,1,1], [-1,1,1], [-1,-1,1], [1,-1,1], [1,1,-1], [-1,1,-1], [-1,-1,-1], [1,-1,-1] ] # 定义立方体的面 faces [ [vertices[0], vertices[1], vertices[2], vertices[3]], [vertices[4], vertices[5], vertices[6], vertices[7]], [vertices[0], vertices[1], vertices[5], vertices[4]], [vertices[2], vertices[3], vertices[7], vertices[6]], [vertices[1], vertices[2], vertices[6], vertices[5]], [vertices[0], vertices[3], vertices[7], vertices[4]] ] # 绘制每个面 for face in faces: x [point[0] for point in face] y [point[1] for point in face] z [point[2] for point in face] ax.plot_trisurf(x, y, z, colorg, alpha0.3) ax.set_title(3D L∞ Norm Ball (Cube)) plt.show()4. 范数球在机器学习中的应用理解了范数球的几何形状后我们可以更好地理解它们在机器学习中的应用。4.1 正则化中的范数选择不同的正则化项对应不同的范数约束L2正则化Ridge回归使用L2范数倾向于均匀缩小所有参数L1正则化Lasso回归使用L1范数倾向于产生稀疏解L∞正则化限制最大参数值适用于某些特殊约束在实际项目中选择哪种正则化取决于具体需求。如果需要特征选择L1通常更好如果只是防止过拟合L2可能更合适。4.2 优化问题的几何解释考虑一个带约束的优化问题minimize f(x) subject to ∥x∥ ≤ r这个约束定义了一个范数球解必须位于球内。不同范数球的形状会影响解的位置L1范数球的尖角使得解更可能落在坐标轴上稀疏性L2范数球的平滑边界使得解更可能分布在各个维度L∞范数球的平坦面使得解更可能落在边界面上4.3 多范数组合的应用在实际应用中有时会组合使用不同范数# Elastic Net正则化结合L1和L2 from sklearn.linear_model import ElasticNet model ElasticNet(alpha0.1, l1_ratio0.5) # l1_ratio控制L1和L2的混合比例这种组合可以同时获得L1的稀疏性和L2的稳定性。