如何在python中计算数组的导数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16841729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:47:38  来源:igfitidea点击:

How do I compute the derivative of an array in python

pythonpython-2.7

提问by Adams Gwazah

How do I compute the derivative of an array, y (say), with respect to another array, x (say) - both arrays from a certain experiment? eg, y = [1,2,3,4,4,5,6]and x = [.1,.2,.5,.6,.7,.8,.9]; I want to get dy/dx!

我如何计算一个数组 y(比如说)相对于另一个数组 x(比如说)的导数——来自某个实验的两个数组?例如,y = [1,2,3,4,4,5,6]x = [.1,.2,.5,.6,.7,.8,.9];我要得到dy/dx

回答by tsm

I'm assuming this is what you meant:

我假设这就是你的意思:

>>> from __future__ import division
>>> x = [.1,.2,.5,.6,.7,.8,.9]
>>> y = [1,2,3,4,4,5,6]
>>> from itertools import izip
>>> def pairwise(iterable): # question 5389507
...     "s -> (s0,s1), (s2,s3), (s4, s5), ..."
...     a = iter(iterable)
...     return izip(a, a)
... 
>>> for ((a, b), (c, d)) in zip(pairwise(x), pairwise(y)):
...   print (d - c) / (b - a)
... 
10.0
10.0
10.0
>>>

question 5389507 link

问题 5389507 链接

That is, define dxas the difference between adjacent elements in x.

即定义dx为 中相邻元素的差值x

回答by eusoubrasileiro

Use numpy.diff

使用numpy.diff

If dx is constant

如果 dx 是常数

from numpy import diff
dx = 0.1
y = [1, 2, 3, 4, 4, 5, 6]
dy = diff(y)/dx
print dy 
array([ 10.,  10.,  10.,   0.,  10.,  10.])

dx is not constant (your example)

dx 不是常数(你的例子)

from numpy import diff
x = [.1, .2, .5, .6, .7, .8, .9]
y = [1, 2, 3, 4, 4, 5, 6]
dydx = diff(y)/diff(x)
print dydx 
[10., 3.33333,  10. ,   0. , 10. ,  10.]

Note that this approximated "derivative" has size n-1where nis your array/list size.

请注意,这个近似“衍生”有大小n-1这里n是你的数组/列表的大小。

Don't know what you are trying to achieve but here are some ideas.
If you are trying to make numerical differentiationmaybe finite differencesformulation might help you better. The solution above is like a first-order accuracy approximation for the forward schema of finite differenceswith a non-uniform grid/array.

不知道您想要实现什么,但这里有一些想法。
如果您正在尝试进行数值微分,那么有限差分公式可能会更好地帮助您。上面的解决方案类似于具有非均匀网格/阵列的有限差分正向模式的一阶精度近似。

回答by user3554809

numpy.diff(x)computes

numpy.diff(x)计算

the difference between adjacent elements in x

x 中相邻元素之间的差值

just like in the answer by @tsm. As a result you get an array which is 1 element shorter than the original one. This of course makes sense, as you can only start computing the differences from the first index (1 "history element" is needed).

就像@tsm 的回答一样。结果,您得到一个比原始数组短 1 个元素的数组。这当然是有道理的,因为您只能开始计算与第一个索引的差异(需要 1 个“历史元素”)。

>>> x = [1,3,4,6,7,8]
>>> dx = numpy.diff(x)
>>> dx
array([2, 1, 2, 1, 1])

>>> y = [1,2,4,2,3,1]
>>> dy = numpy.diff(y)
>>> dy
array([ 1,  2, -2,  1, -2])

Now you can divide those 2 resulting arrays to get the desired derivative.

现在您可以将这 2 个结果数组相除以获得所需的导数。

>>> d = dy / dx
>>> d
array([ 0.5,  2. , -1. ,  1. , -2. ])

If for some reason, you need a relative (to the y-values) growth, you can do it the following way:

如果出于某种原因,您需要相对(相对于 y 值)增长,您可以通过以下方式进行:

>>> d / y[:-1]
array([ 0.5       ,  1.        , -0.25      ,  0.5       , -0.66666667])

Interpret as 50% growth, 100% growth, -25% growth, etc.

解释为 50% 增长、100% 增长、-25% 增长等。

Full code:

完整代码:

import numpy
x = [1,3,4,6,7,8]
y = [1,2,4,2,3,1]
dx = numpy.diff(x)
dy = numpy.diff(y)
d = dy/dx