for date in date_history.date: week_day = date.weekday() # 获取星期几,0 表示星期一,6 表示星期日 sum_week.append(week_day) week_list = [] for i, num in enumerate(sum_week): if num == 6: week_list.append(i) forecast_time_obj_week = forecast_time_obj.weekday() # 判断预测的值是否周末 if forecast_time_obj_week == 6: # 判断是否是周日,0代表周一,6代表周日 pre_true_24hour = dataset_history[(week_list[-1] - 95): (week_list[-1] + 1)] else: pre_true_24hour = pre_true 优化代码
时间: 2024-01-02 12:02:52 浏览: 71
可以考虑使用列表解析式来代替循环生成 sum_week 列表,代码如下:
```
sum_week = [date.weekday() for date in date_history.date]
```
同时,可以使用列表的 index 方法来查找 sum_week 列表中所有 6 的位置,代码如下:
```
week_list = [i for i, num in enumerate(sum_week) if num == 6]
```
这样可以避免使用循环生成 week_list 列表。另外,可以将判断是否是周日的代码提取出来,避免在判断是否是周日时重复计算,代码如下:
```
if forecast_time_obj_week == 6: # 判断是否是周日,0代表周一,6代表周日
pre_true_24hour = dataset_history[(week_list[-1] - 95): (week_list[-1] + 1)]
else:
pre_true_24hour = pre_true
```
相关问题
CREATE PROCEDURE HA_ST01,PRO_TEST01_LNJ(IN ID INTEGER,IN DAY_STR NVARCHAR(8),OUT MESSAGE NVARCHAR(50)) LANGUAGE sqlscript as begin DECLARE DAY_DATE DATE; DECLARE COUNT_ID INTEGER; RS_1=SELECT * FROM "HA_ST01"."DATE_INFO_LNJ" WHERE ID =:ID; SELECT COUNT(1) INTO COUNT_ID FROM :RS_1; SELECT TO_DATE(:DAY_STR)INTO DAY_DATE FROM DUMMY; IF :COUNT_ID >=1 THEN :MESSAGE SELECT'错误:主键重复' INTO MESSAGE FROM DUMMY; ELSEIF :COUNT_ID<1 insert into "HA_ST01"."DATE_INFO_LNJ"(ID,DAY,WEEK_NUM)VALUES(:ID,:DAY_DATE,WEEKDAY(:DAY_DATE)); SELECT'插入成功' INTO MESSAGE FROM DUMMY; END IF; end;有啥错误
在第3行,RS_1应该使用DECLARE关键字声明为变量,应该改为:DECLARE RS_1 CURSOR FOR SELECT * FROM "HA_ST01"."DATE_INFO_LNJ" WHERE ID =:ID;
在第4行,COUNT_ID应该先声明为变量,应该改为:DECLARE COUNT_ID INTEGER;
在第7行,应该使用SELECT COUNT(*) INTO COUNT_ID FROM (SELECT * FROM "HA_ST01"."DATE_INFO_LNJ" WHERE ID =:ID);
在第8行,应该使用SELECT TO_DATE(:DAY_STR,'YYYY-MM-DD') INTO DAY_DATE FROM DUMMY;
在第10行,应该将:MESSAGE SELECT'错误:主键重复' INTO MESSAGE FROM DUMMY; 改为 SET MESSAGE = '错误:主键重复';
在第13行,应该将:MESSAGE SELECT'插入成功' INTO MESSAGE FROM DUMMY; 改为 SET MESSAGE = '插入成功';
正确的存储过程如下:
CREATE PROCEDURE HA_ST01.PRO_TEST01_LNJ (IN ID INTEGER, IN DAY_STR NVARCHAR(8), OUT MESSAGE NVARCHAR(50))
LANGUAGE sqlscript AS
BEGIN
DECLARE RS_1 CURSOR FOR SELECT * FROM "HA_ST01"."DATE_INFO_LNJ" WHERE ID =: ID;
DECLARE COUNT_ID INTEGER;
SELECT COUNT(*) INTO COUNT_ID FROM (SELECT * FROM "HA_ST01"."DATE_INFO_LNJ" WHERE ID =: ID);
DECLARE DAY_DATE DATE;
SELECT TO_DATE(:DAY_STR, 'YYYY-MM-DD') INTO DAY_DATE FROM DUMMY;
IF COUNT_ID >= 1 THEN
SET MESSAGE = '错误:主键重复';
ELSEIF COUNT_ID < 1 THEN
INSERT INTO "HA_ST01"."DATE_INFO_LNJ" (ID, DAY, WEEK_NUM) VALUES (: ID, : DAY_DATE, WEEKDAY(: DAY_DATE));
SET MESSAGE = '插入成功';
END IF;
END;
请你把这段代码用def的形式改写data = torch.randn(16992, 307, 12, 2) week_feature = torch.zeros((data.shape[0], 1)) # 遍历所有时间点,获取对应的星期并将星期转化为对应的数字 for i in range(data.shape[0]): date_str = '2016-01-01 00:00:00' # 假设数据集中的时间从2016年1月1日开始 date = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S') date += datetime.timedelta(minutes=5*i) # 每个时间点间隔5分钟 week = date.weekday() # 获取星期,0表示星期一,1表示星期二,以此类推 week_feature[i] = week + 1 # 将星期转化为对应的数字并将其存入星期特征张量中 # 将星期特征添加到原有的数据集中 week_feature=week_feature.unsqueeze(1).repeat(1, 307, 12, 1) week_feature=week_feature.reshape(16992,307,12,1) data = torch.cat((data, week_feature), dim=-1) # week_feature = week_feature.repeat(1, 307, 12, 1) # 将星期特征张量与数据集在最后一个维度上拼接 # data = torch.cat((data, week_feature), dim=-1) # 输出结果 print(data) print(data.shape) pytorch版本
可以这样改写:
```python
import torch
import datetime
def add_week_feature(data):
week_feature = torch.zeros((data.shape[0], 1))
for i in range(data.shape[0]):
date_str = '2016-01-01 00:00:00'
date = datetime.datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
date += datetime.timedelta(minutes=5*i)
week = date.weekday()
week_feature[i] = week + 1
week_feature = week_feature.unsqueeze(1).repeat(1, 307, 12, 1)
week_feature = week_feature.reshape(16992,307,12,1)
data = torch.cat((data, week_feature), dim=-1)
return data
data = torch.randn(16992, 307, 12, 2)
data = add_week_feature(data)
print(data)
print(data.shape)
```
这样就可以通过调用 `add_week_feature(data)` 函数来实现原有代码的功能。
阅读全文