I created a for loop
for generating metrics on my train and test set. However, in order to calculated Root Mean Square Error
(RMSE), I need to either 1) take the sqrt
of Mean Square Error
or 2) set the parameter mean_squared_error(squared = False)
. However, I only want a parameter for the RMSE, not for the MAE or the R2.
If I try the below I, understandably, get an error TypeError: mean_squared_error() missing 2 required positional arguments: 'y_true' and 'y_pred'
because the parentheses should only come in the for loop
.
from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error
#Metrics on train and test
metrics = {
'RMSE' : mean_squared_error,
'MAE' : mean_absolute_error,
'R2' : r2_score
}
#Train and Test
for key in metrics:
i = metrics[key]
train_score = i(y_train, train_predictions)
test_score = i(y_test, y_pred)
print(f'Train set {key}: {train_score:.4f}')
print(f'Test set {key}: {test_score:.4f}')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…