使用lstm根据前几天的数据预测下一个位置

时间:2019-05-26 11:55:54

标签: python deep-learning time-series lstm prediction

基于10天的数据,我需要预测用户的11天位置。我的输入示例在this link

数据集包含: 200,000个用户,具有1550个随机位置点和每个用户10天的历史记录

from datetime import datetime
from random import seed

import pandas as pd

seed(150)

df = pd.read_csv('input.csv', delimiter=';')
df = df.iloc[:-1,:]
df = df.drop(columns='tslot')

def normalize_cols(df, cols):
for col in df.columns:
print(col, df[col].max(), df[col].min())
df[col] = (df[col] - df[col].min())/(df[col].max() - df[col].min())
return df

df = normalize_cols(df, ['slot', 'location', 'user'])
df.to_csv('cleaned_data.csv', index=False)
from keras.models import Sequential, load_model
from keras.layers import Dense, LSTM, Dropout
from matplotlib import pyplot
import numpy as np
import pandas as pd
df = pd.read_csv('cleaned_data.csv')
train_x = df[['slot', 'user']].copy()
train_y = df[['tower']].copy().values
train_x = train_x.values.reshape((train_x.shape[0], 1, train_x.shape[1]))
model = Sequential()
model.add(LSTM(50, input_shape=(train_x.shape[1], train_x.shape[2])))
model.add(Dropout(0.5))
model.add(Dense(40))
model.add(Dropout(0.3))
model.add(Dense(20))
model.add(Dropout(0.2))
model.add(Dense(1))
model.compile(loss='mae', optimizer='adam')
history = model.fit(train_x, train_y, epochs=100, batch_size=25,  verbose=2, shuffle=True)
model.save('5g_tower.h5')
del model
model = load_model('5g_tower.h5')
## Prediction
def get_tower_number(model, slot, user): 
## normalize input slot, tower, user
slot = ((slot - 0)*1.0) / ((95.0 - 0.0)*1.0)
user = ((user - 1)*1.0) / ((2.0 - 1.0)*1.0)

## get a slot for next 15 mins
res = model.predict(np.array([[[slot, user]]]))

## de-normalize that tower
return int(res[0][0] * ((1354.0 - 9.0)*1.0) + 9.0)

print(get_tower_number(model, 4, 1))

但是无法预测第11天用户的位置。与所需的输出相比,我得到的输出是错误的。 输出示例在this link中给出。如何在该系统中实现单个模型而不是全局模型? 示例输出应为第11天:

     User   location   slot
      1        145       0




      1         94       95
      2         562       0

      2         583       95

实际数据集总共包含350,000个用户

1 个答案:

答案 0 :(得分:1)

仅查看模型实现,您就缺少每个Dense块的激活功能,因此keras正在应用线性激活。 您可以使用以下方法进行修改:

model.add(Activation('relu'))