16

pandas中的窗口对象(窗口函数)

 3 years ago
source link: https://blog.csdn.net/qsx123432/article/details/111396542
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.



引用文章:( Pandas 窗口函数
以下内容是总结记忆版:(个人理解, 如有偏差,请多指教)

窗口函数简介

中文释义: = 窗户。可以通过它对某一个区域进行观测和眺望。

英文释义 = window 。

计算机释义 = 窗口,视窗:
an area within a frame on a computer screen, in which a particular program is operating or in which information of a particular type is shown
(解释:一个框架的区域,在这个区域中一些特殊的程序和信息进行被执性、显示、处理)

为何要引入窗口函数

因为有些特殊的时序数据难以直接进行操作。
窗口函数方便处理时序数据,解决了滞后性和数据分布的不稳定性。
主要用于通过平滑曲线来以图形方式查找数据内的趋势。

基本使用方法

1. 读取或输入时序数据(序列数据)
2. 将数据换成 pd 格式
3. 定义窗口的方式及大小
4. 在指定窗口下执行需要的统计函数

下面将通过这些步骤依次进行展开:

滑动窗口 rolling

话不多说,先上代码,然后带着问题去读解释。
代码段1:

import pandas as pd 
s = pd.Series([1,2,3,4,5])
roller = s.rolling(window=3)
print(roller.mean())

运行结果:

0    NaN
1    NaN
2    2.0
3    3.0
4    4.0

为什么是这样:
这是因为参数 window 设置的原因:
(是 .rolling() 中的必备参数)

window = 数值型int --> 计算统计量的观测值的数量,及从第0个元素开始,
向前数window个,然后在使用后面定义的函数
window  = offset时间偏移量 --> 表示时间窗的大小

程序解释:

s = pd.Series([1,2,3,4,5])	--> [1,2,3,4,5]
经过:s.rolling(window=3)  -->  
		从第0个下标,往前数3个,[空,空 ,1]
		从第1个下标,往前数3个,[空,1 , 2]
		从第2个下标,往前数3个,[1, 2, 3]
		从第3个下标,往前数3个,[2, 3, 4]
		从第4个下标,往前数3个,[3, 4, 5]
然后得到的元素,在经过统计函数,mean()进行计算。
第n个元素的值将是n,n-1和n-2元素的平均值
便形成了:
0    NaN
1    NaN
2    2.0
3    3.0
4    4.0

代码段2:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10, 4),
index = pd.date_range('1/1/2020', periods=10),
columns = ['A', 'B', 'C', 'D'])
print(df)
print (df.rolling(window=3).mean())

自行输入体会

扩张窗口 expanding

.expanding()又叫扩展窗口函数。
不是固定窗口长度,其长度是不断的扩大的。所以才叫expanding (ing现在进行时,expand扩展)–>(时刻在扩展)

rolling(window = 自定义输入)
expanding(min_periods = 自定义输入)

第n个元素的值将是n,n-1,n-2…’1’这n个元素的平均值

类似的函数还有:
sum() 函数 与 consum() 函数 的区别
cummax, cumsum, cumprod 函数是典型的类扩张窗口函数,

代码段1:

import pandas as pd 
s = pd.Series([1,2,3,4,5])
expanding = s.expanding(min_periods=3)
print(expanding.mean())

运行结果:

0    NaN
1    NaN
2    2.0
3    2.5
4    3.0
dtype: float64

代码段2:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10, 4),
      index = pd.date_range('1/1/2018', periods=10),
      columns = ['A', 'B', 'C', 'D'])
print (df.expanding(min_periods=3).mean())

指数加权窗口 ewm

指数 exp()
权重 weight
移动 move
因此组成 ewm() = 指数权重移动起来 = 指数加权移动窗口 (个人理解)

官方详细文档点击查看

常用的设置参数:
com,span,halflife,alpha (最重要的参数)

在这里插入图片描述
ewm类型函数作用ewma指数加权移动平均ewmvar指数加权移动方差ewmstd指数加权移动标准差ewmcorr指数加权移动相关系数ewmcov指数加权移动协方差
import pandas as pd
import numpy as np

df = pd.DataFrame({'T': [0, 1, 2, np.nan, 4]})
print(df)

df1 = df.ewm(com=0.5).mean()
print(df1)

#Specifying times with a timedelta halflife when computing mean
times = pd.date_range(start='2020-12-19', periods=5)
df2 = df.ewm(halflife='4 days', times=pd.DatetimeIndex(times)).mean()
print(df2)
     T
0  0.0
1  1.0
2  2.0
3  NaN
4  4.0
          T
0  0.000000
1  0.750000
2  1.615385
3  1.615385
4  3.670213
          T
0  0.000000
1  0.543214
2  1.114950
3  1.114950
4  2.144696

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK