机器学习实战3.2之数据重采样和滑动窗口

import numpy as np
import pandas as pd

数据重采样

  • 时间数据由一个频率转换到另一个频率
  • 降采样
  • 升采样
rng = pd.date_range('2019-03-29', periods=30, freq='D')
ts = pd.Series(np.random.randn(len(rng)), index=rng)
print(ts.head())
2019-03-29    0.752499
2019-03-30    0.568424
2019-03-31    0.960766
2019-04-01    0.646402
2019-04-02   -1.343210
Freq: D, dtype: float64
ts.resample('3D').sum()
2019-03-29    2.281689
2019-04-01   -0.134376
2019-04-04   -1.988064
2019-04-07   -1.710176
2019-04-10    3.321513
2019-04-13   -0.980541
2019-04-16   -0.877009
2019-04-19   -1.535649
2019-04-22   -1.511353
2019-04-25   -0.418378
dtype: float64
ts.resample('M').sum()
2019-03-31    2.281689
2019-04-30   -5.834033
Freq: M, dtype: float64
days3ts = ts.resample('3D').mean()
days3ts
2019-03-29    0.760563
2019-04-01   -0.044792
2019-04-04   -0.662688
2019-04-07   -0.570059
2019-04-10    1.107171
2019-04-13   -0.326847
2019-04-16   -0.292336
2019-04-19   -0.511883
2019-04-22   -0.503784
2019-04-25   -0.139459
dtype: float64
print(days3ts.resample('D').asfreq())
2019-03-29    0.760563
2019-03-30         NaN
2019-03-31         NaN
2019-04-01   -0.044792
2019-04-02         NaN
2019-04-03         NaN
2019-04-04   -0.662688
2019-04-05         NaN
2019-04-06         NaN
2019-04-07   -0.570059
2019-04-08         NaN
2019-04-09         NaN
2019-04-10    1.107171
2019-04-11         NaN
2019-04-12         NaN
2019-04-13   -0.326847
2019-04-14         NaN
2019-04-15         NaN
2019-04-16   -0.292336
2019-04-17         NaN
2019-04-18         NaN
2019-04-19   -0.511883
2019-04-20         NaN
2019-04-21         NaN
2019-04-22   -0.503784
2019-04-23         NaN
2019-04-24         NaN
2019-04-25   -0.139459
Freq: D, dtype: float64

插值方法:

  • ffill 空值取前面的值
  • bfill 空值取后面的值
  • interpolate 线性取值
days3ts.resample('D').ffill(1) #数字表示填充缺失值的个数
2019-03-29    0.760563
2019-03-30    0.760563
2019-03-31         NaN
2019-04-01   -0.044792
2019-04-02   -0.044792
2019-04-03         NaN
2019-04-04   -0.662688
2019-04-05   -0.662688
2019-04-06         NaN
2019-04-07   -0.570059
2019-04-08   -0.570059
2019-04-09         NaN
2019-04-10    1.107171
2019-04-11    1.107171
2019-04-12         NaN
2019-04-13   -0.326847
2019-04-14   -0.326847
2019-04-15         NaN
2019-04-16   -0.292336
2019-04-17   -0.292336
2019-04-18         NaN
2019-04-19   -0.511883
2019-04-20   -0.511883
2019-04-21         NaN
2019-04-22   -0.503784
2019-04-23   -0.503784
2019-04-24         NaN
2019-04-25   -0.139459
Freq: D, dtype: float64
days3ts.resample('D').interpolate()
2019-03-29    0.760563
2019-03-30    0.492111
2019-03-31    0.223660
2019-04-01   -0.044792
2019-04-02   -0.250757
2019-04-03   -0.456723
2019-04-04   -0.662688
2019-04-05   -0.631812
2019-04-06   -0.600935
2019-04-07   -0.570059
2019-04-08   -0.010982
2019-04-09    0.548094
2019-04-10    1.107171
2019-04-11    0.629165
2019-04-12    0.151159
2019-04-13   -0.326847
2019-04-14   -0.315343
2019-04-15   -0.303840
2019-04-16   -0.292336
2019-04-17   -0.365519
2019-04-18   -0.438701
2019-04-19   -0.511883
2019-04-20   -0.509183
2019-04-21   -0.506484
2019-04-22   -0.503784
2019-04-23   -0.382343
2019-04-24   -0.260901
2019-04-25   -0.139459
Freq: D, dtype: float64

滑动窗口(实用)

df = pd.Series(
    np.random.randn(200),
    index=pd.date_range('2019-3-29', periods=200, freq='D'))
print(df.head())
2019-03-29   -0.318180
2019-03-30    0.720536
2019-03-31   -0.861129
2019-04-01   -0.239880
2019-04-02    1.003837
Freq: D, dtype: float64
r = df.rolling(window=10)
r
Rolling [window=10,center=False,axis=0]
#r.max, r.median, r.std, r.skew, r.sum, r.var
print(r.mean())
2019-03-29         NaN
2019-03-30         NaN
2019-03-31         NaN
2019-04-01         NaN
2019-04-02         NaN
2019-04-03         NaN
2019-04-04         NaN
2019-04-05         NaN
2019-04-06         NaN
2019-04-07    0.014705
2019-04-08    0.248308
2019-04-09    0.343200
2019-04-10    0.457588
2019-04-11    0.506723
2019-04-12    0.300540
2019-04-13    0.400271
2019-04-14    0.408851
2019-04-15    0.332448
2019-04-16    0.049914
2019-04-17    0.232145
2019-04-18    0.067545
2019-04-19   -0.095322
2019-04-20   -0.014605
2019-04-21    0.016297
2019-04-22    0.084544
2019-04-23    0.155432
2019-04-24    0.054461
2019-04-25    0.036726
2019-04-26    0.090883
2019-04-27    0.080384
                ...   
2019-09-15    0.058189
2019-09-16    0.383197
2019-09-17    0.466217
2019-09-18    0.304177
2019-09-19    0.519821
2019-09-20    0.618409
2019-09-21    0.522707
2019-09-22    0.439012
2019-09-23    0.433027
2019-09-24    0.313526
2019-09-25    0.297050
2019-09-26    0.240665
2019-09-27    0.168569
2019-09-28    0.241036
2019-09-29    0.006351
2019-09-30   -0.150190
2019-10-01   -0.143898
2019-10-02   -0.023343
2019-10-03   -0.011604
2019-10-04   -0.254993
2019-10-05    0.058492
2019-10-06   -0.130227
2019-10-07   -0.054155
2019-10-08   -0.011214
2019-10-09    0.197047
2019-10-10    0.105364
2019-10-11    0.045009
2019-10-12    0.011351
2019-10-13   -0.148366
2019-10-14   -0.071507
Freq: D, Length: 200, dtype: float64
import matplotlib.pyplot as plt
plt.figure(figsize=(15,5))
df.plot(style='r--')
df.rolling(window=10).mean().plot(style='b')
<matplotlib.axes._subplots.AxesSubplot at 0x10a838d0>

在这里插入图片描述


  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值