Predicted transactions

本文探讨了处理连续输入数据的两种模型:通过设置定时器扩展执行时间以响应每个新任务,或在沉默期收集输入并在定时器结束时执行任务,以优化资源使用和效率。

When you put something into a dynamic array, it won't grow by just 1 when reaching the limit.  This is important because changing size is an expensive operation.

 

For example if I have a job to execute, and the input data just keep coming and coming every now and then.

 

There could be 2 models for this: 

 

Solution #1: Keep a timeout for the job, extend the execution time by fixed time(such as 1s)  every time we received a new job. When timeout is reached, execute the job. 

When a new input come, and there's no existing job available, we simple create a new job

 

Solution #2: Keep a timeout for the tob, before execution, collect new inputs, before execution in the last second, mark it as silent period. If we have new input coming in the silent period, do not execute the job, request another spin of time as the timeout is defined.

 

# 从data数据框中筛选出'宝贝ID'列值为'1600616216994'的行,然后按照'采集日期'列进行升序排序,最后重置索引并丢弃原来的索引,使得索引从0开始重新编号。 df = data[data['宝贝ID'] == '1600616216994'].sort_values('采集日期').reset_index(drop=True) # 取2023-10月的最后一个日期和最后一行(即最新日期),并忽略合并时的索引 last_october = df[df['采集日期'].astype(str).str.startswith('2023-10')].iloc[-1:] new_df = pd.concat([last_october, df.tail(1)], ignore_index=True) # 将'近一年销量(月)'列从字符串转化为Python对象列表 new_df['近一年销量(月)'] = new_df['近一年销量(月)'].apply(ast.literal_eval) # 将 new_df['近一年销量(月)'][0] 和 new_df['近一年销量(月)'][1]转换为两个DataFrame,并按行合并成一个新的DataFrame,其中新DataFrame的列名为'month'和'transactions'。 month_sale = pd.concat([pd.DataFrame(new_df['近一年销量(月)'][0], columns=['month', 'transactions']), pd.DataFrame(new_df['近一年销量(月)'][1], columns=['month', 'transactions'])]) # 根据month列和transactions列去重,并去掉最后一行2023-11月数据 month_sale = month_sale.drop_duplicates(subset=['month', 'transactions'])[:-1] month_sale # 将'month'列转换为datetime类型 month_sale['month'] = pd.to_datetime(month_sale['month'], format='%Y-%m') # 将交易量数据归一化(总和为1) month_sale['transactions'] = month_sale['transactions'] / month_sale['transactions'].sum() # 计算3个月的移动平均值(使用归一化后的数据) month_sale['moving_average'] = month_sale['transactions'].rolling(window=3, min_periods=1).mean() # 预测2023-11月的销量(基于前三个月移动平均值,并还原原始量级) predicted_sales = month_sale['moving_average'].iloc[-3:].mean() * month_sale['transactions'].sum() print(f"预测的2023-11月销量为:{predicted_sales:.0f}")修改代码
05-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值