python pandas:如何计算导数/梯度

2024-05-19 07:06:53 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有以下两个向量:

In [99]: time_index
Out[99]: 
[1484942413,
 1484942712,
 1484943012,
 1484943312,
 1484943612,
 1484943912,
 1484944212,
 1484944511,
 1484944811,
 1484945110]

In [100]: bytes_in
Out[100]: 
[1293981210388,
 1293981379944,
 1293981549960,
 1293981720866,
 1293981890968,
 1293982062261,
 1293982227492,
 1293982391244,
 1293982556526,
 1293982722320]

其中bytes_in是一个递增计数器,而time_index是一个unix时间戳(epoch)列表。

目标:我想计算的是比特率。

这意味着我将构建一个

In [101]: timeline = pandas.to_datetime(time_index, unit="s")

In [102]: recv = pandas.Series(bytes_in, timeline).resample("300S").mean().ffill().apply(lambda i: i*8)

In [103]: recv
Out[103]: 
2017-01-20 20:00:00    10351849683104
2017-01-20 20:05:00    10351851039552
2017-01-20 20:10:00    10351852399680
2017-01-20 20:15:00    10351853766928
2017-01-20 20:20:00    10351855127744
2017-01-20 20:25:00    10351856498088
2017-01-20 20:30:00    10351857819936
2017-01-20 20:35:00    10351859129952
2017-01-20 20:40:00    10351860452208
2017-01-20 20:45:00    10351861778560
Freq: 300S, dtype: int64

问题:现在,奇怪的是,手动计算梯度会给出:

In [104]: (bytes_in[1]-bytes_in[0])*8/300
Out[104]: 4521.493333333333

这是正确的值。。

当我用熊猫计算梯度时

In [124]: recv.diff()
Out[124]: 
2017-01-20 20:00:00          NaN
2017-01-20 20:05:00    1356448.0
2017-01-20 20:10:00    1360128.0
2017-01-20 20:15:00    1367248.0
2017-01-20 20:20:00    1360816.0
2017-01-20 20:25:00    1370344.0
2017-01-20 20:30:00    1321848.0
2017-01-20 20:35:00    1310016.0
2017-01-20 20:40:00    1322256.0
2017-01-20 20:45:00    1326352.0
Freq: 300S, dtype: float64

与上述不同,1356448.0与4521.493333333333

你能告诉我我做错了什么吗?


Tags: inpandasindexbytestime计数器unixout
3条回答

pd.Series.diff()只接受差异。它也不除以指数的增量。

这给了你答案

recv.diff() / recv.index.to_series().diff().dt.total_seconds()

2017-01-20 20:00:00            NaN
2017-01-20 20:05:00    4521.493333
2017-01-20 20:10:00    4533.760000
2017-01-20 20:15:00    4557.493333
2017-01-20 20:20:00    4536.053333
2017-01-20 20:25:00    4567.813333
2017-01-20 20:30:00    4406.160000
2017-01-20 20:35:00    4366.720000
2017-01-20 20:40:00    4407.520000
2017-01-20 20:45:00    4421.173333
Freq: 300S, dtype: float64

您还可以使用numpy.gradient传递bytes_in和期望的增量。这不会将长度减少一个,而是对边进行假设。

np.gradient(bytes_in, 300) * 8

array([ 4521.49333333,  4527.62666667,  4545.62666667,  4546.77333333,
        4551.93333333,  4486.98666667,  4386.44      ,  4387.12      ,
        4414.34666667,  4421.17333333])

由于Pandas系列/DataFrame中没有内置的derivative方法,因此可以使用https://github.com/scls19fr/pandas-helper-calc

它将为Pandas系列和数据帧提供一个名为calc的新访问器来计算数值导数和积分。

所以你可以简单地

recv.calc.derivative()

它在引擎盖下使用diff()

一个简单的解释是diff从字面上减去了后面的条目,而np.gradient使用了一个中心差分格式。

相关问题 更多 >

    热门问题