Derivative of log plot in python

喜夏-厌秋 提交于 2020-01-06 05:48:08

问题


We have the x and y values, and I am taking their log, by logx = np.log10(x) and logy = np.log10(y). I am trying to compute the derivative of logy w.r.t logx, so dlogy/dlogx. I used to do this successfully using numpy gradient, more precisely

derivy = np.gradient(logy,np.gradient(logx))

but for some strange reason it doesn't seem to work anymore yielding the error: "Traceback (most recent call last): File "derivlog.py", line 79, in <module> grady = np.gradient(logy,np.gradient(logx)) File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line 1598, in gradient raise ValueError("distances must be scalars") ValueError: distances must be scalars"

Context: When trying to detect power-laws, of the kind y ~ x^t, given the values of y as a function of x, one wants to exctract essentially the power t, so we take logs which gives log y ~ t*log x and then take the derivative in order to extract t.

Here's a minimal example for recreating the problem: x=[ 3. 4. 5. 6. 7. 8. 9. 10. 11.]

y = [ 1.05654 1.44989 1.7939 2.19024 2.62387 3.01583 3.32106 3.51618 3.68153]

Are there other (more suited) methods in python for taking such numerical derivatives?


回答1:


Thanks to the discussions in the comments the problem with np.gradient has been solved by updating the numpy package from version 1.12.1 to 1.13.3. This update is specially relevant if you are also getting the ValueError "distances must be scalars" when using gradient. Thus, in order to extract the order of the power-law, computing np.gradient(logy,logx) remains a valid option of going about it.



来源:https://stackoverflow.com/questions/48171283/derivative-of-log-plot-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!