Skip to content

Instantly share code, notes, and snippets.

@nmolivo
Created November 28, 2017 15:35
Show Gist options
  • Save nmolivo/78becadb2a152ff5af84efd38f50b853 to your computer and use it in GitHub Desktop.
Save nmolivo/78becadb2a152ff5af84efd38f50b853 to your computer and use it in GitHub Desktop.
My proposed solution to calculating metrics for LOOCV
loo = LeaveOneOut()
ytests = []
ypreds = []
for train_idx, test_idx in loo.split(Xr):
X_train, X_test = X_array[train_idx], X_array[test_idx] #requires arrays
y_train, y_test = y_array[train_idx], y_array[test_idx]
model = LinearRegression()
model.fit(X = X_train, y = y_train)
y_pred = model.predict(X_test)
# there is only one y-test and y-pred per iteration over the loo.split,
# so to get a proper graph, we append them to respective lists.
ytests += list(y_test)
ypreds += list(y_pred)
rr = metrics.r2_score(ytests, ypreds)
ms_error = metrics.mean_squared_error(ytests, ypreds)
print("Leave One Out Cross Validation")
print("R^2: {:.5f}%, MSE: {:.5f}".format(rr*100, ms_error))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment