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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 12,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import gradio as gr\n",
|
10 |
+
"import tensorflow as tf\n",
|
11 |
+
"import numpy as np\n",
|
12 |
+
"import pandas as pd"
|
13 |
+
]
|
14 |
+
},
|
15 |
+
{
|
16 |
+
"cell_type": "code",
|
17 |
+
"execution_count": 13,
|
18 |
+
"metadata": {},
|
19 |
+
"outputs": [],
|
20 |
+
"source": [
|
21 |
+
"\n",
|
22 |
+
"model_path = \"kia_apartment_keras_model.keras\"\n",
|
23 |
+
"model = tf.keras.models.load_model(model_path)\n",
|
24 |
+
"\n",
|
25 |
+
"df_bfs_data = pd.read_csv('bfs_municipality_and_tax_data.csv', sep=',', encoding='utf-8')\n",
|
26 |
+
"df_bfs_data['tax_income'] = df_bfs_data['tax_income'].str.replace(\"'\", \"\").astype(float)"
|
27 |
+
]
|
28 |
+
},
|
29 |
+
{
|
30 |
+
"cell_type": "code",
|
31 |
+
"execution_count": 14,
|
32 |
+
"metadata": {},
|
33 |
+
"outputs": [],
|
34 |
+
"source": [
|
35 |
+
"locations = {\n",
|
36 |
+
" \"Zürich\": 261,\n",
|
37 |
+
" \"Kloten\": 62,\n",
|
38 |
+
" \"Uster\": 198,\n",
|
39 |
+
" \"Illnau-Effretikon\": 296,\n",
|
40 |
+
" \"Feuerthalen\": 27,\n",
|
41 |
+
" \"Pfäffikon\": 177,\n",
|
42 |
+
" \"Ottenbach\": 11,\n",
|
43 |
+
" \"Dübendorf\": 191,\n",
|
44 |
+
" \"Richterswil\": 138,\n",
|
45 |
+
" \"Maur\": 195,\n",
|
46 |
+
" \"Embrach\": 56,\n",
|
47 |
+
" \"Bülach\": 53,\n",
|
48 |
+
" \"Winterthur\": 230,\n",
|
49 |
+
" \"Oetwil am See\": 157,\n",
|
50 |
+
" \"Russikon\": 178,\n",
|
51 |
+
" \"Obfelden\": 10,\n",
|
52 |
+
" \"Wald (ZH)\": 120,\n",
|
53 |
+
" \"Niederweningen\": 91,\n",
|
54 |
+
" \"Dällikon\": 84,\n",
|
55 |
+
" \"Buchs (ZH)\": 83,\n",
|
56 |
+
" \"Rüti (ZH)\": 118,\n",
|
57 |
+
" \"Hittnau\": 173,\n",
|
58 |
+
" \"Bassersdorf\": 52,\n",
|
59 |
+
" \"Glattfelden\": 58,\n",
|
60 |
+
" \"Opfikon\": 66,\n",
|
61 |
+
" \"Hinwil\": 117,\n",
|
62 |
+
" \"Regensberg\": 95,\n",
|
63 |
+
" \"Langnau am Albis\": 136,\n",
|
64 |
+
" \"Dietikon\": 243,\n",
|
65 |
+
" \"Erlenbach (ZH)\": 151,\n",
|
66 |
+
" \"Kappel am Albis\": 6,\n",
|
67 |
+
" \"Stäfa\": 158,\n",
|
68 |
+
" \"Zell (ZH)\": 231,\n",
|
69 |
+
" \"Turbenthal\": 228,\n",
|
70 |
+
" \"Oberglatt\": 92,\n",
|
71 |
+
" \"Winkel\": 72,\n",
|
72 |
+
" \"Volketswil\": 199,\n",
|
73 |
+
" \"Kilchberg (ZH)\": 135,\n",
|
74 |
+
" \"Wetzikon (ZH)\": 121,\n",
|
75 |
+
" \"Zumikon\": 160,\n",
|
76 |
+
" \"Weisslingen\": 180,\n",
|
77 |
+
" \"Elsau\": 219,\n",
|
78 |
+
" \"Hettlingen\": 221,\n",
|
79 |
+
" \"Rüschlikon\": 139,\n",
|
80 |
+
" \"Stallikon\": 13,\n",
|
81 |
+
" \"Dielsdorf\": 86,\n",
|
82 |
+
" \"Wallisellen\": 69,\n",
|
83 |
+
" \"Dietlikon\": 54,\n",
|
84 |
+
" \"Meilen\": 156,\n",
|
85 |
+
" \"Wangen-Brüttisellen\": 200,\n",
|
86 |
+
" \"Flaach\": 28,\n",
|
87 |
+
" \"Regensdorf\": 96,\n",
|
88 |
+
" \"Niederhasli\": 90,\n",
|
89 |
+
" \"Bauma\": 297,\n",
|
90 |
+
" \"Aesch (ZH)\": 241,\n",
|
91 |
+
" \"Schlieren\": 247,\n",
|
92 |
+
" \"Dürnten\": 113,\n",
|
93 |
+
" \"Unterengstringen\": 249,\n",
|
94 |
+
" \"Gossau (ZH)\": 115,\n",
|
95 |
+
" \"Oberengstringen\": 245,\n",
|
96 |
+
" \"Schleinikon\": 98,\n",
|
97 |
+
" \"Aeugst am Albis\": 1,\n",
|
98 |
+
" \"Rheinau\": 38,\n",
|
99 |
+
" \"Höri\": 60,\n",
|
100 |
+
" \"Rickenbach (ZH)\": 225,\n",
|
101 |
+
" \"Rafz\": 67,\n",
|
102 |
+
" \"Adliswil\": 131,\n",
|
103 |
+
" \"Zollikon\": 161,\n",
|
104 |
+
" \"Urdorf\": 250,\n",
|
105 |
+
" \"Hombrechtikon\": 153,\n",
|
106 |
+
" \"Birmensdorf (ZH)\": 242,\n",
|
107 |
+
" \"Fehraltorf\": 172,\n",
|
108 |
+
" \"Weiach\": 102,\n",
|
109 |
+
" \"Männedorf\": 155,\n",
|
110 |
+
" \"Küsnacht (ZH)\": 154,\n",
|
111 |
+
" \"Hausen am Albis\": 4,\n",
|
112 |
+
" \"Hochfelden\": 59,\n",
|
113 |
+
" \"Fällanden\": 193,\n",
|
114 |
+
" \"Greifensee\": 194,\n",
|
115 |
+
" \"Mönchaltorf\": 196,\n",
|
116 |
+
" \"Dägerlen\": 214,\n",
|
117 |
+
" \"Thalheim an der Thur\": 39,\n",
|
118 |
+
" \"Uetikon am See\": 159,\n",
|
119 |
+
" \"Seuzach\": 227,\n",
|
120 |
+
" \"Uitikon\": 248,\n",
|
121 |
+
" \"Affoltern am Albis\": 2,\n",
|
122 |
+
" \"Geroldswil\": 244,\n",
|
123 |
+
" \"Niederglatt\": 89,\n",
|
124 |
+
" \"Thalwil\": 141,\n",
|
125 |
+
" \"Rorbas\": 68,\n",
|
126 |
+
" \"Pfungen\": 224,\n",
|
127 |
+
" \"Weiningen (ZH)\": 251,\n",
|
128 |
+
" \"Bubikon\": 112,\n",
|
129 |
+
" \"Neftenbach\": 223,\n",
|
130 |
+
" \"Mettmenstetten\": 9,\n",
|
131 |
+
" \"Otelfingen\": 94,\n",
|
132 |
+
" \"Flurlingen\": 29,\n",
|
133 |
+
" \"Stadel\": 100,\n",
|
134 |
+
" \"Grüningen\": 116,\n",
|
135 |
+
" \"Henggart\": 31,\n",
|
136 |
+
" \"Dachsen\": 25,\n",
|
137 |
+
" \"Bonstetten\": 3,\n",
|
138 |
+
" \"Bachenbülach\": 51,\n",
|
139 |
+
" \"Horgen\": 295\n",
|
140 |
+
"}"
|
141 |
+
]
|
142 |
+
},
|
143 |
+
{
|
144 |
+
"cell_type": "code",
|
145 |
+
"execution_count": 15,
|
146 |
+
"metadata": {},
|
147 |
+
"outputs": [],
|
148 |
+
"source": [
|
149 |
+
"# Define the core prediction function\n",
|
150 |
+
"def predict_apartment(rooms, area, town):\n",
|
151 |
+
" bfs_number = locations[town]\n",
|
152 |
+
" df = df_bfs_data[df_bfs_data['bfs_number']==bfs_number]\n",
|
153 |
+
" \n",
|
154 |
+
" if len(df) != 1: # if there are more than two records with the same bfs_number reutrn -1\n",
|
155 |
+
" return -1\n",
|
156 |
+
" \n",
|
157 |
+
" input_data = np.array([rooms, area, df['pop'].iloc[0], df['pop_dens'].iloc[0], df['frg_pct'].iloc[0], df['emp'].iloc[0], df['tax_income'].iloc[0]])\n",
|
158 |
+
" input_data = input_data.reshape(1, 7)\n",
|
159 |
+
" prediction = model.predict(input_data)\n",
|
160 |
+
" return float(np.round(prediction[0][0], 0))"
|
161 |
+
]
|
162 |
+
},
|
163 |
+
{
|
164 |
+
"cell_type": "code",
|
165 |
+
"execution_count": 16,
|
166 |
+
"metadata": {},
|
167 |
+
"outputs": [
|
168 |
+
{
|
169 |
+
"name": "stdout",
|
170 |
+
"output_type": "stream",
|
171 |
+
"text": [
|
172 |
+
"Running on local URL: http://127.0.0.1:7863\n",
|
173 |
+
"\n",
|
174 |
+
"To create a public link, set `share=True` in `launch()`.\n"
|
175 |
+
]
|
176 |
+
},
|
177 |
+
{
|
178 |
+
"data": {
|
179 |
+
"text/html": [
|
180 |
+
"<div><iframe src=\"http://127.0.0.1:7863/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
181 |
+
],
|
182 |
+
"text/plain": [
|
183 |
+
"<IPython.core.display.HTML object>"
|
184 |
+
]
|
185 |
+
},
|
186 |
+
"metadata": {},
|
187 |
+
"output_type": "display_data"
|
188 |
+
},
|
189 |
+
{
|
190 |
+
"data": {
|
191 |
+
"text/plain": []
|
192 |
+
},
|
193 |
+
"execution_count": 16,
|
194 |
+
"metadata": {},
|
195 |
+
"output_type": "execute_result"
|
196 |
+
},
|
197 |
+
{
|
198 |
+
"name": "stdout",
|
199 |
+
"output_type": "stream",
|
200 |
+
"text": [
|
201 |
+
"\u001b[1m1/1\u001b[0m \u001b[32m━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[37m\u001b[0m \u001b[1m0s\u001b[0m 91ms/step\n"
|
202 |
+
]
|
203 |
+
}
|
204 |
+
],
|
205 |
+
"source": [
|
206 |
+
"# Create the Gradio interface\n",
|
207 |
+
"iface = gr.Interface(\n",
|
208 |
+
" fn=predict_apartment,\n",
|
209 |
+
" inputs=[\"number\", \"number\", gr.Dropdown(choices=locations.keys(), label=\"Town\", type=\"value\")],\n",
|
210 |
+
" outputs=gr.Number(),\n",
|
211 |
+
" examples=[[4.5, 120, \"Dietlikon\"], [3.5, 60, \"Winterthur\"]]\n",
|
212 |
+
")\n",
|
213 |
+
"\n",
|
214 |
+
"iface.launch()"
|
215 |
+
]
|
216 |
+
},
|
217 |
+
{
|
218 |
+
"cell_type": "code",
|
219 |
+
"execution_count": null,
|
220 |
+
"metadata": {},
|
221 |
+
"outputs": [],
|
222 |
+
"source": []
|
223 |
+
}
|
224 |
+
],
|
225 |
+
"metadata": {
|
226 |
+
"kernelspec": {
|
227 |
+
"display_name": "venv_new",
|
228 |
+
"language": "python",
|
229 |
+
"name": "python3"
|
230 |
+
},
|
231 |
+
"language_info": {
|
232 |
+
"codemirror_mode": {
|
233 |
+
"name": "ipython",
|
234 |
+
"version": 3
|
235 |
+
},
|
236 |
+
"file_extension": ".py",
|
237 |
+
"mimetype": "text/x-python",
|
238 |
+
"name": "python",
|
239 |
+
"nbconvert_exporter": "python",
|
240 |
+
"pygments_lexer": "ipython3",
|
241 |
+
"version": "3.11.3"
|
242 |
+
}
|
243 |
+
},
|
244 |
+
"nbformat": 4,
|
245 |
+
"nbformat_minor": 2
|
246 |
+
}
|
bfs_municipality_and_tax_data.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
kia_apartment_keras_model.keras
ADDED
Binary file (97.6 kB). View file
|
|