Spaces:
Runtime error
Runtime error
File size: 3,656 Bytes
5fe24db |
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import pickle\n",
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"import xgboost as xgb\n",
"model_path = \"model.None\"\n",
"model = xgb.Booster()\n",
"model.load_model(model_path)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"dv_path = \"dv.bin\"\n",
"with open(dv_path, 'rb') as f_out:\n",
" dv = pickle.load(f_out)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"scaler_path = \"scaler.bin\"\n",
"with open(scaler_path, 'rb') as f_out:\n",
" scaler = pickle.load(f_out)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"def preprocess(data):\n",
" \"\"\"Preprocessing of the data\"\"\"\n",
" # turn json input to dataframe\n",
" data = pd.DataFrame([data])\n",
"\n",
" # define numerical and categorical features\n",
" numerical = [\"X1\", \"X2\", \"X3\", \"X4\", \"X5\", \"X7\"]\n",
" categorical = [\"X6\", \"X8\"]\n",
"\n",
" # preprocess numerical features\n",
" X_num = scaler.transform(data[numerical])\n",
" # preprocess categorical features\n",
" data[categorical] = data[categorical].astype(\"string\")\n",
" X_dicts = data[categorical].to_dict(orient=\"records\")\n",
" X_cat = dv.transform(X_dicts)\n",
" # concatenate both\n",
" X = np.concatenate((X_num, X_cat), axis=1)\n",
"\n",
" return X\n",
"\n",
"\n",
"def predict(X):\n",
" \"\"\"make predictions\"\"\"\n",
" pred = model.predict(X)\n",
" print('prediction', pred[0])\n",
" return float(pred[0])"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def main(input_data):\n",
" \"\"\"request input, preprocess it and make prediction\"\"\"\n",
" features = preprocess(input_data)\n",
" features_2 = xgb.DMatrix(features)\n",
" pred = predict(features_2)\n",
"\n",
" result = {'heat load': pred}\n",
"\n",
" return result\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"prediction 15.648413\n"
]
},
{
"data": {
"text/plain": [
"{'heat load': 15.648412704467773}"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"input_example = {\n",
" \"X1\": 0.98,\n",
" \"X2\": 514.50,\n",
" \"X3\": 294.00,\n",
" \"X4\": 110.25,\n",
" \"X5\": 7.00,\n",
" \"X6\": 2,\n",
" \"X7\": 0.00,\n",
" \"X8\": 0,\n",
"}\n",
"\n",
"main(input_example)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "mlops",
"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.10.8"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|