import numpy as np
from sklearn.metrics import r2_score as skr2_score
def r2_score(y_true, y_pred):
numerator = np.sum((y_true - y_pred) ** 2)
denominator = np.sum((y_true - np.mean(y_true)) ** 2)
return 1 - numerator / denominator
for i in range(10):
rng = np.random.RandomState(i)
y_true = rng.rand(10)
y_pred = rng.rand(10)
score1 = r2_score(y_true, y_pred)
score2 = skr2_score(y_true, y_pred)
assert np.isclose(score1, score2)