Fix R2Score num_samples inflation and refactor for readability (#23334)