Skip to content

Instantly share code, notes, and snippets.

@dfuller22
Created September 22, 2020 02:24
Show Gist options
  • Save dfuller22/21e8cc14dbcbd55d22057994e486c8d7 to your computer and use it in GitHub Desktop.
Save dfuller22/21e8cc14dbcbd55d22057994e486c8d7 to your computer and use it in GitHub Desktop.
def regressor_tester(reg_, X_tr, X_te, y_tr, y_te, verbose=False, display_res=False, keep_preds=False):
import pandas as pd
import numpy as np
from sklearn import metrics
## Check if multiple regressors to check
if isinstance(reg_, list):
## Container for multiple regressor results + counter
count = 0
holder = []
## Looping through each regressor
for r in reg_:
## Fit/predict on train/test data
y_hat_train, y_hat_test = fit_n_pred(r, X_tr, X_te, y_tr, show_reg=verbose)
## Store/get training data metrics
tr_mse = metrics.mean_squared_error(y_tr, y_hat_train)
tr_rmse = np.sqrt(metrics.mean_squared_error(y_tr, y_hat_train))
tr_r2 = metrics.r2_score(y_tr, y_hat_train)
## Store/get testing data metrics
te_mse = metrics.mean_squared_error(y_te, y_hat_test)
te_rmse = np.sqrt(metrics.mean_squared_error(y_te, y_hat_test))
te_r2 = metrics.r2_score(y_te, y_hat_test)
## Creating structure for df in list format
reg_res = [['Name', 'Tr_MSE', 'Tr_RMSE', 'Tr_R2', 'Te_MSE', 'Te_RMSE', 'Te_R2'],
[str(type(r)), tr_mse, tr_rmse, tr_r2, te_mse, te_rmse, te_r2]]
## Convert list into dataframe + append to holder
res_df_inner = pd.DataFrame(reg_res[1:], columns=reg_res[0])
holder.append(res_df_inner)
count += 1
## Display counter + merge dfs into one
print(('---'*10), f'{count} regressors evaluated.', ('---'*10))
res_df = pd.concat(holder, ignore_index=True)
## Optional Q.C.
if display_res:
display(res_df)
## Optional return of preds
if keep_preds:
return res_df, y_hat_train, y_hat_test
return res_df
else:
## Fit/predict on train/test data
y_hat_train, y_hat_test = fit_n_pred(reg_, X_tr, X_te, y_tr, show_reg=verbose)
## Store/get training data metrics
tr_mse = metrics.mean_squared_error(y_tr, y_hat_train)
tr_rmse = np.sqrt(metrics.mean_squared_error(y_tr, y_hat_train))
tr_r2 = metrics.r2_score(y_tr, y_hat_train)
## Store/get testing data metrics
te_mse = metrics.mean_squared_error(y_te, y_hat_test)
te_rmse = np.sqrt(metrics.mean_squared_error(y_te, y_hat_test))
te_r2 = metrics.r2_score(y_te, y_hat_test)
## Creating structure for df in list format
reg_res = [['Name', 'Tr_MSE', 'Tr_RMSE', 'Tr_R2', 'Te_MSE', 'Te_RMSE', 'Te_R2'],
[str(type(reg_)), tr_mse, tr_rmse, tr_r2, te_mse, te_rmse, te_r2]]
## Convert list into dataframe
res_df_ = pd.DataFrame(reg_res[1:], columns=reg_res[0])
## Optional Q.C.
if display_res:
display(res_df_)
## Optional return of preds
if keep_preds:
return res_df_, y_hat_train, y_hat_test
return res_df_
## NECESSARY CODE FOR PREDICTION ##
def fit_n_pred(reg_, X_tr, X_te, y_tr, show_reg=True):
## Try/Except if timer object available
try:
timer = Timer()
timer.start(disp_time=False)
except:
print('No timer for fitting.')
## Fit model to training data
reg_.fit(X_tr, y_tr)
## Get training/test predictions
y_hat_trn = reg_.predict(X_tr)
y_hat_tes = reg_.predict(X_te)
## Try/Except if timer available
try:
timer.stop(disp_time=False)
except:
pass
## Optional Q.C.
if show_reg:
display(reg_)
return y_hat_trn, y_hat_tes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment