0
$\begingroup$

In this code, I get nan for the test result, what is the reason?

Average Train R^2: 0.504

Average Test R^2: nan

loo = LeaveOneOut()
train_r2_scores = []
test_r2_scores = []
for train_index, test_index in loo.split(X):

    X_train, X_test = X.iloc[train_index], X.iloc[test_index]
    y_train, y_test = y.iloc[train_index], y.iloc[test_index]
    model = LinearRegression()
    model.fit(X_train, y_train)
    y_train_pred = model.predict(X_train)
    train_r2 = r2_score(y_train, y_train_pred)
    train_r2_scores.append(train_r2)

    y_test_pred = model.predict(X_test)
    test_r2 = r2_score(y_test, y_test_pred)
    test_r2_scores.append(test_r2)

print("Average Train R^2:", np.mean(train_r2_scores))
print("Average Test R^2:", np.mean(test_r2_scores))```
$\endgroup$

1 Answer 1

1
$\begingroup$

You are using leave-one-out in the wrong way. A side-effect is the error you are facing, here.

Let me explain

For leave-one-out, the test set consists of exactly one sample in every iteration. This means, you cannot compute a meaningful test score (like $r^2$) on a single test sample.

Especially for the case of $r^2$, the sum of squared errors is divided by the variance of the true values. The variance of a single sample is always zero, which causes your problem.

How to fix this

You need to collect predictions and true values inside the loop and compute the $r^2$-score outside of it:

pred_test = []
true_test = []
for train_index, test_index in loo.split(X):

    # ...

    y_test_pred = model.predict(X_test)
    pred_test.append(y_test_pred)
    true_test.append(y_test

test_r2_score = r2_score(true_test, pred_test)
print("Average Train R^2:", np.mean(train_r2_scores))
print("Test R^2:", test_r2_score )
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.