DataFrame加权滑窗rolling使用,win_type=‘triang‘详解

@创建于:20210312
@修改于:20210312

1、背景
2、rolling介绍
2.1 DataFrame rolling API介绍

此处对应的pandas 版本号是 1.2.2,pandas.DataFrame.rolling

DataFrame.rolling(window, min_periods=None, center=False, win_type=None, on=None, axis=0, closed=None)

2.2 参数介绍
序号参数英文中文
1window: intSize of the moving window. This is the number of observations used for calculating the statistic. Each window will be a fixed size.移动窗口的大小。这是用于计算统计数据的观测数。每个窗口的大小都是固定的。
2window: offsetIf its an offset then this will be the time period of each window. Each window will be a variable sized based on the observations included in the time-period. This is only valid for datetimelike indexes.如果是偏移量,则这将是每个窗口的时间段。每个窗口的大小将根据时间段中的观察结果而变化。这仅对datetimelike索引有效。
3window: BaseIndexer subclassIf a BaseIndexer subclass is passed, calculates the window boundaries based on the defined get_window_bounds method. Additional rolling keyword arguments, namely min_periods, center, and closed will be passed to get_window_bounds.如果传递了BaseIndexer子类,则根据定义的get_window_bounds方法计算窗口边界。还可以传递其他关键字参数给get_window_bounds,即min_periods、center和closed。
4min_periods int, default NoneMinimum number of observations in window required to have a value (otherwise result is NA). For a window that is specified by an offset, min_periods will default to 1. Otherwise, min_periods will default to the size of the window.窗口中需要有值的最小观察数(否则结果为NA)。对于由偏移量指定的窗口,minu periods将默认为1。否则,min_periods将默认为窗口的大小。
5center bool, default FalseSet the labels at the center of the window.将标签设置在窗口的中心。
6win_type str, default NoneProvide a window type. If win_type=None, all points are evenly weighted; otherwise, win_type can accept a string of any scipy.signal window function…提供窗口类型。如果win_type=None,则所有点的权重相等;否则,win_type可以接受任意值的字符串scipy.signal window function
7on str, optionalFor a DataFrame, a datetime-like column or MultiIndex level on which to calculate the rolling window, rather than the DataFrame’s index. Provided integer column is ignored and excluded from result since an integer index is not used to calculate the rolling window.对于DataFrame类型,datatime类型的列或者MultiIndex类型的用于计算rolling窗口,而不是DataFrame的索引。对于dataframe而言,指定要计算滚动窗口的列,值为列名。
8axis int or str, default 0方向(轴),一般都是0
9closed str, default NoneMake the interval closed on the ‘right’, ‘left’, ‘both’ or ‘neither’ endpoints. Defaults to ‘right’. Changed in version 1.2.0: The closed parameter with fixed windows is now supported.使“right”、“left”、“both”或“neither”端点上的间隔闭合。默认为“right”。 在版本1.2.0中更改:现在支持带有固定窗口的closed参数。(仅支持datetimelike 和 offset 设置的滑窗)
3、例子
3.1 window例子
import pandas as pd
import numpy as np

df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]})
df.rolling(window=2).mean()

	B
0 	NaN
1 	0.5
2 	1.5
3 	NaN
4 	NaN

df = pd.DataFrame({'B': [0, 1, 2, 3, 4, 5]})
df.rolling(window=3, center=True).sum()

 	B
0 	NaN
1 	3.0
2 	6.0
3 	9.0
4 	12.0
5 	NaN
3.2 win_type=‘triang’例子

(1)支持的函数类型
win_type支持的函数名称详见:Window functions (scipy.signal.windows)

在这里插入图片描述
(2)triang数学模型
scipy.signal.windows.triang

在这里插入图片描述
winow = n,且n为奇数
权重分配:
1 ( n + 1 ) / 2 , 2 ( n + 1 ) / 2 , . . . , 1 , . . . , 2 ( n + 1 ) / 2 , 1 ( n + 1 ) / 2 \frac{1}{(n+1)/2},\frac{2}{(n+1)/2}, ...,1,...,\frac{2}{(n+1)/2},\frac{1}{(n+1)/2} (n+1)/21(n+1)/22...1...(n+1)/22(n+1)/21
权重和:
n + 1 2 \frac{n+1}{2} 2n+1

winow = n,且n为偶数
权重分配:
2 n + 2 , 4 n + 2 , . . . , n n + 2 , . . . , 4 n + 2 , 2 n + 2 \frac{2}{n+2},\frac{4}{n+2}, ...,\frac{n}{n+2},...,\frac{4}{n+2},\frac{2}{n+2} n+22n+24...n+2n...n+24n+22
权重和:
n 2 \frac{n}{2} 2n

(3)triang演示

df = pd.DataFrame({'B': [0, 1, 2, 3, 4, 5, 6]})
df.rolling(window=5, win_type='triang').sum()

 	B
0 	NaN
1 	NaN
2 	NaN
3 	NaN
4 	6.0
5 	9.0
6 	12.0

df.rolling(window=5, win_type='triang').mean()

 	B
0 	NaN
1 	NaN
2 	NaN
3 	NaN
4 	2.0
5 	3.0
6 	4.0

df.rolling(window=4, win_type='triang').sum()

	B
0 	NaN
1 	NaN
2 	NaN
3 	3.0
4 	5.0
5 	7.0
6 	9.0

df.rolling(window=4, win_type='triang').mean()

 	B
0 	NaN
1 	NaN
2 	NaN
3 	1.5
4 	2.5
5 	3.5
6 	4.5

(4)其他函数数学模型

scipy.signal.windows.hann
在这里插入图片描述
在这里插入图片描述scipy.signal.windows.cosine

在这里插入图片描述

scipy.signal.windows.gaussian

在这里插入图片描述
在这里插入图片描述scipy.signal.windows.boxcar
在这里插入图片描述

4、参考链接

pandas.DataFrame.rolling

scipy.signal.windows.triang

时间序列分析 - 移动平均SMA, EMA(EWMA) 之python

这是一个用于滚动预测的函数,它采用一个预测模型,以及一些输入特征和目标变量来生成预测结果。 具体来说,该函数的输入包括: - model:预测模型,可接受任何实现了“fit”和“predict”方法的模型对象。 - X:形状为(n_samples, n_features)的特征矩阵,其中n_samples是样本数,n_features是特征数。 - y:形状为(n_samples,)的目标向量,其中n_samples是样本数。 - window_size:窗口大小,用于指定滚动预测过程中每个窗口的大小。 - step_size:步长,用于指定滚动预测过程中每个窗口之间的跨度。 该函数的输出包括: - y_true:形状为(n_windows,)的目标向量,其中n_windows是滚动预测过程中生成的窗口数。 - y_pred:形状为(n_windows,)的预测结果向量,其中n_windows是滚动预测过程中生成的窗口数。 该函数的实现逻辑是,对于每个窗口,它会使用模型对该窗口中的特征和目标变量进行拟合,并使用最后一个特征来生成一个预测结果。然后,该函数将该预测结果添加到预测结果向量中,并将该窗口的最后一个目标变量添加到目标变量向量中。最后,该函数返回目标变量向量和预测结果向量。 需要注意的是,该函数返回的预测结果向量是基于滚动预测过程生成的,因此它的长度可能会比输入的目标向量短。此外,由于该函数使用了滚动窗口,因此它需要一些额外的输入参数来指定窗口的大小和步长。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值