{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "def initialize_lists(size=30):\n", " initial_values = [0] * size\n", " return initial_values.copy(), initial_values.copy(), initial_values.copy()\n", "\n", "def get_window(df, len_df):\n", " if len_df > 30:\n", " return df[len_df-31:len_df].astype('float32')\n", " else:\n", " return None\n", "\n", "def transform_window(df_window, scaler):\n", " return scaler.transform(df_window)\n", "\n", "def prepare_input(df_trans):\n", " return df_trans[:30,:].reshape((1,30,30))\n", "\n", "def predict(model, df_new):\n", " return model.predict(df_new)\n", "\n", "def calculate_residuals(df_trans, pred):\n", " actual = df_trans[30,:25]\n", " resid = actual - pred\n", " return actual, resid\n", "\n", "def resize_prediction(pred, df_trans):\n", " pred.resize((pred.shape[0], pred.shape[1] + len(df_trans[30,25:])))\n", " pred[:, -len(df_trans[30,25:]):] = df_trans[30,25:]\n", " return pred\n", "\n", "def inverse_transform(scaler, pred, df_trans):\n", " pred = scaler.inverse_transform(np.array(pred))\n", " actual = scaler.inverse_transform(np.array([df_trans[30,:]]))\n", " return actual, pred\n", "\n", "def update_lists(actual_list, pred_list, resid_list, actual, pred, resid):\n", " actual_list.pop(0)\n", " pred_list.pop(0)\n", " resid_list.pop(0)\n", " actual_list.append(actual[0,1])\n", " pred_list.append(pred[0,1])\n", " resid_list.append(resid[0,1])\n", " return actual_list, pred_list, resid_list\n", "\n", "def calculate_distances(resid, kmeans1, kmeans2, kmeans3, kmeans4):\n", " dist = []\n", " dist.append(np.linalg.norm(resid[:,1:8]-kmeans1.cluster_centers_[0], ord=2, axis=1))\n", " dist.append(np.linalg.norm(resid[:,8:15]-kmeans2.cluster_centers_[0], ord=2, axis=1))\n", " dist.append(np.linalg.norm(resid[:,15:22]-kmeans3.cluster_centers_[0], ord=2, axis=1))\n", " dist.append(np.linalg.norm(resid[:,22:29]-kmeans4.cluster_centers_[0], ord=2, axis=1))\n", " return np.array(dist)\n", "\n", "def pipeline(df, scaler, model, kmeans1, kmeans2, kmeans3, kmeans4):\n", " actual_list, pred_list, resid_list = initialize_lists()\n", " len_df = np.len(df)\n", " df_window = get_window(df, len_df)\n", " if df_window is not None:\n", " df_trans = transform_window(df_window, scaler)\n", " df_new = prepare_input(df_trans)\n", " pred = predict(model, df_new)\n", " actual, resid = calculate_residuals(df_trans, pred)\n", " pred = resize_prediction(pred, df_trans)\n", " actual, pred = inverse_transform(scaler, pred, df_trans)\n", " actual_list, pred_list, resid_list = update_lists(actual_list, pred_list, resid_list, actual, pred, resid)\n", " dist = calculate_distances(resid, kmeans1, kmeans2, kmeans3, kmeans4)\n", " return actual_list, pred_list, resid_list, dist\n", " else:\n", " return actual_list, pred_list, resid_list, None\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'd']" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inp = ['a', 'b']\n", "out = ['c','d']\n", "\n", "inp+out" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "smartbuilding", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.8" } }, "nbformat": 4, "nbformat_minor": 2 }