python中的对数图的导数 [英] Derivative of log plot in python

查看:119
本文介绍了python中的对数图的导数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有xy值,我以logx = np.log10(x)logy = np.log10(y)记录它们的日志.我正在尝试计算逻辑w.r.t logx的导数,因此是dlogy/dlogx.我曾经使用numpy渐变成功地做到这一点,更精确地

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))

但由于某些奇怪的原因,它似乎不再起作用,并产生错误:"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"

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"

上下文:尝试检测幂律时,给定y的值作为x,的函数的y ~ x^t,类型的人想要从本质上吸引功率t,因此我们取给出log y ~ t*log x的日志,然后取导数以便提取t.

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.

这是重现问题的最小示例:x=[ 3. 4. 5. 6. 7. 8. 9. 10. 11.]

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]

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

python中是否还有其他(更合适的)方法可用于获取此类数值导数?

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

推荐答案

由于注释中的讨论,通过将numpy软件包从版本1.12.1更新为1.13.3.,已解决了np.gradient的问题.如果在使用gradient.时也得到了ValueError "distances must be scalars",则特别相关.因此,为了提取幂律的阶数,计算np.gradient(logy,logx)仍然是解决此问题的有效选择.

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.

这篇关于python中的对数图的导数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆