In order to judge which model performs best at varying values of \( \lambda \) (for ridge and LASSO) we compute \( R^2 \) which is given by $$ \begin{align} R^2 = 1 - \frac{(y - \hat{y})^2}{(y - \bar{y})^2}, \tag{45} \end{align} $$ where \( y \) is a vector with the true values of the energy, \( \hat{y} \) is the predicted values of \( y \) from the models and \( \bar{y} \) is the mean of \( \hat{y} \).
def r_squared(y, y_hat):
return 1 - np.sum((y - y_hat) ** 2) / np.sum((y - np.mean(y_hat)) ** 2)
This is the same metric used by Scikit-learn for their regression models when scoring.
y_hat = clf.predict(X_test)
r_test = r_squared(y_test, y_hat)
sk_r_test = clf.score(X_test, y_test)
assert abs(r_test - sk_r_test) < 1e-2