<jupyter_start><jupyter_text>TS_DEEP_LEARNING_TENSORFLOW **Time Series Deep Learning with TensorFlow** Based on https://towardsdatascience.com/time-series-forecasting-with-lstms-using-tensorflow-2-and-keras-in-python-6ceee9c6c651<jupyter_code>%load_ext autoreload
%autoreload 2
%matplotlib inline
import os
import sys
sys.path.append('..')
from copy import deepcopy
import datetime
import IPython
import IPython.display
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import tensorflow as tf
import statslib as stb
mpl.rcParams['figure.figsize'] = (8*1.6, 8)
mpl.rcParams['axes.grid'] = False
figsize = (8*1.6, 8)
import keras<jupyter_output><empty_output><jupyter_text>Data<jupyter_code>time = np.arange(0, 100, 0.1)
sin = np.sin(time) + np.random.normal(scale=0.5, size=len(time))
plt.plot(sin)
df = pd.DataFrame(dict(sine=sin), index=time, columns=['sine'])

train_size = int(len(df) * 0.8)
test_size = len(df) - train_size
train, test = df.iloc[0:train_size], df.iloc[train_size:len(df)]
print(len(train), len(test))

def create_dataset(X, y, time_steps=1):
    Xs, ys = [], []
    for i in range(len(X) - time_steps):
        v = X.iloc[i:(i + time_steps)].values
        Xs.append(v)
        ys.append(y.iloc[i + time_steps])
    return np.array(Xs), np.array(ys)
time_steps = 10

# reshape to [samples, time_steps, n_features]

X_train, y_train = create_dataset(train, train.sine, time_steps)
X_test, y_test = create_dataset(test, test.sine, time_steps)

print(X_train.shape, y_train.shape)
model = keras.Sequential()
model.add(keras.layers.LSTM(
  units=128,
  input_shape=(X_train.shape[1], X_train.shape[2])
))
model.add(keras.layers.Dense(units=1))
model.compile(
  loss='mean_squared_error',
  optimizer=keras.optimizers.Adam(0.001)
)
y_pred = model.predict(X_test)
fig, ax = plt.subplots()
pd.DataFrame(y_pred).plot(ax=ax)
pd.DataFrame(y_test).plot(ax=ax)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1448170731707317, 'fraction_numerical': 0.02032520325203252, 'mean_word_length': 5.652027027027027, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '420955', 'license': 'Apache-2.0', 'n_tokens_mistral': 814, 'n_tokens_neox': 783, 'n_words': 148, 'path': 'sandbox/TS_NN_TensorFlow-TutorialShort.ipynb', 'repo_name': 'ashubertt/statslib', 'size': '1968'}