Scipy误差函数详解

误差函数简介

误差函数的实质是正态分布的概率,其重要性可见一斑,其表达式为

erf ⁡ = 2 π ∫ 0 x e − t 2 d t \operatorname{erf}=\frac{2}{\sqrt{\pi}}\int^x_0e^{-t^2}\text dt erf=π 20xet2dt

和标准正态分布累积分布函数 Φ \Phi Φ的关系为

Φ ( x ) = 1 2 + 1 2 erf ⁡ ( x 2 ) \Phi(x)=\frac{1}{2}+\frac{1}{2}\operatorname{erf}(\frac{x}{\sqrt{2}}) Φ(x)=21+21erf(2 x)

scipy中提供了erf函数,下面画一下误差函数的图像

import numpy as np
import matplotlib.pyplot as plt
import scipy.special as ss
xs = np.arange(-3000, 3000)/1000
plt.plot(xs, ss.erf(xs))
plt.show()

在这里插入图片描述

由于误差函数在概率统计中的含义是累积分布函数,所以不可能是发散的。其泰勒展开也收敛,其泰勒级数为

erf ⁡ ( x ) = 2 π ∑ n = 0 ∞ ( − 1 ) n z 2 n + 1 n ! ( 2 n + 1 ) \operatorname{erf}(x)=\frac{2}{\sqrt\pi}\sum^\infty_{n=0}\frac{(-1)^nz^{2n+1}}{n!(2n+1)} erf(x)=π 2n=0n!(2n+1)(1)nz2n+1

复平面上的误差函数

误差函数在复平面上也有定义,而且图像十分精彩

xs, ys = np.indices([500,500])/100-2.5
zs = ss.erf(xs + 1j*ys)
fig = plt.figure()

ax = fig.add_subplot(1,3,1,projection='3d')
ax.plot_surface(xs, ys, np.real(zs))
ax.set_title("real(zs)")

ax = fig.add_subplot(1,3,2,projection='3d')
ax.plot_surface(xs, ys, np.imag(zs))
ax.set_title("imag(zs)")

ax = fig.add_subplot(1,3,3,projection='3d')
ax.plot_surface(xs, ys, np.abs(zs))
ax.set_title("abs(zs)")

plt.show()

图像为

在这里插入图片描述

如果把其实部或者虚部画成为彩图可能更加带感

xs, ys = np.indices([400,400])/100-2
zs = ss.erf(xs + 1j*ys)
plt.imshow(np.real(zs), cmap=plt.cm.prism)
plt.axis('off')
plt.show()

得到

在这里插入图片描述

与误差函数相关的函数

scipy中,定义了一些列和误差函数相关的函数,

函数与erf的关系
erfc1 - erf(x)
erfcxexp(x**2) * erfc(x)
erfi-i erf(i z)
erfinv误差函数翻转
erfcinverfc翻转

下面逐一演示这3对函数

fDct = {
    "erf":ss.erf,
    "erfinv":ss.erfinv,
    "erfi":ss.erfi,
    "erfc":ss.erfc,
    "erfcinv":ss.erfcinv,
    "erfcx":ss.erfcx
}

fig = plt.figure()
xs = np.arange(-3000, 3000)/1000
for i,key in enumerate(fDct):
    ax = fig.add_subplot(2,3,i+1)
    ax.plot(xs, fDct[key](xs))
    ax.set_title(key)

plt.show()

对比如下

在这里插入图片描述

### 损失函数的概念 损失函数是一种用于评估机器学习模型性能的重要工具。它通过量化预测值与实际值之间的差异来反映模型的表现质量[^1]。具体而言,损失函数越低,则表示模型的预测能力越好。 在训练过程中,优化算法的目标是最小化损失函数的值。这是因为较小的损失意味着模型能够更精确地拟合数据并做出更好的预测[^2]。 --- ### 常见的损失函数及其适用场景 以下是几种常见的损失函数以及它们的应用领域: #### 1. **均方误差 (Mean Squared Error, MSE)** MSE 是回归问题中最常用的损失函数之一。它的定义如下: \[ L(y,\hat{y}) = \frac{1}{n} \sum_{i=1}^{n}(y_i-\hat{y}_i)^2 \] 其中 \( y \) 表示真实值,\( \hat{y} \) 表示预测值,\( n \) 表示样本数量。由于平方项的存在,较大的错误会被放大,因此该方法对异常值较为敏感。 ```python import numpy as np def mse_loss(y_true, y_pred): return ((y_true - y_pred) ** 2).mean() ``` #### 2. **平均绝对误差 (Mean Absolute Error, MAE)** MAE 同样适用于回归问题,但它不会像 MSE 那样惩罚较大偏差。其公式为: \[ L(y,\hat{y}) = \frac{1}{n}\sum_{i=1}^{n}|y_i-\hat{y}_i| \] 相比 MSE,MAE 对异常值的影响更为稳健。 ```python def mae_loss(y_true, y_pred): return abs(y_true - y_pred).mean() ``` #### 3. **交叉熵损失 (Cross-Entropy Loss)** 交叉熵主要用于分类任务,尤其是多类别分类问题。对于二元分类问题,可以采用 sigmoid 函数配合 binary cross-entropy;而对于多类别分类则常使用 softmax 和 categorical cross-entropy。其基本形式为: \[ L(y,\hat{y}) = -\frac{1}{n}\sum_{i=1}^{n}[y_i\log(\hat{y}_i)+(1-y_i)\log(1-\hat{y}_i)] \] 此公式的前提是目标变量已被编码成概率分布的形式。 ```python from scipy.special import log_softmax def cross_entropy_loss(y_true, logits): log_probs = log_softmax(logits, axis=-1) loss = -(y_true * log_probs).sum(axis=-1).mean() return loss ``` #### 4. **Huber 损失** 当面对可能含有噪声的数据集时,Huber 损失提供了一种折衷方案。它结合了 MSE 的平滑性和 MAE 的鲁棒性特点。如果残差小于某个阈值 δ,则按二次方式增长;否则线性增加。 ```python def huber_loss(y_true, y_pred, delta=1.0): error = y_true - y_pred is_small_error = abs(error) <= delta squared_loss = 0.5 * (error ** 2) linear_loss = delta * (abs(error) - 0.5 * delta) return tf.where(is_small_error, squared_loss, linear_loss).mean() ``` --- ### 如何选择合适的损失函数? 选择适当的损失函数取决于以下几个因素: - 数据特性:是否存在大量离群点? - 业务需求:是否希望某些类型的误判受到更多关注? - 计算效率:复杂度较高的损失函数可能会延长训练时间。 例如,在金融风控领域,假阳性可能导致不必要的经济损失,此时可考虑调整权重参数使模型更加注重减少此类失误。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微小冷

请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值