user9471916
user9471916

Reputation: 33

Converting log values back to number

I am using FbProphet and we should covert the data to log values in orer to normalize the data like this:df['y'] = np.log(df['y']) But once I have predicted the values, then I get a dataframe like this:

      ds        n_tickets   yhat
0 2018-02-17       2202  7.545468
1 2018-02-18       2449  7.703022
2 2018-02-19       2409  7.705301
3 2018-02-20       2364  7.675143
4 2018-02-21       2306  7.693359
5 2018-02-22       2492  7.728534
6 2018-02-23       2300  7.669022
7 2018-02-24       2359  7.534430
8 2018-02-25       2481  7.691983
9 2018-02-26       2446  7.694263

Here, yhat are my predicted values but they are log values and n_tickets are my actual values. So, I need to convert yhat back to normal number to make a comparison. I am trying to find out but getting confused.

Upvotes: 3

Views: 12904

Answers (1)

John Karasinski
John Karasinski

Reputation: 1006

Following up on the answer provided by Lambda, the full solution is:

from io import StringIO
import numpy as np
import pandas as pd

s = """
      ds        n_tickets   yhat
0 2018-02-17       2202  7.545468
1 2018-02-18       2449  7.703022
2 2018-02-19       2409  7.705301
3 2018-02-20       2364  7.675143
4 2018-02-21       2306  7.693359
5 2018-02-22       2492  7.728534
6 2018-02-23       2300  7.669022
7 2018-02-24       2359  7.534430
8 2018-02-25       2481  7.691983
9 2018-02-26       2446  7.694263
"""

# Load your example
df = pd.read_csv(StringIO(s), delim_whitespace=True)

# Add the exponential of the yhat column as a new column
df['exp_yhat'] = np.exp(df['yhat'])
print(df.head())

#            ds  n_tickets      yhat     exp_yhat
# 0  2018-02-17       2202  7.545468  1892.148056
# 1  2018-02-18       2449  7.703022  2215.031714
# 2  2018-02-19       2409  7.705301  2220.085527
# 3  2018-02-20       2364  7.675143  2154.131705
# 4  2018-02-21       2306  7.693359  2193.730943

Upvotes: 3

Related Questions