<jupyter_start><jupyter_text>test<jupyter_code>import pandas as pd 

def gene(start_date, end_date, gid, pid):
    dr = pd.date_range(start_date, end_date)
    n = len(dr)
    qty = np.arange(n) / 50 #+ np.random.randint(-3, 3, n)
    df = pd.DataFrame({'date': dr, 
                      'goodsqty': qty,
                      'goodsid': gid,
                      'placepointid': pid})
    return df

# 根据goodsid， placepointid 获取对应的时间序列数据
def get_subdata(data, gid, pid):
    return data[(data['goodsid'] == gid) & (data['placepointid'] == pid)]
    
# 生成虚拟数据, 四个组合 (0, 0),(0, 1),(1, 0),(1, 1)
data = pd.concat((gene('1997-1-1', '1999-4-5', 0, 0), 
           gene('2020-1-1', '2022-3-3', 0, 1),
           gene('2015-12-12', '2018-9-9', 1, 0),
           gene('2025-6-9', '2027-9-9', 1, 1)), axis=0)

# 选出 goodsid = 0, placepointid = 0 的数据
subdata = get_subdata(data, 1, 0)

# 对日期排序
subdata = subdata.sort_values(by=['date'])

# 对subdata fit 一个ts模型 ......
# subdata
tsdata = pd.DataFrame({'sale': np.array([1,3,5,7,9,7,5,3] * 1000)})
# for i in range(1, 9):
#     tsdata['pre_{}'.format(i)] = tsdata['sale'].shift(i)
# tsdata.dropna(inplace=True)
iid = 0
pid = 0
data = tsdata.to_numpy()
scaler = MinMaxScaler() 
data = scaler.fit_transform(data)
# batch_size, num_steps = 64, 16
batch_size, num_steps = 64, 10
num_inputs = 1 + 3 + 3
num_hiddens = 16
net = RNNModel(nn.LSTM(num_inputs, num_hiddens), vocab_size=1, num_item=2, num_place=2, item_embed_size=3, place_embed_size=3)
for name, param in net.named_parameters():
    print(name, param.size())
# 训练
train_iter = SeqDataLoader(data, batch_size=batch_size, num_steps=num_steps, vocab_size=1)
num_epochs, lr = 500, 0.01
device = 'cpu'
train(net, train_iter, iid, pid, lr, num_epochs, device)
prefix = torch.tensor(data[-15:]).reshape(1, -1, 1)
# 向后预测
predict = predict_rnn(inputs=prefix, iid=iid, pid=pid, num_preds=15, net=net, device='cpu')
scaler.inverse_transform(np.array(predict).reshape(-1, 1))<jupyter_output><empty_output><jupyter_text>linear<jupyter_code>tsdata = pd.DataFrame({'sale': range(8000)})
# for i in range(1, 9):
#     tsdata['pre_{}'.format(i)] = tsdata['sale'].shift(i)
# tsdata.dropna(inplace=True)
iid = 0
pid = 0
data = tsdata.to_numpy()
scaler = MinMaxScaler() 
data = scaler.fit_transform(data)

# batch_size, num_steps = 64, 16
batch_size, num_steps = 64, 10
num_inputs = 1 + 3 + 3
num_hiddens = 16
net = RNNModel(nn.LSTM(num_inputs, num_hiddens), vocab_size=1, num_item=2, num_place=2, item_embed_size=3, place_embed_size=3)

# 训练
train_iter = SeqDataLoader(data, batch_size=batch_size, num_steps=num_steps, vocab_size=1)
num_epochs, lr = 500, 0.01
device = 'cpu'
train(net, train_iter, iid, pid, lr, num_epochs, device)
prefix = torch.tensor(data[-15:]).reshape(1, -1, 1)
# 向后预测
predict = predict_rnn(inputs=prefix, iid=iid, pid=pid, num_preds=15, net=net, device='cpu')
scaler.inverse_transform(np.array(predict).reshape(-1, 1)).reshape(-1)
prefix = torch.tensor(data[10:30]).reshape(1, -1, 1)
# 向后预测
predict = predict_rnn(inputs=prefix, iid=iid, pid=pid, num_preds=15, net=net, device='cpu')
scaler.inverse_transform(np.array(predict).reshape(-1, 1)).reshape(-1)<jupyter_output><empty_output><jupyter_text>id<jupyter_code>def get_tsdata(data, gid, pid):
    return data[(data['goodsid'] == gid) & (data['placepointid'] == pid)]['goodsqty'].to_numpy()
scaler1 = MinMaxScaler() 
scaler2 = MinMaxScaler() 
scaler3 = MinMaxScaler() 

ts1 = np.array([1,3,5,7,9,7,5,3] * 1000)
ts2 = np.array([0,10,20,30,40,50,60,70,80,70,60,50,40,30,20,10] * 500)
ts3 = np.arange(8000)

df1 = pd.DataFrame({'goodsqty': scaler1.fit_transform(ts1.reshape(-1, 1)).reshape(-1),
                  'goodsid': 0,
                  'placepointid': 0})
df2 = pd.DataFrame({'goodsqty': scaler2.fit_transform(ts2.reshape(-1, 1)).reshape(-1),
                  'goodsid': 1,
                  'placepointid': 0})
df3 = pd.DataFrame({'goodsqty': scaler3.fit_transform(ts3.reshape(-1, 1)).reshape(-1),
                  'goodsid': 2,
                  'placepointid': 0})

data = pd.concat((df1, df2, df3), axis=0)
good_place = data[['goodsid', 'placepointid']].drop_duplicates().to_numpy()
# batch_size, num_steps = 64, 16
batch_size, num_steps = 64, 14
num_inputs = 1 + 3 + 3
num_hiddens = 16
net = RNNModel(nn.LSTM(num_inputs, num_hiddens), vocab_size=1, num_item=3, num_place=1, item_embed_size=3, place_embed_size=3)
# 训练
train_iter1 = SeqDataLoader(get_tsdata(data, 0, 0).reshape(-1, 1), batch_size=batch_size, num_steps=num_steps, vocab_size=1)
train_iter2 = SeqDataLoader(get_tsdata(data, 1, 0).reshape(-1, 1), batch_size=batch_size, num_steps=num_steps, vocab_size=1)
train_iter3 = SeqDataLoader(get_tsdata(data, 2, 0).reshape(-1, 1), batch_size=batch_size, num_steps=num_steps, vocab_size=1)
train_iter_lst = [(train_iter1, 0, 0), (train_iter2, 1, 0), (train_iter3, 2, 0)]
num_epochs, lr = 800, 0.01
device = 'cpu'
for _ in range(num_epochs):
    for train_iter, iid, pid in train_iter_lst:
        train(net, train_iter, iid, pid, lr, 1, device)<jupyter_output>epoch: 1, mse: 0.10532869398593903, time: 0.06879854202270508
epoch: 1, mse: 0.07545756548643112, time: 0.04687905311584473
epoch: 1, mse: 0.03596240282058716, time: 0.0598757266998291
epoch: 1, mse: 0.09377843886613846, time: 0.05219078063964844
epoch: 1, mse: 0.047823794186115265, time: 0.05826163291931152
epoch: 1, mse: 0.006077420897781849, time: 0.05227398872375488
epoch: 1, mse: 0.06382707506418228, time: 0.06023836135864258
epoch: 1, mse: 0.03050898388028145, time: 0.05019664764404297
epoch: 1, mse: 0.00493152579292655, time: 0.05825066566467285
epoch: 1, mse: 0.04159929230809212, time: 0.050238847732543945
epoch: 1, mse: 0.01812414452433586, time: 0.058226823806762695
epoch: 1, mse: 0.005725582595914602, time: 0.05019831657409668
epoch: 1, mse: 0.025556471198797226, time: 0.0582432746887207
epoch: 1, mse: 0.01333511434495449, time: 0.05228900909423828
epoch: 1, mse: 0.0030036508105695248, time: 0.06017565727233887
epoch: 1, mse: 0.02947215549647808, time: 0.06025338172912598
ep[...]<jupyter_text>two ts  0，0<jupyter_code>prefix = torch.tensor(df1['goodsqty'].values[-15:]).reshape(1, -1, 1)
# 向后预测
predict = predict_rnn(inputs=prefix, iid=0, pid=0, num_preds=15, net=net, device='cpu')
scaler1.inverse_transform(np.array(predict).reshape(-1, 1)).reshape(-1)<jupyter_output><empty_output><jupyter_text>1， 0<jupyter_code>prefix = torch.tensor(df2['goodsqty'].values[-15:]).reshape(1, -1, 1)
# 向后预测
predict = predict_rnn(inputs=prefix, iid=1, pid=0, num_preds=15, net=net, device='cpu')
scaler2.inverse_transform(np.array(predict).reshape(-1, 1)).reshape(-1)
prefix = torch.tensor(df2['goodsqty'].values[-15:]).reshape(1, -1, 1)
# 向后预测
predict = predict_rnn(inputs=prefix, iid=0, pid=0, num_preds=15, net=net, device='cpu')
scaler2.inverse_transform(np.array(predict).reshape(-1, 1)).reshape(-1)<jupyter_output><empty_output><jupyter_text>three ts  0， 0<jupyter_code>prefix = torch.tensor(df1['goodsqty'].values[-15:]).reshape(1, -1, 1)
# 向后预测
predict = predict_rnn(inputs=prefix, iid=0, pid=0, num_preds=15, net=net, device='cpu')
scaler1.inverse_transform(np.array(predict).reshape(-1, 1)).reshape(-1)<jupyter_output><empty_output><jupyter_text>1，0<jupyter_code>prefix = torch.tensor(df2['goodsqty'].values[-15:]).reshape(1, -1, 1)
# 向后预测
predict = predict_rnn(inputs=prefix, iid=1, pid=0, num_preds=15, net=net, device='cpu')
scaler2.inverse_transform(np.array(predict).reshape(-1, 1)).reshape(-1)<jupyter_output><empty_output><jupyter_text>2，0<jupyter_code>prefix = torch.tensor(df3['goodsqty'].values[-25:]).reshape(1, -1, 1)
# 向后预测
predict = predict_rnn(inputs=prefix, iid=2, pid=0, num_preds=15, net=net, device='cpu')
scaler3.inverse_transform(np.array(predict).reshape(-1, 1)).reshape(-1)
prefix = torch.tensor(df3['goodsqty'].values[10:30]).reshape(1, -1, 1)
# 向后预测
predict = predict_rnn(inputs=prefix, iid=2, pid=0, num_preds=15, net=net, device='cpu')
scaler3.inverse_transform(np.array(predict).reshape(-1, 1)).reshape(-1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17513419048807888, 'fraction_numerical': 0.1278242416677069, 'mean_word_length': 5.5726004922067265, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '653550', 'license': 'Apache-2.0', 'n_tokens_mistral': 4043, 'n_tokens_neox': 3491, 'n_words': 586, 'path': 'chapter/8 RNN/rnn_for_ts.ipynb', 'repo_name': 'sxy22/d2l', 'size': '8011'}