Search code examples
pythontensorflowkeraslstmprediction

How to apply LSTM to predict parking Availability


I'm new with recurrent neural network and I have to apply LSTM (KERAS) to predict parking Availability from my dataset. I have a dataset with two features, timestamp (Y-M-D H-M-S) and the parking availability (number of free parking spaces). Each 5 minutes, for each day starting from 00:03 AM to 23:58 PM (188 samples for each day) was sampled the parking Availability for a duration of 25 weeks. I need some help to understand how to apply LSTM (what timestep to select ect).


Solution

  • It seems that you want to understand that how could you use your dataset and apply LSTMs over it to get some meaningful out of your data.

    Now here you can reframe your data set to create more features from your present data set for eg.

    Features That could be derived out of Data

    1. Take Out day of the month (which day is it 1-31)
    2. Week of the month (which week of month it is 1-4)
    3. Day of the week (Monday - Saturday)
    4. what is the time ( you can have any of the value out of 188)

    Features that could be added from opensource data

    1. What is the wheather of the day
    2. Is there any holiday nearby(days remaining for next holiday/function etc.)

    Now let's Assume for each row you have K features in your data and you have a target that you have to predict which is what is the availability of parking. P(#parking_space|X)

    Now just just keep your timesteps as a variable while creating your model and reshape your data from X.shape-->(Examples, Features) to the format X.shape-->(examples,Timesteps,Features). You can use below code and define your own look_back

    Here your architecture will be many to many with Tx=Ty

    def create_dataset_many_to_many(dataset,look_back=70):
        data_timestamp=[]
        for i in range(len(dataset)-look_back):
            data_timestamp.append(dataset[i:i+look_back])
            if i%1000==0:
                print(i)
        return np.array(data_timestamp)
    

    Now you can build model

    import tensorflow as tf
    
    import matplotlib.pyplot as plt
    # Importing pandas
    import pandas as pd
    # Importing keras model
    from tensorflow.keras.models import Model
    # Importing layers like input and dense
    from tensorflow.keras.layers import Dense, Input,LSTM,Dropout
    #importing train test split
    from sklearn.model_selection import train_test_split
    
    class Lstm_model(tf.keras.Model):
        def __init__(self, **kwargs):
            super(Lstm_model, self).__init__()   
            self.Lstm1 = tf.keras.layers.LSTM(32,return_sequences=True)
            self.Lstm2 = tf.keras.layers.LSTM(32,return_sequences=True) 
            self.Regressor = tf.keras.layers.Dense(1, )
    
        def call(self, inputs):
    
            input_A=inputs
            x = self.Lstm1(input_A)
            x = self.Lstm2(x)
            pred = self.Regressor(x) 
            
            return  pred
    
    lstms_ = Lstm_model()
    lstms_.compile(optimizer='adam', loss=tf.keras.losses.MeanSquaredError())
    lstms_.fit(X,Y, epochs=50)
    

    This is just a glimpse of how you can make your model.