{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Time series with Keras","metadata":{"papermill":{"duration":0.032461,"end_time":"2022-02-08T12:25:30.457825","exception":false,"start_time":"2022-02-08T12:25:30.425364","status":"completed"},"tags":[]}},{"cell_type":"markdown","source":"#### Please upvote if you find it useful","metadata":{}},{"cell_type":"code","source":"pip install comet_ml -qqq","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:00:31.767720Z","iopub.execute_input":"2022-10-14T19:00:31.768466Z","iopub.status.idle":"2022-10-14T19:00:50.625504Z","shell.execute_reply.started":"2022-10-14T19:00:31.768358Z","shell.execute_reply":"2022-10-14T19:00:50.624223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import comet_ml\n\nexperiment = comet_ml.Experiment(\n    api_key=\"Vgk7SfXoD1E1pCki5TDyZ7ecU\",\n     project_name=\"time-series\", log_code=True,\n    auto_metric_logging=True,\n    auto_param_logging=True,\n    auto_histogram_weight_logging=True,\n    auto_histogram_gradient_logging=True,\n    auto_histogram_activation_logging=True,\n)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:00:50.627880Z","iopub.execute_input":"2022-10-14T19:00:50.628185Z","iopub.status.idle":"2022-10-14T19:00:55.708850Z","shell.execute_reply.started":"2022-10-14T19:00:50.628150Z","shell.execute_reply":"2022-10-14T19:00:55.708030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport tensorflow as tf\nfrom statsmodels.tsa.seasonal import seasonal_decompose\nfrom tensorflow.keras.preprocessing.sequence import TimeseriesGenerator\nfrom sklearn.preprocessing import MinMaxScaler\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.callbacks import TensorBoard, EarlyStopping","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.048332,"end_time":"2022-02-08T12:25:30.600424","exception":false,"start_time":"2022-02-08T12:25:30.552092","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-14T19:00:55.710296Z","iopub.execute_input":"2022-10-14T19:00:55.710684Z","iopub.status.idle":"2022-10-14T19:01:04.301602Z","shell.execute_reply.started":"2022-10-14T19:00:55.710651Z","shell.execute_reply":"2022-10-14T19:01:04.299608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\",index_col='t_dat',parse_dates=True)","metadata":{"papermill":{"duration":75.89177,"end_time":"2022-02-08T12:26:46.523192","exception":false,"start_time":"2022-02-08T12:25:30.631422","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-14T19:01:04.305171Z","iopub.execute_input":"2022-10-14T19:01:04.306261Z","iopub.status.idle":"2022-10-14T19:02:22.222594Z","shell.execute_reply.started":"2022-10-14T19:01:04.306196Z","shell.execute_reply":"2022-10-14T19:02:22.221527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train.tail()","metadata":{"papermill":{"duration":0.060252,"end_time":"2022-02-08T12:26:46.610188","exception":false,"start_time":"2022-02-08T12:26:46.549936","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-14T19:02:22.224310Z","iopub.execute_input":"2022-10-14T19:02:22.224669Z","iopub.status.idle":"2022-10-14T19:02:22.245880Z","shell.execute_reply.started":"2022-10-14T19:02:22.224617Z","shell.execute_reply":"2022-10-14T19:02:22.244676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions = transactions_train.groupby('t_dat')['price'].sum().reset_index()","metadata":{"papermill":{"duration":3.17255,"end_time":"2022-02-08T12:26:49.812368","exception":false,"start_time":"2022-02-08T12:26:46.639818","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-14T19:02:22.247912Z","iopub.execute_input":"2022-10-14T19:02:22.248305Z","iopub.status.idle":"2022-10-14T19:02:22.881528Z","shell.execute_reply.started":"2022-10-14T19:02:22.248257Z","shell.execute_reply":"2022-10-14T19:02:22.880639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['ds'] = transactions['t_dat']\ntransactions['y'] = transactions['price']\ntransactions = transactions[['ds','y']]","metadata":{"papermill":{"duration":0.045722,"end_time":"2022-02-08T12:26:49.886910","exception":false,"start_time":"2022-02-08T12:26:49.841188","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-14T19:02:22.883145Z","iopub.execute_input":"2022-10-14T19:02:22.883465Z","iopub.status.idle":"2022-10-14T19:02:22.893629Z","shell.execute_reply.started":"2022-10-14T19:02:22.883424Z","shell.execute_reply":"2022-10-14T19:02:22.892309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.tail()","metadata":{"papermill":{"duration":0.052746,"end_time":"2022-02-08T12:26:49.969812","exception":false,"start_time":"2022-02-08T12:26:49.917066","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-14T19:02:22.895599Z","iopub.execute_input":"2022-10-14T19:02:22.895981Z","iopub.status.idle":"2022-10-14T19:02:22.910755Z","shell.execute_reply.started":"2022-10-14T19:02:22.895902Z","shell.execute_reply":"2022-10-14T19:02:22.909762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['ds'] = pd.to_datetime(transactions['ds'])","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:22.914305Z","iopub.execute_input":"2022-10-14T19:02:22.914643Z","iopub.status.idle":"2022-10-14T19:02:22.924648Z","shell.execute_reply.started":"2022-10-14T19:02:22.914605Z","shell.execute_reply":"2022-10-14T19:02:22.923854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.set_index('ds', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:22.927415Z","iopub.execute_input":"2022-10-14T19:02:22.928317Z","iopub.status.idle":"2022-10-14T19:02:22.937263Z","shell.execute_reply.started":"2022-10-14T19:02:22.928278Z","shell.execute_reply":"2022-10-14T19:02:22.936182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = seasonal_decompose(transactions['y'])","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:22.938737Z","iopub.execute_input":"2022-10-14T19:02:22.939079Z","iopub.status.idle":"2022-10-14T19:02:22.957365Z","shell.execute_reply.started":"2022-10-14T19:02:22.939034Z","shell.execute_reply":"2022-10-14T19:02:22.956560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20))\nresults.plot(weights=True);\n","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:22.958731Z","iopub.execute_input":"2022-10-14T19:02:22.959433Z","iopub.status.idle":"2022-10-14T19:02:24.221152Z","shell.execute_reply.started":"2022-10-14T19:02:22.959395Z","shell.execute_reply":"2022-10-14T19:02:24.220132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.observed.plot(figsize=(12,6))","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:24.222903Z","iopub.execute_input":"2022-10-14T19:02:24.223881Z","iopub.status.idle":"2022-10-14T19:02:24.576523Z","shell.execute_reply.started":"2022-10-14T19:02:24.223827Z","shell.execute_reply":"2022-10-14T19:02:24.575597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.trend.plot(figsize=(12,6))","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:24.577788Z","iopub.execute_input":"2022-10-14T19:02:24.578021Z","iopub.status.idle":"2022-10-14T19:02:24.912684Z","shell.execute_reply.started":"2022-10-14T19:02:24.577992Z","shell.execute_reply":"2022-10-14T19:02:24.911791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.seasonal.plot(figsize=(8,6))","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:24.914346Z","iopub.execute_input":"2022-10-14T19:02:24.914841Z","iopub.status.idle":"2022-10-14T19:02:25.284845Z","shell.execute_reply.started":"2022-10-14T19:02:24.914795Z","shell.execute_reply":"2022-10-14T19:02:25.284136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results.resid.plot(figsize=(12,6))","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:25.285765Z","iopub.execute_input":"2022-10-14T19:02:25.285985Z","iopub.status.idle":"2022-10-14T19:02:25.615821Z","shell.execute_reply.started":"2022-10-14T19:02:25.285956Z","shell.execute_reply":"2022-10-14T19:02:25.614838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = seasonal_decompose(transactions.head(100)['y'])\nresults.seasonal.plot(figsize=(8,6))","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:25.617204Z","iopub.execute_input":"2022-10-14T19:02:25.617478Z","iopub.status.idle":"2022-10-14T19:02:25.920773Z","shell.execute_reply.started":"2022-10-14T19:02:25.617444Z","shell.execute_reply":"2022-10-14T19:02:25.919809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.info()","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:25.922043Z","iopub.execute_input":"2022-10-14T19:02:25.922324Z","iopub.status.idle":"2022-10-14T19:02:25.937690Z","shell.execute_reply.started":"2022-10-14T19:02:25.922290Z","shell.execute_reply":"2022-10-14T19:02:25.936301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = transactions[[\"y\"]]\nsplit_time = 600\nx_train = data[:split_time]\nx_valid = data[split_time:]","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:25.939140Z","iopub.execute_input":"2022-10-14T19:02:25.939400Z","iopub.status.idle":"2022-10-14T19:02:25.946675Z","shell.execute_reply.started":"2022-10-14T19:02:25.939364Z","shell.execute_reply":"2022-10-14T19:02:25.945467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = MinMaxScaler()\nscaled_train = scaler.fit_transform(x_train)\nscaled_test = scaler.transform(x_valid)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:25.948194Z","iopub.execute_input":"2022-10-14T19:02:25.948452Z","iopub.status.idle":"2022-10-14T19:02:25.970750Z","shell.execute_reply.started":"2022-10-14T19:02:25.948420Z","shell.execute_reply":"2022-10-14T19:02:25.969562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"window_size = 60\nbatch_size = 100\ngenerator = TimeseriesGenerator(scaled_train, scaled_train, length=window_size, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:25.972226Z","iopub.execute_input":"2022-10-14T19:02:25.972909Z","iopub.status.idle":"2022-10-14T19:02:25.980537Z","shell.execute_reply.started":"2022-10-14T19:02:25.972858Z","shell.execute_reply":"2022-10-14T19:02:25.979082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(scaled_train)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:25.981782Z","iopub.execute_input":"2022-10-14T19:02:25.982003Z","iopub.status.idle":"2022-10-14T19:02:25.997127Z","shell.execute_reply.started":"2022-10-14T19:02:25.981975Z","shell.execute_reply":"2022-10-14T19:02:25.996304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(x_valid)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:25.998207Z","iopub.execute_input":"2022-10-14T19:02:25.998479Z","iopub.status.idle":"2022-10-14T19:02:26.004994Z","shell.execute_reply.started":"2022-10-14T19:02:25.998440Z","shell.execute_reply":"2022-10-14T19:02:26.003930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X, y= generator[0]","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:26.006482Z","iopub.execute_input":"2022-10-14T19:02:26.006788Z","iopub.status.idle":"2022-10-14T19:02:26.015482Z","shell.execute_reply.started":"2022-10-14T19:02:26.006757Z","shell.execute_reply":"2022-10-14T19:02:26.014616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:26.016993Z","iopub.execute_input":"2022-10-14T19:02:26.017256Z","iopub.status.idle":"2022-10-14T19:02:26.030950Z","shell.execute_reply.started":"2022-10-14T19:02:26.017226Z","shell.execute_reply":"2022-10-14T19:02:26.030140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rm -rf logs","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:26.032316Z","iopub.execute_input":"2022-10-14T19:02:26.033411Z","iopub.status.idle":"2022-10-14T19:02:27.215906Z","shell.execute_reply.started":"2022-10-14T19:02:26.033368Z","shell.execute_reply":"2022-10-14T19:02:27.214784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_folder = \"logs\"","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:27.217864Z","iopub.execute_input":"2022-10-14T19:02:27.218179Z","iopub.status.idle":"2022-10-14T19:02:27.223512Z","shell.execute_reply.started":"2022-10-14T19:02:27.218144Z","shell.execute_reply":"2022-10-14T19:02:27.222484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks =  [TensorBoard(log_dir=log_folder),EarlyStopping(patience=10,monitor=\"mae\",mode=\"min\")]","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:27.230781Z","iopub.execute_input":"2022-10-14T19:02:27.231180Z","iopub.status.idle":"2022-10-14T19:02:27.242646Z","shell.execute_reply.started":"2022-10-14T19:02:27.231144Z","shell.execute_reply":"2022-10-14T19:02:27.241888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%load_ext tensorboard","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:27.244071Z","iopub.execute_input":"2022-10-14T19:02:27.244316Z","iopub.status.idle":"2022-10-14T19:02:27.266199Z","shell.execute_reply.started":"2022-10-14T19:02:27.244286Z","shell.execute_reply":"2022-10-14T19:02:27.265429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_features = 1\n\nmodel = tf.keras.models.Sequential([ \n  tf.keras.layers.LSTM(60, input_shape=(window_size, n_features),return_sequences=True),\n  tf.keras.layers.LSTM(60),\n  tf.keras.layers.Dense(30, activation=\"relu\"),\n  tf.keras.layers.Dense(10, activation=\"relu\"),\n  tf.keras.layers.Dense(1),\n])\n\n\nmodel.compile(loss=tf.keras.losses.MeanSquaredError(),\n              optimizer=tf.keras.optimizers.Adam(),\n              metrics=[\"mse\"])","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:27.267433Z","iopub.execute_input":"2022-10-14T19:02:27.267695Z","iopub.status.idle":"2022-10-14T19:02:27.941171Z","shell.execute_reply.started":"2022-10-14T19:02:27.267661Z","shell.execute_reply":"2022-10-14T19:02:27.940381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 500\nhistory = model.fit(generator,epochs=epochs,callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:02:27.942657Z","iopub.execute_input":"2022-10-14T19:02:27.943002Z","iopub.status.idle":"2022-10-14T19:12:45.817726Z","shell.execute_reply.started":"2022-10-14T19:02:27.942956Z","shell.execute_reply":"2022-10-14T19:12:45.816484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval_batch = scaled_train[-window_size:]","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:45.820349Z","iopub.execute_input":"2022-10-14T19:12:45.820681Z","iopub.status.idle":"2022-10-14T19:12:45.827517Z","shell.execute_reply.started":"2022-10-14T19:12:45.820634Z","shell.execute_reply":"2022-10-14T19:12:45.826599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(eval_batch)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:45.829067Z","iopub.execute_input":"2022-10-14T19:12:45.829325Z","iopub.status.idle":"2022-10-14T19:12:45.842368Z","shell.execute_reply.started":"2022-10-14T19:12:45.829295Z","shell.execute_reply":"2022-10-14T19:12:45.841464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get last 60 values\neval_batch = eval_batch.reshape((1,window_size,n_features))","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:45.843503Z","iopub.execute_input":"2022-10-14T19:12:45.843718Z","iopub.status.idle":"2022-10-14T19:12:45.852283Z","shell.execute_reply.started":"2022-10-14T19:12:45.843692Z","shell.execute_reply":"2022-10-14T19:12:45.851581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval_batch","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:45.853253Z","iopub.execute_input":"2022-10-14T19:12:45.853462Z","iopub.status.idle":"2022-10-14T19:12:45.868761Z","shell.execute_reply.started":"2022-10-14T19:12:45.853436Z","shell.execute_reply":"2022-10-14T19:12:45.867749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"eval_batch.shape","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:45.870018Z","iopub.execute_input":"2022-10-14T19:12:45.870852Z","iopub.status.idle":"2022-10-14T19:12:45.881513Z","shell.execute_reply.started":"2022-10-14T19:12:45.870814Z","shell.execute_reply":"2022-10-14T19:12:45.880872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_batch = model.predict(eval_batch)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:45.882870Z","iopub.execute_input":"2022-10-14T19:12:45.883878Z","iopub.status.idle":"2022-10-14T19:12:46.972496Z","shell.execute_reply.started":"2022-10-14T19:12:45.883820Z","shell.execute_reply":"2022-10-14T19:12:46.971789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict_batch[0]","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:46.975672Z","iopub.execute_input":"2022-10-14T19:12:46.975950Z","iopub.status.idle":"2022-10-14T19:12:46.984055Z","shell.execute_reply.started":"2022-10-14T19:12:46.975920Z","shell.execute_reply":"2022-10-14T19:12:46.982962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions = []\n\nfirst_eval_batch = scaled_train[-window_size:]\ncurrent_batch = first_eval_batch.reshape((1, window_size, n_features))","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:46.985839Z","iopub.execute_input":"2022-10-14T19:12:46.986567Z","iopub.status.idle":"2022-10-14T19:12:46.995709Z","shell.execute_reply.started":"2022-10-14T19:12:46.986525Z","shell.execute_reply":"2022-10-14T19:12:46.994733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"first_eval_batch","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:46.997758Z","iopub.execute_input":"2022-10-14T19:12:46.998009Z","iopub.status.idle":"2022-10-14T19:12:47.010771Z","shell.execute_reply.started":"2022-10-14T19:12:46.997981Z","shell.execute_reply":"2022-10-14T19:12:47.009831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(first_eval_batch)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:47.012144Z","iopub.execute_input":"2022-10-14T19:12:47.012390Z","iopub.status.idle":"2022-10-14T19:12:47.023736Z","shell.execute_reply.started":"2022-10-14T19:12:47.012360Z","shell.execute_reply":"2022-10-14T19:12:47.022838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"current_pred = model.predict(current_batch)[0]","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:47.025317Z","iopub.execute_input":"2022-10-14T19:12:47.025713Z","iopub.status.idle":"2022-10-14T19:12:47.097983Z","shell.execute_reply.started":"2022-10-14T19:12:47.025668Z","shell.execute_reply":"2022-10-14T19:12:47.097178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"current_pred","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:47.099712Z","iopub.execute_input":"2022-10-14T19:12:47.100238Z","iopub.status.idle":"2022-10-14T19:12:47.105171Z","shell.execute_reply.started":"2022-10-14T19:12:47.100198Z","shell.execute_reply":"2022-10-14T19:12:47.104480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions\ntest_predictions.append(current_pred) ","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:47.106588Z","iopub.execute_input":"2022-10-14T19:12:47.107061Z","iopub.status.idle":"2022-10-14T19:12:47.118768Z","shell.execute_reply.started":"2022-10-14T19:12:47.107024Z","shell.execute_reply":"2022-10-14T19:12:47.117329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:47.120639Z","iopub.execute_input":"2022-10-14T19:12:47.121174Z","iopub.status.idle":"2022-10-14T19:12:47.133926Z","shell.execute_reply.started":"2022-10-14T19:12:47.121124Z","shell.execute_reply":"2022-10-14T19:12:47.132922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"current_batch = np.append(current_batch[:,1:,:],[[current_pred]],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:47.135189Z","iopub.execute_input":"2022-10-14T19:12:47.135447Z","iopub.status.idle":"2022-10-14T19:12:47.145923Z","shell.execute_reply.started":"2022-10-14T19:12:47.135415Z","shell.execute_reply":"2022-10-14T19:12:47.144914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'])","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:14:58.394662Z","iopub.execute_input":"2022-10-14T19:14:58.395032Z","iopub.status.idle":"2022-10-14T19:14:58.610216Z","shell.execute_reply.started":"2022-10-14T19:14:58.394996Z","shell.execute_reply":"2022-10-14T19:14:58.609346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predictions = []\n\nfirst_eval_batch = scaled_train[-window_size:]\ncurrent_batch = first_eval_batch.reshape((1, window_size, n_features))\n\nfor i in range(len(x_valid)):\n    \n    current_pred = model.predict(current_batch)[0]\n    \n    test_predictions.append(current_pred) \n    \n    current_batch = np.append(current_batch[:,1:,:],[[current_pred]],axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:47.812221Z","iopub.execute_input":"2022-10-14T19:12:47.812876Z","iopub.status.idle":"2022-10-14T19:12:56.001625Z","shell.execute_reply.started":"2022-10-14T19:12:47.812838Z","shell.execute_reply":"2022-10-14T19:12:56.000512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_predictions = scaler.inverse_transform(test_predictions)","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:56.003026Z","iopub.execute_input":"2022-10-14T19:12:56.003304Z","iopub.status.idle":"2022-10-14T19:12:56.009079Z","shell.execute_reply.started":"2022-10-14T19:12:56.003272Z","shell.execute_reply":"2022-10-14T19:12:56.008051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_predictions","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:14:31.036916Z","iopub.execute_input":"2022-10-14T19:14:31.037958Z","iopub.status.idle":"2022-10-14T19:14:31.047106Z","shell.execute_reply.started":"2022-10-14T19:14:31.037911Z","shell.execute_reply":"2022-10-14T19:14:31.046348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_valid['Predictions'] = true_predictions","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:56.010616Z","iopub.execute_input":"2022-10-14T19:12:56.011080Z","iopub.status.idle":"2022-10-14T19:12:56.030765Z","shell.execute_reply.started":"2022-10-14T19:12:56.011034Z","shell.execute_reply":"2022-10-14T19:12:56.029752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_valid.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:56.032449Z","iopub.execute_input":"2022-10-14T19:12:56.032711Z","iopub.status.idle":"2022-10-14T19:12:56.050907Z","shell.execute_reply.started":"2022-10-14T19:12:56.032680Z","shell.execute_reply":"2022-10-14T19:12:56.049772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_valid.plot(figsize=(10,6))","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:56.052460Z","iopub.execute_input":"2022-10-14T19:12:56.052700Z","iopub.status.idle":"2022-10-14T19:12:56.396077Z","shell.execute_reply.started":"2022-10-14T19:12:56.052670Z","shell.execute_reply":"2022-10-14T19:12:56.395160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.metrics.mean_absolute_error(x_valid[\"y\"],x_valid[\"Predictions\"]).numpy()","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:56.397675Z","iopub.execute_input":"2022-10-14T19:12:56.398464Z","iopub.status.idle":"2022-10-14T19:12:56.408778Z","shell.execute_reply.started":"2022-10-14T19:12:56.398410Z","shell.execute_reply":"2022-10-14T19:12:56.407532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %tensorboard --logdir logs","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:12:56.410584Z","iopub.execute_input":"2022-10-14T19:12:56.411364Z","iopub.status.idle":"2022-10-14T19:12:56.419352Z","shell.execute_reply.started":"2022-10-14T19:12:56.411313Z","shell.execute_reply":"2022-10-14T19:12:56.418340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"experiment.end()","metadata":{"execution":{"iopub.status.busy":"2022-10-14T19:27:08.434106Z","iopub.execute_input":"2022-10-14T19:27:08.434502Z","iopub.status.idle":"2022-10-14T19:39:36.239224Z","shell.execute_reply.started":"2022-10-14T19:27:08.434460Z","shell.execute_reply":"2022-10-14T19:39:36.237968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Don't forget to up vote if you find it useful.","metadata":{}}]}