为什么通过 PredictionErrorDisplay 绘制误差与实际值会导致值误差?

问题描述 投票:0回答:1

我使用 sklearn 训练了一个随机森林回归模型,并用它对测试数据集进行了一些预测。当然,模型预测的值与实际值不一样是有误差的;在这种情况下,模型的平均平均误差和均方误差相当高。

我想将错误可视化,以便我可以了解错误是否始终很大,或者是否只有一些异常大的错误推高了平均值。

我尝试使用sklearn的PredictionErrorDisplay函数来执行此操作,但以下代码返回错误消息“ValueError:无法强制转换为Series,长度必须为1”

errors = PredictionErrorDisplay(y_true = test_targets, y_pred = test_predictions)
errors.plot()
plt.savefig('Output.png')
plt.clf()

有人知道我该如何解决这个问题吗?我对错误的理解是,我需要将 PredictionErrorDisplay 创建的对象转换为不同的格式,但我不确定如何做到这一点,或者格式需要准确是什么。

python matplotlib scikit-learn
1个回答
0
投票

错误的常见原因:

  1. 不正确的输入形状:确保y_true和y_pred是具有相同长度的一维数组或列表。如果其中之一是多维的或形状不匹配,sklearn 可能无法正确解释它们。

  2. 直接使用 DataFrame 或 Series:PredictionErrorDisplay 需要直接使用 numpy 数组或列表。如果您传递 pandas Series 或 DataFrames,则可能会遇到此错误。在将它们传递给 PredictionErrorDisplay 之前,将它们转换为 numpy 数组或列表。

如何解决错误:

import numpy as np

# Assuming test_targets and test_predictions are pandas Series or numpy arrays
test_targets_array = np.array(test_targets)
test_predictions_array = np.array(test_predictions)
# Check dimensions
assert test_targets_array.shape == test_predictions_array.shape, \
"Dimensions of y_true and y_pred must match"


from sklearn.inspection import PredictionErrorDisplay

errors= PredictionErrorDisplay(y_true=test_targets_array, y_pred=test_predictions_array)
© www.soinside.com 2019 - 2024. All rights reserved.