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

本文详细介绍了Pandas库中DataFrame的rolling方法,包括其参数解释、不同应用场景下的使用技巧及示例代码。重点讲解了固定窗口大小、中心窗口设置、权重分配等功能,并通过实例展示了如何利用不同类型窗口进行数据统计。

@创建于: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 参数介绍
序号 参数 英文 中文
1 window: int Size of the moving window. This is the number of observations used for calculating the statistic. Each window will be a fixed size. 移动窗口的大小。这是用于计算统计数据的观测数。每个窗口的大小都是固定的。
2 window: offset If 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索引有效。
3 window: BaseIndexer subclass If 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。
4 min_periods int, default None Minimum 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将默认为窗口的大小。
5 center bool, default False Set the labels at the center of the window. 将标签设置在窗口的中心。
6 win_type str, default None Provide 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
7 on str, optional For 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而言,指定要计算滚动窗口的列,值为列名。
8 axis int or str, default 0 方向(轴),一般都是0
9 closed str, default None Make 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
以下是对代码 `original_index = np.arange(len(features)); features_df = pd.DataFrame(X_scaled, index = original_index); target_df = pd.DataFrame(target, index = original_index, columns = ['target'])` 各部分的详细解释: #### 1. `original_index = np.arange(len(features))` - `np.arange` 是 NumPy 库中的函数,用于创建一个等差数列。`len(features)` 返回 `features` 数组的长度,也就是样本的数量。 - 这行代码创建了一个从 0 到 `len(features) - 1` 的整数数组,作为后续 DataFrame 的索引。这个索引数组 `original_index` 可以确保每个样本在转换为 DataFrame 后仍能保持其原始的顺序。 #### 2. `features_df = pd.DataFrame(X_scaled, index = original_index)` - `pd.DataFrame` 是 Pandas 库中的函数,用于创建一个 DataFrame 对象。`X_scaled` 是经过标准化处理后的特征数组。 - 通过指定 `index = original_index`,将之前创建的索引数组应用到新创建的 DataFrame 中。这样,`features_df` 中的每一行都有一个对应的索引,方便后续的数据处理和分析。 #### 3. `target_df = pd.DataFrame(target, index = original_index, columns = ['target'])` - 同样使用 `pd.DataFrame` 创建一个新的 DataFrame 对象,`target` 是目标变量数组。 - `index = original_index` 确保 `target_df` 的索引与 `features_df` 一致,便于后续将特征和目标变量进行匹配。 - `columns = ['target']` 指定了 DataFrame 的列名,这里将目标变量的列名设置为 `'target'`。 ### 代码的用途 这段代码的主要用途是将经过标准化处理的特征数组 `X_scaled` 和目标变量数组 `target` 转换为 Pandas 的 DataFrame 对象,并为它们设置相同的索引。这样做的好处是方便后续的数据处理、分析和可视化,因为 Pandas 的 DataFrame 提供了丰富的功能和方法,例如数据筛选、统计计算、绘图等。 ### 示例代码 ```python import numpy as np import pandas as pd from sklearn.preprocessing import StandardScaler from sklearn.datasets import make_classification # 生成模拟数据 features, target = make_classification(n_samples=100, n_features=2, random_state=42) # 初始化标准化缩放器 scaler = StandardScaler() # 使用 fit_transform 方法同时拟合和转换数据 X_scaled = scaler.fit_transform(features) # 创建索引数组 original_index = np.arange(len(features)) # 创建特征 DataFrame features_df = pd.DataFrame(X_scaled, index=original_index) # 创建目标变量 DataFrame target_df = pd.DataFrame(target, index=original_index, columns=['target']) print("特征 DataFrame:") print(features_df.head()) print("目标变量 DataFrame:") print(target_df.head()) ```
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值