Python 滑动窗口-测量每个环形窗口的观察长度
让我们分析一下这个示例代码,其中用于从数据集创建不同的窗口,并在循环中返回它们Python 滑动窗口-测量每个环形窗口的观察长度,python,pandas,for-loop,zip,sliding-window,Python,Pandas,For Loop,Zip,Sliding Window,让我们分析一下这个示例代码,其中用于从数据集创建不同的窗口,并在循环中返回它们 months = [Jan, Feb, Mar, Apr, May] for x, y in zip(months, months[1:]): print(x, y) # Output of each window will be: Jan Feb Feb Mar Mar Apr Apr May 假设现在我想计算每个窗口中使用的月份之间的长度百分比 步骤中的示例: 当返回第一个窗口(1-2月)时,我想
months = [Jan, Feb, Mar, Apr, May]
for x, y in zip(months, months[1:]):
print(x, y)
# Output of each window will be:
Jan Feb
Feb Mar
Mar Apr
Apr May
假设现在我想计算每个窗口中使用的月份之间的长度百分比
步骤中的示例:
months = [Jan, Feb, Mar, Apr, May]
for x, y in zip(months, months[2:]):
print(x, y)
# Output of each window will be:
Jan Feb March
Feb Mar Apr
Mar Apr May
目标是计算整个窗口长度内每个窗口的两个月长度:
- 第一个窗口:一月+二月/一月+二月+三月
- 第二窗口:二月+三月/二月+三月+四月
- 继续到最后一个窗口
months
是一个period\u范围
对象。)
现在,在范围内迭代。每次迭代都有两个月的时间窗口
# print header labels
print('{:10s} {:10s} {:>10s} {:>10s} {:>10s} {:>10s} '.format(
'start', 'end', 'month', 'front (d)', 'total (d)', 'frac'))
for start, end in zip(months, months[1:]):
front_month = start.month
# number of days in first month (e.g., Jan)
front_month_days = start.days_in_month
# number of days in current sliding window (e.g., Jan + Feb)
days_in_curr_window = (end.end_time - start.start_time).days
frac = front_month_days / days_in_curr_window
print('{:10s} {:10s} {:10d} {:10d} {:10d} {:10.3f}'.format(
str(start), str(end), front_month,
front_month_days, days_in_curr_window, frac))
start end month front (d) total (d) frac
2020-01 2020-02 1 31 60 0.517
2020-02 2020-03 2 29 60 0.483
2020-03 2020-04 3 31 61 0.508
2020-04 2020-05 4 30 61 0.492
这里有一个方法。(在我的例子中,months
是一个period\u范围
对象。)
现在,在范围内迭代。每次迭代都有两个月的时间窗口
# print header labels
print('{:10s} {:10s} {:>10s} {:>10s} {:>10s} {:>10s} '.format(
'start', 'end', 'month', 'front (d)', 'total (d)', 'frac'))
for start, end in zip(months, months[1:]):
front_month = start.month
# number of days in first month (e.g., Jan)
front_month_days = start.days_in_month
# number of days in current sliding window (e.g., Jan + Feb)
days_in_curr_window = (end.end_time - start.start_time).days
frac = front_month_days / days_in_curr_window
print('{:10s} {:10s} {:10d} {:10d} {:10d} {:10.3f}'.format(
str(start), str(end), front_month,
front_month_days, days_in_curr_window, frac))
start end month front (d) total (d) frac
2020-01 2020-02 1 31 60 0.517
2020-02 2020-03 2 29 60 0.483
2020-03 2020-04 3 31 61 0.508
2020-04 2020-05 4 30 61 0.492
非常感谢。好的,这工作顺利,非常清楚,做得好!现在,如果我将每个窗口设置为三个月(因此将是一月-三月…二月-四月…三月-五月),我想计算的不是1,而是整个窗口的前两个月(因此,一月+二月/一月+二月+三月),我想应该有一种方法将前两个月设置为前两个月,而不仅仅是开始月份,对吗,我注意到:如果我们只使用“年-月”的日期格式,那么在计算时,日期从何而来?它是否包含在pd.period\U范围内?如果我们不使用天数来计算每个月的长度,而是希望使用实际的数据点(即行号),这是否可能呢?
pd。Period
表示一段时间。有用的属性包括:start\u time
,end\u time
,以及days\u in\u month
。此处的详细信息:请发布“使用实际数据点(行号)”的样本输入和输出数据?不确定目标是什么。谢谢你的回复!我已经编辑了这个问题,希望现在问题清楚了:)谢谢!好的,这工作顺利,非常清楚,做得好!现在,如果我将每个窗口设置为三个月(因此将是一月-三月…二月-四月…三月-五月),我想计算的不是1,而是整个窗口的前两个月(因此,一月+二月/一月+二月+三月),我想应该有一种方法将前两个月设置为前两个月,而不仅仅是开始月份,对吗,我注意到:如果我们只使用“年-月”的日期格式,那么在计算时,日期从何而来?它是否包含在pd.period\U范围内?如果我们不使用天数来计算每个月的长度,而是希望使用实际的数据点(即行号),这是否可能呢?pd。Period
表示一段时间。有用的属性包括:start\u time
,end\u time
,以及days\u in\u month
。此处的详细信息:请发布“使用实际数据点(行号)”的样本输入和输出数据?不确定目标是什么。谢谢你的回复!我已经编辑了这个问题,希望现在问题清楚了:)
# print header labels
print('{:10s} {:10s} {:>10s} {:>10s} {:>10s} {:>10s} '.format(
'start', 'end', 'month', 'front (d)', 'total (d)', 'frac'))
for start, end in zip(months, months[1:]):
front_month = start.month
# number of days in first month (e.g., Jan)
front_month_days = start.days_in_month
# number of days in current sliding window (e.g., Jan + Feb)
days_in_curr_window = (end.end_time - start.start_time).days
frac = front_month_days / days_in_curr_window
print('{:10s} {:10s} {:10d} {:10d} {:10d} {:10.3f}'.format(
str(start), str(end), front_month,
front_month_days, days_in_curr_window, frac))
start end month front (d) total (d) frac
2020-01 2020-02 1 31 60 0.517
2020-02 2020-03 2 29 60 0.483
2020-03 2020-04 3 31 61 0.508
2020-04 2020-05 4 30 61 0.492