2
$\begingroup$

For a target variable y, it is transformed using np.log1p. Then a random forest regression model is trained using the transformed y.

Then I tried to use cross_val_score in sklearn to compute the neg_mean_absolute_error. The mean of the returned scores is -0.17. Should I convert it back by using np.emp1(0.17)?

But the error is too small. As I am predicting housing price. The error should be much larger than this.

$\endgroup$
1
  • 2
    $\begingroup$ If I understand correctly, you should first back-transform the results of the regression and then calculate both the price errors and their absolute mean. $\endgroup$ Commented Dec 28, 2019 at 11:27

2 Answers 2

1
$\begingroup$

Rather than transforming the estimate of the generalization error, you're better off using a model which transforms the training data and then is capable of predicting back on the original scale.

This is done with sklearn.compose.TransformTargetRegressor as shown below.

import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.compose import TransformedTargetRegressor
from sklearn.pipeline import Pipeline
from sklearn.model_selection import cross_val_score

N = 1000
x = np.random.normal(size = (N, 1))
y = 10 + x + np.random.normal(size = (N, 1))
y = y.ravel()

model = Pipeline([
    ('ttr', TransformedTargetRegressor(regressor=LinearRegression(), func = np.log1p, inverse_func = lambda x: np.exp(x)-1))
])


cross_val_score(model, x, y, cv=5, scoring = 'neg_mean_squared_error').mean()

>>>-0.997... #Depending on the data used.

Note that our cross validated error is very close to the true standard deviation of the noise (which is 1 in this case) despite the model mispecification (the true conditional mean is linear whereas the model is exponential)

$\endgroup$
0
$\begingroup$

You should be able to calculate the mean absolute error (MAE) using just very basic functions in Python. If y is your target variable and you used your transformed target variable y_trans to train the model, you will get a transformed outcome variable out_trans. To get a meaningful MAE, in housing price e.g., you would have to transform your outcome variable,out_trans, using the inverse transformation function you used in y. Note that this is different from transforming the MAE itself, as you suggested. After that, the MAE is just the mean of the differences between y and out, in module:

import numpy as np
mae = np.mean(abs(y) - abs(out))

You could of course compute the MAE with the transformed quantities, but as you said, the unit won't be that meaningful.

$\endgroup$
1
  • $\begingroup$ Thank you! By using your method, I can compute the MAE for the trained model. But what if I want to compute the average MAE in cross validation? Can I do that in cross_val_score? Or I need to use the pipeline in sklearn $\endgroup$
    – JOHN
    Commented Dec 29, 2019 at 4:29

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.