在前面两节课中,我们了解了AI的发展历程和基本概念。今天,我们将深入学习AI算法中必不可少的数学基础知识——微积分和概率论。这些数学工具是理解和实现各种AI算法的基石。
为什么AI需要数学?
AI算法本质上是数学模型,它们通过数学公式来描述数据之间的关系,并通过数学方法来优化模型参数。掌握必要的数学知识,能帮助我们:
- 理解算法的工作原理
- 调试和优化模型
- 解决实际问题时做出正确决策
graph TD
A[AI算法] --> B[数学基础]
B --> C[微积分]
B --> D[概率论]
C --> E[优化算法]
D --> F[统计学习]
微积分初步
微积分是研究函数变化率和累积量的数学分支,在AI中主要用于优化算法,如梯度下降。
函数概念
函数是描述输入和输出之间关系的规则。在AI中,我们经常需要找到最优的函数来拟合数据。
import numpy as np
import matplotlib.pyplot as plt
# 创建函数示例
def linear_function(x):
"""线性函数: y = 2x + 1"""
return 2 * x + 1
def quadratic_function(x):
"""二次函数: y = x^2 - 4x + 3"""
return x**2 - 4*x + 3
# 生成数据点
x_values = np.linspace(-3, 7, 100)
y_linear = linear_function(x_values)
y_quadratic = quadratic_function(x_values)
# 绘制函数图像
plt.figure(figsize=(12, 5))
plt.subplot(1, 2, 1)
plt.plot(x_values, y_linear, 'b-', linewidth=2)
plt.title('线性函数: y = 2x + 1')
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True, alpha=0.3)
plt.subplot(1, 2, 2)
plt.plot(x_values, y_quadratic, 'r-', linewidth=2)
plt.title('二次函数: y = x² - 4x + 3')
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
# 函数应用示例:预测房价
def predict_house_price(area, price_per_sqm=50000, base_price=500000):
"""根据面积预测房价"""
return base_price + price_per_sqm * area
area = 100 # 平方米
price = predict_house_price(area)
print(f"面积为{area}平方米的房子预测价格为: {price}元")
导数的概念
导数描述函数在某一点的变化率,即函数曲线的斜率。在AI中,导数用于确定函数增减方向,是优化算法的核心。
# 数值微分示例
def numerical_derivative(func, x, h=1e-5):
"""数值微分计算导数"""
return (func(x + h) - func(x - h)) / (2 * h)
# 定义函数
def f(x):
return x**2 + 3*x + 2
# 计算导数
x_point = 2
derivative = numerical_derivative(f, x_point)
print(f"函数 f(x) = x² + 3x + 2 在 x = {x_point} 处的导数为: {derivative}")
# 解析解: f'(x) = 2x + 3
analytical_derivative = 2 * x_point + 3
print(f"解析解为: {analytical_derivative}")
# 可视化导数概念
x = np.linspace(-4, 2, 100)
y = f(x)
# 在某点的切线
x_tangent = -1
y_tangent = f(x_tangent)
slope = numerical_derivative(f, x_tangent)
# 切线方程: y - y1 = m(x - x1)
tangent_line = slope * (x - x_tangent) + y_tangent
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'b-', linewidth=2, label='f(x) = x² + 3x + 2')
plt.plot(x, tangent_line, 'r--', linewidth=2, label=f'在x={x_tangent}处的切线')
plt.scatter([x_tangent], [y_tangent], color='red', s=50, zorder=5)
plt.title('导数的几何意义:切线斜率')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
print(f"在x={x_tangent}处,函数值为{y_tangent:.2f},切线斜率为{slope:.2f}")
偏导数简介
在多变量函数中,偏导数表示函数对其中一个变量的变化率,而保持其他变量不变。
# 多变量函数示例
def multivariable_function(x, y):
"""二元函数: f(x, y) = x² + y²"""
return x**2 + y**2
# 偏导数计算
def partial_derivative_x(func, x, y, h=1e-5):
"""对x的偏导数"""
return (func(x + h, y) - func(x - h, y)) / (2 * h)
def partial_derivative_y(func, x, y, h=1e-5):
"""对y的偏导数"""
return (func(x, y + h) - func(x, y - h)) / (2 * h)
# 计算偏导数
x_val, y_val = 2, 3
partial_x = partial_derivative_x(multivariable_function, x_val, y_val)
partial_y = partial_derivative_y(multivariable_function, x_val, y_val)
print(f"函数 f(x, y) = x² + y² 在点({x_val}, {y_val})处:")
print(f"对x的偏导数: {partial_x}")
print(f"对y的偏导数: {partial_y}")
# 解析解: ∂f/∂x = 2x, ∂f/∂y = 2y
print(f"解析解 - 对x的偏导数: {2 * x_val}")
print(f"解析解 - 对y的偏导数: {2 * y_val}")
# 可视化三维函数和梯度
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(12, 5))
# 3D图
ax1 = fig.add_subplot(121, projection='3d')
x = np.linspace(-3, 3, 30)
y = np.linspace(-3, 3, 30)
X, Y = np.meshgrid(x, y)
Z = multivariable_function(X, Y)
ax1.plot_surface(X, Y, Z, cmap='viridis', alpha=0.7)
ax1.set_title('三维函数: f(x, y) = x² + y²')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_zlabel('z')
# 等高线图
ax2 = fig.add_subplot(122)
contour = ax2.contour(X, Y, Z, levels=20)
ax2.clabel(contour, inline=True, fontsize=8)
ax2.set_title('等高线图')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
# 显示梯度方向
grad_x, grad_y = 2*x_val, 2*y_val
ax2.arrow(x_val, y_val, -0.5*grad_x, -0.5*grad_y,
head_width=0.2, head_length=0.2, fc='red', ec='red')
ax2.scatter(x_val, y_val, color='red', s=50)
plt.tight_layout()
plt.show()
print(f"在点({x_val}, {y_val})处,梯度向量为({grad_x}, {grad_y}),指向函数增长最快的方向")
导数在优化中的应用
在AI中,我们经常需要找到函数的最小值或最大值,导数是解决这类优化问题的关键工具。
# 梯度下降示例
def gradient_descent_example():
"""梯度下降法寻找函数最小值"""
# 目标函数: f(x) = x^2 - 4x + 3
def objective_function(x):
return x**2 - 4*x + 3
# 目标函数的导数: f'(x) = 2x - 4
def derivative(x):
return 2*x - 4
# 梯度下降参数
x = 0.0 # 初始点
learning_rate = 0.1 # 学习率
iterations = 20 # 迭代次数
# 记录过程
x_history = [x]
y_history = [objective_function(x)]
print("梯度下降过程:")
print(f"初始点: x = {x:.4f}, f(x) = {objective_function(x):.4f}")
for i in range(iterations):
# 计算梯度
grad = derivative(x)
# 更新参数
x = x - learning_rate * grad
# 记录
x_history.append(x)
y_history.append(objective_function(x))
if i % 5 == 0:
print(f"第{i}次迭代: x = {x:.4f}, f(x) = {objective_function(x):.4f}")
print(f"最终结果: x = {x:.4f}, f(x) = {objective_function(x):.4f}")
print(f"理论最优解: x = 2.0000, f(x) = -1.0000")
# 可视化优化过程
x_vals = np.linspace(-1, 5, 100)
y_vals = objective_function(x_vals)
plt.figure(figsize=(10, 6))
plt.plot(x_vals, y_vals, 'b-', linewidth=2, label='f(x) = x² - 4x + 3')
plt.scatter(x_history, y_history, c='red', s=50, zorder=5, label='优化路径')
plt.scatter(x_history[0], y_history[0], c='green', s=100, zorder=5, label='起始点')
plt.scatter(x_history[-1], y_history[-1], c='orange', s=100, zorder=5, label='终点')
plt.title('梯度下降优化过程')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
gradient_descent_example()
概率论初步
概率论帮助我们处理不确定性,在机器学习中用于建模数据分布和进行统计推断。
概率的基本概念
概率是对事件发生可能性的度量,取值范围在0到1之间。
# 概率基本概念演示
import random
from collections import Counter
def simulate_coin_toss(n_trials):
"""模拟抛硬币实验"""
results = [random.choice(['正面', '反面']) for _ in range(n_trials)]
counter = Counter(results)
print(f"抛硬币{n_trials}次的结果统计:")
for outcome, count in counter.items():
probability = count / n_trials
print(f"{outcome}: {count}次, 概率: {probability:.4f}")
return results
# 进行模拟
print("小样本实验 (10次):")
simulate_coin_toss(10)
print("\n" + "="*30 + "\n")
print("大样本实验 (1000次):")
simulate_coin_toss(1000)
# 条件概率示例
def conditional_probability_example():
"""条件概率示例"""
# 假设班级中有60%的学生喜欢数学,40%的学生喜欢语文
# 同时喜欢数学和语文的学生占30%
p_math = 0.6 # P(喜欢数学)
p_chinese = 0.4 # P(喜欢语文)
p_both = 0.3 # P(喜欢数学且喜欢语文)
# 条件概率: P(喜欢语文|喜欢数学) = P(喜欢数学且喜欢语文) / P(喜欢数学)
p_chinese_given_math = p_both / p_math
print("条件概率示例:")
print(f"P(喜欢数学) = {p_math}")
print(f"P(喜欢语文) = {p_chinese}")
print(f"P(喜欢数学且喜欢语文) = {p_both}")
print(f"P(喜欢语文|喜欢数学) = {p_chinese_given_math:.4f}")
print("即在喜欢数学的学生中,有50%也喜欢语文")
conditional_probability_example()
随机变量
随机变量是对随机实验结果的数值化表示,分为离散型和连续型。
# 离散型随机变量示例
def dice_roll_simulation(n_rolls=1000):
"""模拟掷骰子实验"""
rolls = [random.randint(1, 6) for _ in range(n_rolls)]
counter = Counter(rolls)
# 计算频率
frequencies = {i: counter[i]/n_rolls for i in range(1, 7)}
print("掷骰子实验结果:")
print("点数\t频数\t频率\t理论概率")
for i in range(1, 7):
freq = frequencies.get(i, 0)
print(f"{i}\t{counter[i]}\t{freq:.4f}\t{1/6:.4f}")
# 可视化
plt.figure(figsize=(10, 6))
points = list(range(1, 7))
observed_freq = [frequencies.get(i, 0) for i in points]
theoretical_prob = [1/6] * 6
plt.bar(points, observed_freq, alpha=0.7, label='观察频率')
plt.plot(points, theoretical_prob, 'ro-', label='理论概率')
plt.xlabel('骰子点数')
plt.ylabel('概率')
plt.title(f'掷骰子实验 ({n_rolls}次)')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
return frequencies
dice_roll_simulation(1000)
常见概率分布
在AI中,我们经常遇到几种重要的概率分布:
from scipy import stats
# 均匀分布
def uniform_distribution_demo():
"""均匀分布演示"""
# 在[a, b]区间上的均匀分布
a, b = 0, 10
uniform_dist = stats.uniform(loc=a, scale=b-a)
# 生成样本
samples = uniform_dist.rvs(size=1000)
# 绘制直方图
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.hist(samples, bins=30, density=True, alpha=0.7, color='blue')
plt.xlabel('值')
plt.ylabel('密度')
plt.title('均匀分布样本直方图')
plt.grid(True, alpha=0.3)
# 概率密度函数
x = np.linspace(a-1, b+1, 1000)
pdf = uniform_dist.pdf(x)
plt.subplot(1, 2, 2)
plt.plot(x, pdf, 'r-', linewidth=2)
plt.xlabel('x')
plt.ylabel('概率密度')
plt.title('均匀分布概率密度函数')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
print(f"均匀分布 U({a}, {b}) 的均值: {uniform_dist.mean():.4f}")
print(f"理论均值: {(a+b)/2:.4f}")
# 正态分布(高斯分布)
def normal_distribution_demo():
"""正态分布演示"""
# 参数
mu, sigma = 100, 15 # 均值和标准差
normal_dist = stats.norm(loc=mu, scale=sigma)
# 生成样本
samples = normal_dist.rvs(size=1000)
# 绘制直方图
plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.hist(samples, bins=30, density=True, alpha=0.7, color='green')
plt.xlabel('值')
plt.ylabel('密度')
plt.title('正态分布样本直方图')
plt.grid(True, alpha=0.3)
# 概率密度函数
x = np.linspace(mu-4*sigma, mu+4*sigma, 1000)
pdf = normal_dist.pdf(x)
plt.subplot(1, 2, 2)
plt.plot(x, pdf, 'r-', linewidth=2)
plt.xlabel('x')
plt.ylabel('概率密度')
plt.title(f'正态分布概率密度函数 N({mu}, {sigma}²)')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
print(f"正态分布 N({mu}, {sigma}²) 的样本统计:")
print(f"样本均值: {np.mean(samples):.4f}")
print(f"样本标准差: {np.std(samples):.4f}")
print(f"理论均值: {mu}")
print(f"理论标准差: {sigma}")
uniform_distribution_demo()
print("\n" + "="*50 + "\n")
normal_distribution_demo()
链式法则:深度学习的核心
链式法则是反向传播算法的基础,是理解深度学习的关键。
链式法则的数学原理
对于复合函数 ,其导数为:
对于多层复合函数 :
# 链式法则可视化示例
def chain_rule_demo():
"""链式法则演示"""
# 定义复合函数: y = sin(x^2)
# 内层函数: u = x^2
# 外层函数: y = sin(u)
# dy/dx = cos(u) * 2x = cos(x^2) * 2x
x = np.linspace(-3, 3, 100)
# 内层函数
u = x**2
# 外层函数
y = np.sin(u)
# 直接求导: dy/dx = cos(x^2) * 2x
dy_dx_direct = np.cos(x**2) * 2 * x
# 使用链式法则: dy/dx = (dy/du) * (du/dx)
dy_du = np.cos(u) # dy/du = cos(u)
du_dx = 2 * x # du/dx = 2x
dy_dx_chain = dy_du * du_dx
# 数值验证
print("链式法则验证:")
print(f"直接求导结果 (x=1): {np.cos(1**2) * 2 * 1:.6f}")
print(f"链式法则结果 (x=1): {np.cos(1) * 2 * 1:.6f}")
print("两种方法结果一致!")
# 可视化
fig, axes = plt.subplots(2, 2, figsize=(15, 10))
# 原始函数
axes[0, 0].plot(x, y, 'b-', linewidth=2, label='y = sin(x²)')
axes[0, 0].set_xlabel('x')
axes[0, 0].set_ylabel('y')
axes[0, 0].set_title('复合函数 y = sin(x²)')
axes[0, 0].legend()
axes[0, 0].grid(True, alpha=0.3)
# 内层函数
axes[0, 1].plot(x, u, 'g-', linewidth=2, label='u = x²')
axes[0, 1].set_xlabel('x')
axes[0, 1].set_ylabel('u')
axes[0, 1].set_title('内层函数 u = x²')
axes[0, 1].legend()
axes[0, 1].grid(True, alpha=0.3)
# 导数对比
axes[1, 0].plot(x, dy_dx_direct, 'r-', linewidth=2, label='dy/dx (直接求导)')
axes[1, 0].plot(x, dy_dx_chain, 'b--', linewidth=2, label='dy/dx (链式法则)')
axes[1, 0].set_xlabel('x')
axes[1, 0].set_ylabel('dy/dx')
axes[1, 0].set_title('导数对比')
axes[1, 0].legend()
axes[1, 0].grid(True, alpha=0.3)
# 链式法则分解
axes[1, 1].plot(x, dy_du, 'g-', linewidth=2, label='dy/du = cos(u)')
axes[1, 1].plot(x, du_dx, 'orange', linewidth=2, label='du/dx = 2x')
axes[1, 1].set_xlabel('x')
axes[1, 1].set_ylabel('值')
axes[1, 1].set_title('链式法则分解')
axes[1, 1].legend()
axes[1, 1].grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
chain_rule_demo()
链式法则在神经网络中的应用
在神经网络中,链式法则用于计算损失函数对每一层权重的梯度:
# 简化的神经网络反向传播示例
def neural_network_backprop_demo():
"""神经网络反向传播中的链式法则"""
# 假设一个简单的两层网络
# 输入 x -> 隐藏层 h -> 输出 y
# h = sigmoid(W1 * x + b1)
# y = W2 * h + b2
# Loss = (y - target)^2
x = 2.0 # 输入
W1, b1 = 1.0, 0.5 # 第一层权重和偏置
W2, b2 = 0.8, 0.2 # 第二层权重和偏置
target = 1.0 # 目标值
# 前向传播
z1 = W1 * x + b1
h = 1 / (1 + np.exp(-z1)) # sigmoid
z2 = W2 * h + b2
y = z2
loss = (y - target)**2
print("前向传播:")
print(f"输入 x = {x}")
print(f"隐藏层输出 h = {h:.4f}")
print(f"输出 y = {y:.4f}")
print(f"损失 Loss = {loss:.4f}")
# 反向传播(使用链式法则)
# dLoss/dW2 = dLoss/dy * dy/dW2
dLoss_dy = 2 * (y - target)
dy_dW2 = h
dLoss_dW2 = dLoss_dy * dy_dW2
# dLoss/dW1 = dLoss/dy * dy/dh * dh/dz1 * dz1/dW1
dy_dh = W2
dh_dz1 = h * (1 - h) # sigmoid的导数
dz1_dW1 = x
dLoss_dW1 = dLoss_dy * dy_dh * dh_dz1 * dz1_dW1
print("\n反向传播(链式法则):")
print(f"dLoss/dW2 = {dLoss_dW2:.4f}")
print(f"dLoss/dW1 = {dLoss_dW1:.4f}")
print("\n这就是神经网络训练中梯度下降的基础!")
neural_network_backprop_demo()
贝叶斯定理:概率推理的核心
贝叶斯定理是概率论中最重要的定理之一,在机器学习中用于:
- 朴素贝叶斯分类器
- 贝叶斯优化
- 贝叶斯神经网络
- 不确定性量化
贝叶斯定理的数学表达
其中:
- 是后验概率
- 是似然
- 是先验概率
- 是证据
# 贝叶斯定理示例
def bayes_theorem_demo():
"""贝叶斯定理演示:疾病诊断问题"""
# 假设某种疾病的患病率为1%
P_disease = 0.01
P_no_disease = 0.99
# 如果患病,检测呈阳性的概率为95%
P_positive_given_disease = 0.95
# 如果没有患病,检测呈阳性的概率为5%(假阳性)
P_positive_given_no_disease = 0.05
# 计算检测呈阳性的总概率
P_positive = (P_positive_given_disease * P_disease +
P_positive_given_no_disease * P_no_disease)
# 使用贝叶斯定理计算:如果检测呈阳性,实际患病的概率
P_disease_given_positive = (P_positive_given_disease * P_disease) / P_positive
print("贝叶斯定理应用:疾病诊断")
print("=" * 60)
print(f"疾病患病率: {P_disease*100}%")
print(f"检测准确率(真阳性): {P_positive_given_disease*100}%")
print(f"假阳性率: {P_positive_given_no_disease*100}%")
print(f"\n如果检测呈阳性,实际患病的概率: {P_disease_given_positive*100:.2f}%")
print("\n这个结果可能出人意料,但这是贝叶斯定理的正确应用!")
# 可视化
fig, axes = plt.subplots(1, 2, figsize=(15, 5))
# 先验概率
axes[0].bar(['患病', '未患病'], [P_disease, P_no_disease],
color=['red', 'green'], alpha=0.7)
axes[0].set_ylabel('概率')
axes[0].set_title('先验概率')
axes[0].set_ylim([0, 1])
# 后验概率(检测呈阳性后)
P_no_disease_given_positive = 1 - P_disease_given_positive
axes[1].bar(['患病', '未患病'],
[P_disease_given_positive, P_no_disease_given_positive],
color=['red', 'green'], alpha=0.7)
axes[1].set_ylabel('概率')
axes[1].set_title('后验概率(检测呈阳性后)')
axes[1].set_ylim([0, 1])
plt.tight_layout()
plt.show()
return P_disease_given_positive
bayes_theorem_demo()
更多概率分布
除了均匀分布和正态分布,机器学习中还常用到其他分布:
# 更多概率分布
def additional_distributions():
"""展示更多常用的概率分布"""
fig, axes = plt.subplots(2, 3, figsize=(18, 10))
# 1. 二项分布
n, p = 20, 0.5
x_binom = np.arange(0, n+1)
pmf_binom = stats.binom.pmf(x_binom, n, p)
axes[0, 0].bar(x_binom, pmf_binom, alpha=0.7, color='blue')
axes[0, 0].set_title(f'二项分布 B({n}, {p})')
axes[0, 0].set_xlabel('成功次数')
axes[0, 0].set_ylabel('概率')
axes[0, 0].grid(True, alpha=0.3)
# 2. 泊松分布
lambda_poisson = 5
x_poisson = np.arange(0, 20)
pmf_poisson = stats.poisson.pmf(x_poisson, lambda_poisson)
axes[0, 1].bar(x_poisson, pmf_poisson, alpha=0.7, color='green')
axes[0, 1].set_title(f'泊松分布 λ={lambda_poisson}')
axes[0, 1].set_xlabel('事件次数')
axes[0, 1].set_ylabel('概率')
axes[0, 1].grid(True, alpha=0.3)
# 3. 指数分布
lambda_exp = 2
x_exp = np.linspace(0, 5, 100)
pdf_exp = stats.expon.pdf(x_exp, scale=1/lambda_exp)
axes[0, 2].plot(x_exp, pdf_exp, 'r-', linewidth=2)
axes[0, 2].set_title(f'指数分布 λ={lambda_exp}')
axes[0, 2].set_xlabel('x')
axes[0, 2].set_ylabel('概率密度')
axes[0, 2].grid(True, alpha=0.3)
# 4. Beta分布
alpha_beta, beta_beta = 2, 5
x_beta = np.linspace(0, 1, 100)
pdf_beta = stats.beta.pdf(x_beta, alpha_beta, beta_beta)
axes[1, 0].plot(x_beta, pdf_beta, 'purple', linewidth=2)
axes[1, 0].set_title(f'Beta分布 α={alpha_beta}, β={beta_beta}')
axes[1, 0].set_xlabel('x')
axes[1, 0].set_ylabel('概率密度')
axes[1, 0].grid(True, alpha=0.3)
# 5. 卡方分布
df_chi2 = 5
x_chi2 = np.linspace(0, 20, 100)
pdf_chi2 = stats.chi2.pdf(x_chi2, df_chi2)
axes[1, 1].plot(x_chi2, pdf_chi2, 'orange', linewidth=2)
axes[1, 1].set_title(f'卡方分布 df={df_chi2}')
axes[1, 1].set_xlabel('x')
axes[1, 1].set_ylabel('概率密度')
axes[1, 1].grid(True, alpha=0.3)
# 6. t分布
df_t = 10
x_t = np.linspace(-4, 4, 100)
pdf_t = stats.t.pdf(x_t, df_t)
pdf_normal = stats.norm.pdf(x_t, 0, 1)
axes[1, 2].plot(x_t, pdf_t, 'b-', linewidth=2, label=f't分布 df={df_t}')
axes[1, 2].plot(x_t, pdf_normal, 'r--', linewidth=2, label='标准正态分布')
axes[1, 2].set_title('t分布 vs 标准正态分布')
axes[1, 2].set_xlabel('x')
axes[1, 2].set_ylabel('概率密度')
axes[1, 2].legend()
axes[1, 2].grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
print("\n各种概率分布在机器学习中的应用:")
print("- 二项分布: 二分类问题、A/B测试")
print("- 泊松分布: 计数数据、事件发生频率")
print("- 指数分布: 等待时间、生存分析")
print("- Beta分布: 概率的分布、贝叶斯推断")
print("- 卡方分布: 假设检验、方差分析")
print("- t分布: 小样本统计推断")
additional_distributions()
数学在AI中的实际应用
让我们通过一个简单的线性回归例子,看看这些数学知识如何在AI中应用:
# 简单线性回归示例
def simple_linear_regression():
"""简单线性回归演示"""
# 生成示例数据
np.random.seed(42)
x = np.linspace(0, 10, 50)
y = 2 * x + 1 + np.random.normal(0, 2, 50) # y = 2x + 1 + 噪声
# 计算回归系数 (最小二乘法)
# 公式: slope = Σ((xi - x_mean)(yi - y_mean)) / Σ((xi - x_mean)²)
# intercept = y_mean - slope * x_mean
x_mean = np.mean(x)
y_mean = np.mean(y)
numerator = np.sum((x - x_mean) * (y - y_mean))
denominator = np.sum((x - x_mean) ** 2)
slope = numerator / denominator
intercept = y_mean - slope * x_mean
print("线性回归结果:")
print(f"计算得到的斜率: {slope:.4f}")
print(f"计算得到的截距: {intercept:.4f}")
print("理论值 - 斜率: 2.0000, 截距: 1.0000")
# 预测值
y_pred = slope * x + intercept
# 绘制结果
plt.figure(figsize=(10, 6))
plt.scatter(x, y, alpha=0.6, label='原始数据')
plt.plot(x, y_pred, 'r-', linewidth=2, label=f'拟合直线: y = {slope:.2f}x + {intercept:.2f}')
plt.xlabel('x')
plt.ylabel('y')
plt.title('简单线性回归')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
# 计算误差
mse = np.mean((y - y_pred) ** 2)
print(f"均方误差 (MSE): {mse:.4f}")
simple_linear_regression()
本周学习总结
今天我们深入学习了AI算法中重要的数学基础:
-
微积分初步
- 函数概念和表示
- 导数的几何意义和计算
- 偏导数在多变量函数中的应用
- 导数在优化问题中的关键作用
- 链式法则:深度学习的核心,反向传播的基础
- 梯度下降算法的原理和实现
-
概率论初步
- 概率的基本概念和计算
- 随机变量的定义和类型
- 常见的概率分布(均匀分布、正态分布)
- 条件概率和实际应用
- 贝叶斯定理:概率推理的核心,在机器学习中的广泛应用
- 更多概率分布:二项分布、泊松分布、指数分布、Beta分布、卡方分布、t分布
-
数学在AI中的应用
- 通过具体例子展示了数学知识如何应用于AI算法
- 链式法则在神经网络反向传播中的应用
- 贝叶斯定理在分类和优化中的应用
graph TD
A[数学基础] --> B[微积分]
A --> C[概率论]
B --> D[导数]
B --> E[偏导数]
B --> F[优化应用]
C --> G[概率概念]
C --> H[随机变量]
C --> I[概率分布]
课后练习
- 运行本节所有代码示例,理解每个数学概念的含义
- 修改线性回归示例中的参数,观察拟合结果的变化
- 尝试实现一个计算函数二阶导数的程序
- 用Python模拟掷两个骰子的实验,计算点数之和的概率分布
下节预告
下一节我们将开始接触实际的AI案例,通过体验早期AI案例来加深对AI技术的理解,包括感知机和简单的搜索算法应用,敬请期待!
有任何疑问请在讨论区留言,我们会定期回复大家的问题。