Keras: read csv timeseries

From OnnoWiki
Jump to: navigation, search

Sumber: https://stackoverflow.com/questions/47408427/tensorflow-timeseries-data-import-with-datetime



According to the source code, a RandomWindowInputFn accepts either a CSVReader or a NumpyReader. So you could use pandas to read the CSV, do the date parsing and then feed the transformed dates into a NumpyReader

My time-series data looks like this

timestamp   value
0   2014-02-14 14:30:00 0.132
1   2014-02-14 14:35:00 0.134
2   2014-02-14 14:40:00 0.134
3   2014-02-14 14:45:00 0.134
4   2014-02-14 14:50:00 0.134

First, i parsed the timestamp column into a int col using pandas

from datetime import datetime as dt import pandas as pd

def date_parser(date_str):
    return dt.strptime(date_str, "%Y-%m-%d %H:%M:%S").strftime("%s")
data = pd.read_csv("my_data.csv"
                   , header=0
                   , parse_dates=['timestamp']
                   , date_parser=date_parser)

data['timestamp'] = data['timestamp'].apply(lambda x: int(x))

Then we can pass on these arrays to the NumpyReader

np_reader = tf.contrib.timeseries.NumpyReader(data={tf.contrib.timeseries.TrainEvalFeatures.TIMES: data['timestamp'].values, tf.contrib.timeseries.TrainEvalFeatures.VALUES : data['value'].values})

And finally pass the np_reader to the RandomWindowInputFn

train_input_fn = tf.contrib.timeseries.RandomWindowInputFn(
      np_reader, batch_size=32, window_size=16)

Hope this helps somebody!


Pranala Menarik