系列数据的平滑曲线

系列数据的平滑曲线

本文介绍了绘制 pandas 系列数据的平滑曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据是:

>>> ts = pd.TimeSeries(data,indexconv)
>>> tsgroup = ts.resample('t',how='sum')
>>> tsgroup
2014-11-08 10:30:00    3
2014-11-08 10:31:00    4
2014-11-08 10:32:00    7
  [snip]
2014-11-08 10:54:00    5
2014-11-08 10:55:00    2
Freq: T, dtype: int64
>>> tsgroup.plot()
>>> plt.show()

indexconv是使用datetime.strptime转换的字符串.

indexconv are strings converted using datetime.strptime.

像这样的情节非常前卫(这些不是我的实际情节):

The plot is very edgy like this (these aren't my actual plots):

我如何像这样平滑它:

How can I smooth it out like this:

我了解本文中提到的scipy.interpolate >(这是我从中获取图像的地方),但是如何将其应用于熊猫时间序列?

I know about scipy.interpolate mentioned in this article (which is where I got the images from), but how can I apply it for Pandas time series?

我发现了一个名为 Vincent 的出色库熊猫,但它不支持Python 2.6.

I found this great library called Vincent that deals with Pandas, but it doesn't support Python 2.6.

推荐答案

知道了.在此问题的帮助下,这是我的工作:

Got it. With help from this question, here's what I did:

  1. 将我的tsgroup从几分钟重新采样到几秒钟.

  1. Resample my tsgroup from minutes to seconds.

\>>> tsres = tsgroup.resample('S')
\>>> tsres
2014-11-08 10:30:00     3
2014-11-08 10:30:01   NaN
2014-11-08 10:30:02   NaN
2014-11-08 10:30:03   NaN
...
2014-11-08 10:54:58   NaN
2014-11-08 10:54:59   NaN
2014-11-08 10:55:00     2
Freq: S, Length: 1501

  • 使用 .interpolate(method='cubic') .这会将数据传递到scipy.interpolate.interp1d并使用cubic类型,因此您需要安装scipy(pip install scipy).

  • Interpolate the data using .interpolate(method='cubic'). This passes the data to scipy.interpolate.interp1d and uses the cubic kind, so you need to have scipy installed (pip install scipy) .

    \>>> tsint = tsres.interpolate(method='cubic')
    \>>> tsint
    2014-11-08 10:30:00    3.000000
    2014-11-08 10:30:01    3.043445
    2014-11-08 10:30:02    3.085850
    2014-11-08 10:30:03    3.127220
    ...
    2014-11-08 10:54:58    2.461532
    2014-11-08 10:54:59    2.235186
    2014-11-08 10:55:00    2.000000
    Freq: S, Length: 1501

  • 使用tsint.plot()绘制它.以下是原始tsgrouptsint之间的比较:

  • Plot it using tsint.plot(). Here's a comparison between the original tsgroup and tsint:

    如果.interpolate(method='cubic')提示您即使已安装Scipy也未安装,则出现错误,请打开/usr/lib64/python2.6/site-packages/scipy/interpolate/polyint.py或打开文件的任何位置,然后将第二行从from scipy import factorial更改为from scipy.misc import factorial.

    If you're getting an error from .interpolate(method='cubic') telling you that Scipy isn't installed even if you do have it installed, open up /usr/lib64/python2.6/site-packages/scipy/interpolate/polyint.py or wherever your file might be and change the second line from from scipy import factorial to from scipy.misc import factorial.

    这篇关于绘制 pandas 系列数据的平滑曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-20 01:20