python - sklearn.metrics.r2_score 的工作原理

我尝试从 https://en.wikipedia.org/wiki/Coefficient_of_determination#Definitions 实现公式,但结果不同。为什么会这样?

y_true = np.array([1, 1, 0])
y_pred = np.array([1, 0, 1])

r2 = r2_score(y_true, y_pred)
print(r2)

y_true_mean = statistics.mean(y_true)
r2 = 1 - np.sum((y_true - y_pred) ** 2) / np.sum((y_true - y_true_mean) ** 2)
print(r2)

-1.9999999999999996

0.0

回答1

不确定您使用什么 statistics 包,但似乎不同的结果源自那里。尝试使用 np.mean 代替。这给出了与 sklearn 相同的 R2:

import numpy as np

y_true = np.array([1, 1, 0])
y_pred = np.array([1, 0, 1])

y_true_mean = np.mean(y_true)
r2 = 1 - np.sum((y_true - y_pred) ** 2) / np.sum((y_true - y_true_mean) ** 2)
print(r2)

https://tio.run/##bYwxCsMwDEV3n0KjFdomceeepJRgiKEZrAjFHnx6V8GmQylokN77@lzSe6d7rVvkXRJQjlzAH0BsTFmS5AAPPW5exBf7nC@gM71QJUtYf@WkXmV/XWLw1CLnZhtFI07hDNdTHDnaLhS0VoRhAIcw/gt8i3vKsGyUrDis9QM

相似文章

随机推荐

最新文章