1
$\begingroup$

I have been running a log-transform on my target values because the distribution appears to be highly right skewed as you can see in the picture.enter image description here

After having called

df['target'] = np.log(df['target'])

the distribution target looks like this enter image description here

that is way better than before for training a model.

At this point I run the ML process and I train my model on log-scaled targets getting the following predictions (still using the log-scaled targets):

enter image description here

obtained by simply plotting log scaled predictions against log scaled true values, where the red line is the 'ideal' linear relationship between predictions and targets that I'm trying to achieve.

I got an R2 score of 0.40 which is not amazing but is not too bad at the moment.

The problem is, that when I try to get back to the original values by an inverse transform, i.e.

preds = np.exp(model.predict(X_test))
y_test = np.exp(y_test)

then I get the following:

enter image description here

and a R2 score of -0.090 obtained by running

r2_score(y_test, preds)

(hence using the inverse transformed values).

What am I doing wrong?

thank you in advance,

James

$\endgroup$

2 Answers 2

2
$\begingroup$

You should be calculating r2_score on the test data that is transformed in the same manner as the train data. In your case, the data that is transformed with log transformation, as the weights that are learned in the training part work with the transformed data.

Therefore, no need to do: preds = np.exp(model.predict(X_test)) y_test = np.exp(y_test)

Run the s2_score on preds and y_test without the np.exp transformation.

$\endgroup$
2
  • $\begingroup$ Thank you that's what I have done in the end... Now the question is, how should I look at these transformed errors I mean what is the effect of taking the log, it will shrink the errors that I do as well. $\endgroup$ Commented Jan 24, 2022 at 16:16
  • $\begingroup$ I think this link sums up the answer to your question . You can't "shrink" the target without a potential problem with "shrinking". $\endgroup$ Commented Jan 25, 2022 at 21:45
0
$\begingroup$

I got the similar situation when the performance on the log-scaled target is much better than the performance on the original scale. One of the issues is that the performance on the log-scaled target is not good enough (though is much better than the original scale). I finally found a way to improve the performance on the log scale (around 0.7 in terms of r2), leading to much better on the original scale.

$\endgroup$

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.