What is the inverse operation of np log and np diff

0 votes
I have used the statement dataTrain = np.log(mdataTrain).diff() in my program. I want to reverse the effects of the statement. How can it be done in Python?
Jun 16, 2020 in Python by kartik
• 37,520 points
6,592 views

1 answer to this question.

0 votes

Hello @kartik,

The reverse will involve taking the cumulative sum and then the exponential. Since pd.Series.diff loses information, namely the first value in a series, you will need to store and reuse this data:

np.random.seed(0)

s = pd.Series(np.random.random(10))

print(s.values)

# [ 0.5488135   0.71518937  0.60276338  0.54488318  0.4236548   0.64589411
#   0.43758721  0.891773    0.96366276  0.38344152]

t = np.log(s).diff()
t.iat[0] = np.log(s.iat[0])
res = np.exp(t.cumsum())

print(res.values)

# [ 0.5488135   0.71518937  0.60276338  0.54488318  0.4236548   0.64589411
#   0.43758721  0.891773    0.96366276  0.38344152]

Hope this is helpful!!

answered Jun 16, 2020 by Niroj
• 82,840 points

Related Questions In Python

0 votes
1 answer

What is the output of and explanation?

It will print concatenated lists. Output would ...READ MORE

answered Jul 20, 2018 in Python by Priyaj
• 58,020 points
839 views
0 votes
0 answers

what is the use of args and kwargs in python?

can you an example for each? READ MORE

May 11, 2019 in Python by Waseem
• 4,540 points
704 views
0 votes
1 answer

what is the output of m = np.array([2,4,6]) n = np.array([2, True, False]) print(m+n)

Hi. @Nandini, The above code will give you ...READ MORE

answered Nov 11, 2020 in Python by Gitika
• 65,770 points
511 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,422 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,042 views
0 votes
1 answer

How do I get the path and name of the file that is currently executing?

Hello @kartik, Since Python 3 is fairly mainstream, ...READ MORE

answered May 11, 2020 in Python by Niroj
• 82,840 points
2,639 views
0 votes
1 answer

What is the equivalent of “none” in django templates?

Hello @kartik, None, False and True all are available ...READ MORE

answered Jul 3, 2020 in Python by Niroj
• 82,840 points
8,897 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP