Spaces:
Sleeping
Sleeping
File size: 4,290 Bytes
cd9d584 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
{
"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
}
|