Model Validation in Python
Kasey Jones
Data Scientist
$$ MAE = \frac{\sum_{i=1}^{n} |y_i - \hat{y}_i|}{n} $$
$$ MSE = \frac{\sum_{i=1}^{n} (y_i - \hat{y}_i) ^2}{n} $$
rfr = RandomForestRegressor(n_estimators=500, random_state=1111) rfr.fit(X_train, y_train) test_predictions = rfr.predict(X_test)
sum(abs(y_test - test_predictions))/len(test_predictions)
9.99
from sklearn.metrics import mean_absolute_error
mean_absolute_error(y_test, test_predictions)
9.99
sum(abs(y_test - test_predictions)**2)/len(test_predictions)
141.4
from sklearn.metrics import mean_squared_error
mean_squared_error(y_test, test_predictions)
141.4
chocolate_preds = rfr.predict(X_test[X_test[:, 1] == 1])
mean_absolute_error(y_test[X_test[:, 1] == 1], chocolate_preds)
8.79
nonchocolate_preds = rfr.predict(X_test[X_test[:, 1] == 0])
mean_absolute_error(y_test[X_test[:, 1] == 0], nonchocolate_preds)
10.99
Model Validation in Python