我有一个有效的代码.我想知道如何使用 np. lib. stride_www.example.com_strided或避免循环.

import yfinance as yf
import pandas as pd
import numpy as np

# Fetch Apple stock data
apple_data = yf.download('AAPL', start='2024-01-01', end='2024-03-31')

# Extract volume data
apple_volume = apple_data['Volume']

# Resample to ensure every date is included
apple_volume = apple_volume.resample('D').ffill()

# Function to calculate rolling sum with reset using NumPy
def rolling_sum_with_reset(series, window_size):
    rolling_sums = np.zeros(len(series))
    current_sum = 0
    for i, value in enumerate(series):
        if i % window_size == 0:
            current_sum = 0
        current_sum += value
        rolling_sums[i] = current_sum
    return rolling_sums


rolling_3_day_volume = rolling_sum_with_reset(apple_volume, 3)

推荐答案

For a sample array of integers:

In [225]: arr = np.arange(90); x=rolling_sum_with_reset(arr,3)
In [226]: x
Out[226]: 
array([  0.,   1.,   3.,   3.,   7.,  12.,   6.,  13.,  21.,   9.,  19.,
        30.,  12.,  25.,  39.,  15.,  31.,  48.,  18.,  37.,  57.,  21.,
        43.,  66.,  24.,  49.,  75.,  27.,  55.,  84.,  30.,  61.,  93.,
        33.,  67., 102.,  36.,  73., 111.,  39.,  79., 120.,  42.,  85.,
       129.,  45.,  91., 138.,  48.,  97., 147.,  51., 103., 156.,  54.,
       109., 165.,  57., 115., 174.,  60., 121., 183.,  63., 127., 192.,
        66., 133., 201.,  69., 139., 210.,  72., 145., 219.,  75., 151.,
       228.,  78., 157., 237.,  81., 163., 246.,  84., 169., 255.,  87.,
       175., 264.])

如果arr的大小是windows的倍数,则可以将其重新整形为2d数组,并将cumsum应用于每行:

In [227]: y = np.cumsum(arr.reshape(-1,3), axis=1).ravel()

与您的结果相符:

In [228]: np.allclose(x,y)
Out[228]: True

我认为cumsum可以与某种'重置'步长数组一起使用,但这种整形方法更容易.

as_strided可以用来创建窗口,但由于窗口中不需要重叠,结果与此reshape相同:

In [230]: np.lib.stride_tricks.sliding_window_view(arr,3)[::3]
Out[230]: 
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11],
       [12, 13, 14],
       [15, 16, 17],
        ...

尺寸10的windows

In [237]: rolling_sum_with_reset(arr,10)
Out[237]: 
array([  0.,   1.,   3.,   6.,  10.,  15.,  21.,  28.,  36.,  45.,  10.,
        21.,  33.,  46.,  60.,  75.,  91., 108., 126., 145.,  20.,  41.,
        63.,  86., 110., 135., 161., 188., 216., 245.,  30.,  61.,  93.,
       126., 160., 195., 231., 268., 306., 345.,  40.,  81., 123., 166.,
       210., 255., 301., 348., 396., 445.,  50., 101., 153., 206., 260.,
       315., 371., 428., 486., 545.,  60., 121., 183., 246., 310., 375.,
       441., 508., 576., 645.,  70., 141., 213., 286., 360., 435., 511.,
       588., 666., 745.,  80., 161., 243., 326., 410., 495., 581., 668.,
       756., 845.])

In [238]: y = np.cumsum(arr.reshape(-1,10), axis=1).ravel()

In [239]: y
Out[239]: 
array([  0,   1,   3,   6,  10,  15,  21,  28,  36,  45,  10,  21,  33,
        46,  60,  75,  91, 108, 126, 145,  20,  41,  63,  86, 110, 135,
       161, 188, 216, 245,  30,  61,  93, 126, 160, 195, 231, 268, 306,
       345,  40,  81, 123, 166, 210, 255, 301, 348, 396, 445,  50, 101,
       153, 206, 260, 315, 371, 428, 486, 545,  60, 121, 183, 246, 310,
       375, 441, 508, 576, 645,  70, 141, 213, 286, 360, 435, 511, 588,
       666, 745,  80, 161, 243, 326, 410, 495, 581, 668, 756, 845])

Python相关问答推荐

Chatgpt API不断返回错误:404未能从API获取响应

提取两行之间的标题的常规表达

Python 3.12中的通用[T]类方法隐式类型检索

使用numpy提取数据块

如何在Windows上用Python提取名称中带有逗号的文件?

如何在python xsModel库中定义一个可选[December]字段,以产生受约束的SON模式

Telethon加入私有频道

通过pandas向每个非空单元格添加子字符串

如何在Polars中从列表中的所有 struct 中 Select 字段?

cv2.matchTemplate函数匹配失败

合并与拼接并举

在Python中控制列表中的数据步长

如何在Airflow执行日期中保留日期并将时间转换为00:00

Python如何导入类的实例

Django在一个不是ForeignKey的字段上加入'

如果不使用. to_list()[0],我如何从一个pandas DataFrame中获取一个值?

Polars时间戳同步延迟计算

正在try 让Python读取特定的CSV文件

将标签与山脊线图对齐

Pandas ,快速从词典栏中提取信息到新栏