了解 MAE、MSE 和 RMSE:机器学习中的关键指标

王林
发布: 2024-08-18 06:10:02
原创
1020 人浏览过

Understanding MAE, MSE, and RMSE: Key Metrics in Machine Learning

In der Welt des maschinellen Lernens ist die Bewertung der Leistung eines Modells von entscheidender Bedeutung. Diese Auswertung hilft uns zu verstehen, wie gut unser Modell Daten vorhersagt oder klassifiziert. Unter den vielen verfügbaren Metriken sind der mittlere absolute Fehler (MAE), der mittlere quadratische Fehler (MSE) und der mittlere quadratische Fehler (RMSE) drei der am häufigsten verwendeten Metriken. Aber warum nutzen wir sie? Was macht sie so wichtig?


1. Mittlerer absoluter Fehler (MAE)

Was ist MAE?

Der mittlere absolute Fehler misst die durchschnittliche Größe der Fehler in einer Reihe von Vorhersagen, ohne deren Richtung zu berücksichtigen. Es ist der Durchschnitt der absoluten Differenzen zwischen vorhergesagten Werten und tatsächlichen Werten.
Understanding MAE, MSE, and RMSE: Key Metrics in Machine LearningUnderstanding MAE, MSE, and RMSE: Key Metrics in Machine Learning

Warum MAE verwenden?

  • Interpretierbarkeit:MAE bietet eine klare, unkomplizierte Interpretation des durchschnittlichen Fehlers. Wenn der MAE im Durchschnitt 5 beträgt, weichen die Vorhersagen des Modells 5 Einheiten von den tatsächlichen Werten ab.
  • **Robustheit: **MAE ist im Vergleich zu MSE und RMSE weniger empfindlich gegenüber Ausreißern, da es die Fehlerterme nicht quadriert.

Wann sollte MAE verwendet werden?

MAE wird bevorzugt, wenn Sie ein direktes Verständnis des durchschnittlichen Fehlers wünschen, ohne die Auswirkungen großer Fehler zu übertreiben. Dies ist besonders nützlich, wenn der Datensatz Ausreißer aufweist oder die Fehlerkosten linear sind.

2. Mittlerer quadratischer Fehler (MSE)

Was ist MSE?

Der mittlere quadratische Fehler ist der Durchschnitt der quadrierten Differenzen zwischen den vorhergesagten und den tatsächlichen Werten.

Understanding MAE, MSE, and RMSE: Key Metrics in Machine LearningUnderstanding MAE, MSE, and RMSE: Key Metrics in Machine Learning

Warum MSE verwenden?

  • Fehlerverstärkung:Durch die Quadrierung der Fehler verleiht MSE größeren Fehlern mehr Gewicht, was es zu einer guten Metrik macht, wenn große Fehler besonders unerwünscht sind.
  • Mathematische Eigenschaften:MSE ist differenzierbar und wird häufig als Verlustfunktion in Optimierungsalgorithmen wie Gradient Descent verwendet, da ihre Ableitung einfach zu berechnen ist. Wann sollte MSE verwendet werden?

MSE wird häufig verwendet, wenn große Fehler problematischer sind als kleine und wenn Sie möchten, dass die Metrik große Abweichungen stärker bestraft. Es wird auch häufig beim Modelltraining verwendet, da es rechentechnisch praktisch ist.

3. Root Mean Squared Error (RMSE)

Was ist RMSE?

Root Mean Squared Error ist die Quadratwurzel des MSE. Es bringt die Metrik wieder auf den ursprünglichen Maßstab der Daten zurück und macht sie einfacher zu interpretieren als MSE.
Understanding MAE, MSE, and RMSE: Key Metrics in Machine LearningUnderstanding MAE, MSE, and RMSE: Key Metrics in Machine Learning

Warum RMSE verwenden?

Interpretierbarkeit im Maßstab: RMSE liegt im Gegensatz zu MSE im gleichen Maßstab wie die Originaldaten, wodurch sie besser interpretierbar sind.
Empfindlich gegenüber großen Fehlern: Wie MSE bestraft auch RMSE große Fehler, da es jedoch auf der Originalskala liegt, kann es eine intuitivere Messung der Fehlergröße liefern.

Wann sollte RMSE verwendet werden?

RMSE wird bevorzugt, wenn Sie eine Metrik wünschen, die große Fehler bestraft, die Ergebnisse aber dennoch in derselben Einheit wie die Originaldaten vorliegen müssen. Es wird häufig in Zusammenhängen verwendet, in denen die Verteilung der Fehlergrößen wichtig ist und es entscheidend ist, dass sie im gleichen Maßstab wie die Daten sind.


Auswahl der richtigen Metrik

Understanding MAE, MSE, and RMSE: Key Metrics in Machine Learning

  • MAE对异常值更加稳健,并给出与数据相同单位的平均误差,方便解读。
  • MSE放大较大的误差,当较大的误差代价特别高昂时非常有用,并且在模型训练中经常用作损失函数。
  • RMSE结合了MSE和MAE的优点,提供了一个错误度量,可以惩罚大错误并保持可解释性。

在实践中,MAE、MSE 和 RMSE 之间的选择取决于当前问题的具体要求。如果您的应用程序需要简单、可解释的指标,MAE 可能是最佳选择。如果需要更严厉地惩罚较大的错误,MSE 或 RMSE 可能更合适。

图形表示

1. 建立和回归模型

以下是我们如何使用回归模型生成 MAE、MSE 和 RMSE 的图形表示:

雷雷

Understanding MAE, MSE, and RMSE: Key Metrics in Machine Learning

2. 剧情说明

  • **蓝点:**这些代表实际的数据点。
  • 红线:这是代表模型预测值的回归线。
  • 灰线:这些虚线代表每个数据点的残差或误差。这些线的长度对应于误差大小。
  • MAE、MSE、RMSE:在图中进行了注释,显示这些值可帮助可视化模型性能的评估方式。

3. 解释

  • MAE:给出与数据相同单位的平均误差,显示数据点到回归线的平均距离。
  • MSE:对误差进行平方,更加强调较大的误差,常用于回归模型的训练。
  • RMSE:提供与原始数据相同规模的指标,使其比MSE更易于解释,同时仍然惩罚较大的错误。

训练机器学习模型

Understanding MAE, MSE, and RMSE: Key Metrics in Machine Learning

训练机器学习模型时,特别是在回归任务中,选择正确的误差度量至关重要,因为它会影响模型的学习方式及其性能评估方式。我们来分解一下MAE、MSE、RMSE在模型训练中的意义:

1.MAE(平均绝对误差)

定义:MAE是预测值和实际值之间绝对差的平均值。

在模型训练中的意义:

  • 对异常值的鲁棒性:与 MSE 和 RMSE 相比,MAE 对异常值不太敏感,因为它平等地对待所有错误,而不对它们进行平方。这意味着在训练过程中,模型的目标是最小化平均误差,而不会过分关注较大的误差。
  • 线性惩罚:MAE的线性性质意味着每个错误对模型学习过程的影响与该错误的大小成正比。
  • Interpretability:MAE与原始数据的单位相同,更容易解释。如果 MAE 为 5,则意味着模型的预测平均偏差 5 个单位。

2.MSE(均方误差)

定义:MSE是预测值和实际值之间的平方差的平均值。

Significance in Model Training:

  • Sensitivity to Outliers: MSE is sensitive to outliers because it squares the error, making larger errors much more significant in the calculation. This causes the model to prioritize reducing large errors during training.
  • Punishing Large Errors: The squaring effect means that the model will penalize larger errors more severely, which can lead to a better fit for most data points but might overfit to outliers.
  • Smooth Gradient: MSE is widely used in optimization algorithms like gradient descent because it provides a smooth gradient, making it easier for the model to converge during training.
  • Model’s Focus on Large Errors: Since large errors have a bigger impact, the model might focus on reducing these at the cost of slightly increasing smaller errors, which can be beneficial if large errors are particularly undesirable in the application.

3. RMSE (Root Mean Squared Error)

Definition: RMSE is the square root of the average of the squared differences between the predicted and actual values.

Significance in Model Training:

  • Balance between MAE and MSE: RMSE retains the sensitivity to outliers like MSE but brings the error metric back to the original scale of the data, making it more interpretable than MSE.
  • Penalizes Large Errors: Similar to MSE, RMSE also penalizes larger errors more due to the squaring process, but because it takes the square root, it doesn’t exaggerate them as much as MSE does.
  • Interpretable Units: Since RMSE is on the same scale as the original data, it’s easier to understand in the context of the problem. For instance, an RMSE of 5 means that on average, the model’s prediction errors are about 5 units away from the actual values.
  • Optimization in Complex Models: RMSE is often used in models where the distribution of errors is important, such as in complex regression models or neural networks.

Visual Example to Show Significance in Model Training:

Let’s consider a graphical representation that shows how these metrics affect the model’s training process.

  • MAE Focuses on Reducing Average Error: Imagine the model adjusting the regression line to minimize the average height of the gray dashed lines (errors) equally for all points.
  • MSE Prioritizes Reducing Large Errors: The model might adjust the line more drastically to reduce the longer dashed lines (larger errors), even if it means increasing some smaller ones.
  • RMSE Balances Both: The model will make adjustments that reduce large errors but will not overemphasize them to the extent of distorting the overall fit.
import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_absolute_error, mean_squared_error # Generate synthetic data with an outlier np.random.seed(42) X = 2 * np.random.rand(100, 1) y = 4 + 3 * X + np.random.randn(100, 1) y[98] = 30 # Adding an outlier # Train a simple linear regression model model = LinearRegression() model.fit(X, y) y_pred = model.predict(X) # Calculate MAE, MSE, and RMSE mae = mean_absolute_error(y, y_pred) mse = mean_squared_error(y, y_pred) rmse = np.sqrt(mse) # Plotting the regression line with errors plt.figure(figsize=(12, 8)) # Scatter plot of actual data points plt.scatter(X, y, color='blue', label='Actual Data') # Regression line plt.plot(X, y_pred, color='red', label='Regression Line') # Highlighting errors (residuals) for i in range(len(X)): plt.vlines(X[i], y[i], y_pred[i], color='gray', linestyle='dashed') # Annotating one of the residual lines plt.text(X[0] + 0.1, (y[0] + y_pred[0]) / 2, 'Error (Residual)', color='gray') # Adding annotations for MAE, MSE, RMSE plt.text(0.5, 20, f'MAE: {mae:.2f}', fontsize=12, bbox=dict(facecolor='white', alpha=0.5)) plt.text(0.5, 18, f'MSE: {mse:.2f}', fontsize=12, bbox=dict(facecolor='white', alpha=0.5)) plt.text(0.5, 16, f'RMSE: {rmse:.2f}', fontsize=12, bbox=dict(facecolor='white', alpha=0.5)) # Titles and labels plt.title('Linear Regression with MAE, MSE, and RMSE - Impact on Model Training') plt.xlabel('X') plt.ylabel('y') plt.legend() plt.show()
登录后复制

Understanding MAE, MSE, and RMSE: Key Metrics in Machine Learning

Explanation:

Outlier Impact: Notice how the model tries to adjust for the outlier in the upper region, which affects MSE and RMSE more significantly.

Model Training Implications:

  • With MAE: The model may place less emphasis on the outlier, leading to a fit that is more balanced but less sensitive to extreme deviations.
  • With MSE and RMSE: The model might adjust more aggressively to minimize the impact of the outlier, which can lead to a more distorted fit if outliers are rare.

Choosing the right approach for training a model depends on the specific problem you’re trying to solve, the nature of your data, and the goals of your model. Here’s a guide to help you decide which metric (MAE, MSE, RMSE) to focus on, along with considerations for training your model:

1. Nature of the Data

Presence of Outliers:

  • MAE: If your data contains outliers, and you don’t want these outliers to disproportionately affect your model, MAE is a good choice. It treats all errors equally, so a few large errors won’t dominate the metric.
  • MSE/RMSE: If outliers are expected and meaningful (e.g., extreme but valid cases), and you want your model to account for them strongly, MSE or RMSE might be more appropriate.

Homogeneous Data:

If your data is relatively homogeneous, without significant outliers, MSE or RMSE can help capture the overall performance, focusing more on the general fit of the model.

2. 模型的目标

可解释性:

  • MAE:提供更容易的解释,因为它与目标变量具有相同的单位。如果原始单位的可解释性很重要,并且您想用简单的术语理解平均误差,那么 MAE 是更好的选择。
  • RMSE:也可以用相同的单位解释,但重点是更多地惩罚较大的错误。

关注较大的错误:

  • MSE/RMSE:如果您更关心较大的错误,因为它们在您的应用程序中成本特别高或风险很大(例如,预测医疗剂量、财务预测),那么 MSE 或 RMSE 应该是您的重点。这些指标对较大的错误进行更多的惩罚,这可以指导模型优先考虑减少重大偏差。
  • MAE:如果您的应用程序平等对待所有错误,并且您不希望模型过度关注大偏差,那么 MAE 是更好的选择。

3. 模型类型和复杂性

简单线性模型:

  • MAE:适用于简单的线性模型,其目标是最小化平均偏差,而不必过多担心异常值。
  • MSE/RMSE:也可以使用,特别是如果模型需要考虑所有数据点,包括极端情况。

复杂模型(例如神经网络、集成方法):

  • MSE/RMSE:这些通常用于更复杂的模型,因为它们提供更平滑的梯度,这对于梯度下降等优化技术至关重要。对较大错误的惩罚也可以帮助微调复杂的模型。

4. 优化与收敛

梯度下降和优化:

  • MSE/RMSE:在优化算法中通常是首选,因为它们提供了平滑且连续的误差表面,这对于梯度下降等方法的有效收敛至关重要。
  • MAE:可能不太平滑,这可能会使优化稍微更具挑战性,尤其是在大型模型中。然而,现代优化技术在许多情况下可以解决这个问题。

5. 背景考虑

应用特定要求:

  • MAE:非常适合需要避免异常值影响或错误成本是线性的应用程序,例如估计交付时间或预测分数。
  • MSE/RMSE:最适合特别不希望出现大错误以及应用程序要求对这些错误进行更高惩罚的情况,例如在高风险的财务预测、安全关键系统中或在竞争环境中优化模型时。

结论:采取哪种方法

  • 如果异常值不是主要问题:使用 MSE 或 RMSE。它们帮助模型注意较大的错误,这在许多应用中至关重要。
  • 如果您想要平衡的方法:RMSE 通常是一个很好的折衷方案,因为它给出了与目标变量相同单位的度量,同时仍然比较小的错误更多地惩罚较大的错误。
  • 如果您需要对异常值的鲁棒性:使用 MAE。它确保异常值不会不成比例地影响模型,使其适合您想要更平衡模型的情况。
  • 对于原始单位的可解释性:MAE 或 RMSE 更容易解释,因为它们与目标变量的单位相同。这在您需要向非技术利益相关者解释结果的领域尤其重要。

在实践中,您可以根据这些考虑因素从一个指标开始,然后进行实验,看看您的模型在每个指标上的表现如何。在训练期间监控多个指标以全面了解模型的性能也很常见。

Medium 文章 - 了解 MAE、MSE 和 RMSE:机器学习中的关键指标

@mondalsabbha

以上是了解 MAE、MSE 和 RMSE:机器学习中的关键指标的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!