Spaces:
Sleeping
Sleeping
Upload 19 files
Browse files- Lezione 0.A - Python.ipynb +1496 -0
- Lezione 0.E - Esercizi.ipynb +451 -0
- Lezione 0.S - Soluzioni.ipynb +0 -0
- Lezione 1.A - Pandas e Feature Engineering.ipynb +0 -0
- Lezione 1.E - Esercizi.ipynb +874 -0
- Lezione 1.S - Soluzioni.ipynb +0 -0
- Lezione 2.A - Regressione e Classificazione.ipynb +0 -0
- Lezione 2.E - Esercizi.ipynb +891 -0
- Lezione 2.S - Soluzioni.ipynb +0 -0
- Lezione 3.A - Clustering e anomaly detection.ipynb +0 -0
- Lezione 3.E - Esercizi.ipynb +480 -0
- Lezione 3.S - Soluzioni.ipynb +0 -0
- Lezione 4.A - Time Series.ipynb +0 -0
- Lezione 4.E - Esercizi.ipynb +294 -0
- Lezione 4.S - Soluzioni.ipynb +0 -0
- Lezione 5.A - Machine Learning.ipynb +0 -0
- Lezione 5.E - Esercizi.ipynb +289 -0
- Lezione 5.S - Soluzioni.ipynb +0 -0
- Lezione 6.A - Deep Learning.ipynb +0 -0
Lezione 0.A - Python.ipynb
ADDED
|
@@ -0,0 +1,1496 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"# Lezione 0\n",
|
| 8 |
+
"\n",
|
| 9 |
+
"#### In questo primo notebook introdurremo i concetti fondamentali per iniziare ad usare jupyter e Python ai fini del calcolo scientifico.\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"Il notebook è così suddiviso: <br>\n",
|
| 12 |
+
"1) [Hello, Python](#section1)<a href='#section1'></a> <br>\n",
|
| 13 |
+
"2) [Le funzioni](#section2)<a href='#section2'></a><br>\n",
|
| 14 |
+
"3) [Booleans & Condizioni](#section3)<a href='#section3'></a> <br>\n",
|
| 15 |
+
"4) [Le liste](#section4)<a href='#section4'></a> <br>\n",
|
| 16 |
+
"5) [Loops](#section5)<a href='#section5'></a><br>\n",
|
| 17 |
+
"6) [I dizionari](#section6)<a href='#section6'></a><br>\n",
|
| 18 |
+
"7) [Le librerie esterne](#section7)<a href='#section7'></a>\n",
|
| 19 |
+
"\n"
|
| 20 |
+
]
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"cell_type": "markdown",
|
| 24 |
+
"metadata": {},
|
| 25 |
+
"source": [
|
| 26 |
+
"<a id='section1'></a>\n",
|
| 27 |
+
"## 1) Hello, Python!"
|
| 28 |
+
]
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"cell_type": "markdown",
|
| 32 |
+
"metadata": {},
|
| 33 |
+
"source": [
|
| 34 |
+
"Python è un linguaggio interpretato, ovvero a differenza di C++ (che è un linguaggio compilato), esegue il codice riga-per-riga, mentre C++ compila il codice e successivamente lo esegue. <br>\n",
|
| 35 |
+
"Il vantaggio dei linguaggi di programmazione \"interpretati\" è che sono più facili \"da leggere\", ma più lenti in esecuzione rispetto ad un linguaggio compilato. <br>\n",
|
| 36 |
+
"Python utilizza molte librerie, scritte in codice nativo, che permettono di avere prestazioni paragonabili a quelle dei linguaggi compilati.<br>\n",
|
| 37 |
+
"<br>\n",
|
| 38 |
+
"Ecco un esempio di programma python. "
|
| 39 |
+
]
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"cell_type": "code",
|
| 43 |
+
"execution_count": 1,
|
| 44 |
+
"metadata": {
|
| 45 |
+
"tags": []
|
| 46 |
+
},
|
| 47 |
+
"outputs": [
|
| 48 |
+
{
|
| 49 |
+
"name": "stdout",
|
| 50 |
+
"output_type": "stream",
|
| 51 |
+
"text": [
|
| 52 |
+
"Ho 6 pere\n",
|
| 53 |
+
"Esempio finito.\n"
|
| 54 |
+
]
|
| 55 |
+
}
|
| 56 |
+
],
|
| 57 |
+
"source": [
|
| 58 |
+
"# Importo la libreria random \n",
|
| 59 |
+
"# Servirà per generare numeri casuali\n",
|
| 60 |
+
"import random as rd\n",
|
| 61 |
+
"\n",
|
| 62 |
+
"pere = 0\n",
|
| 63 |
+
"\n",
|
| 64 |
+
"# Genero un numero casuale tra 0 e 10\n",
|
| 65 |
+
"Pere_comprate = rd.randint(0, 10) \n",
|
| 66 |
+
"Pere_totali = pere + Pere_comprate\n",
|
| 67 |
+
"\n",
|
| 68 |
+
"if Pere_totali > 0:\n",
|
| 69 |
+
" print(\"Ho\", Pere_totali, \"pere\")\n",
|
| 70 |
+
"else:\n",
|
| 71 |
+
" print(\"Non ho nessuna pera.\")\n",
|
| 72 |
+
"\n",
|
| 73 |
+
"print(\"Esempio finito.\")"
|
| 74 |
+
]
|
| 75 |
+
},
|
| 76 |
+
{
|
| 77 |
+
"cell_type": "markdown",
|
| 78 |
+
"metadata": {},
|
| 79 |
+
"source": [
|
| 80 |
+
"Vediamo ora qualche aspetto del linguaggio:"
|
| 81 |
+
]
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
"cell_type": "code",
|
| 85 |
+
"execution_count": 2,
|
| 86 |
+
"metadata": {},
|
| 87 |
+
"outputs": [],
|
| 88 |
+
"source": [
|
| 89 |
+
"pere = 0"
|
| 90 |
+
]
|
| 91 |
+
},
|
| 92 |
+
{
|
| 93 |
+
"cell_type": "markdown",
|
| 94 |
+
"metadata": {},
|
| 95 |
+
"source": [
|
| 96 |
+
"In python le variaibli vengono create senza dover definire il loro tipo.\n",
|
| 97 |
+
"Per conoscere l'effettiva tipologia delle variabili che usiamo in Python possiamo usare la funzione **type**:"
|
| 98 |
+
]
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"cell_type": "code",
|
| 102 |
+
"execution_count": 3,
|
| 103 |
+
"metadata": {},
|
| 104 |
+
"outputs": [
|
| 105 |
+
{
|
| 106 |
+
"data": {
|
| 107 |
+
"text/plain": [
|
| 108 |
+
"int"
|
| 109 |
+
]
|
| 110 |
+
},
|
| 111 |
+
"execution_count": 3,
|
| 112 |
+
"metadata": {},
|
| 113 |
+
"output_type": "execute_result"
|
| 114 |
+
}
|
| 115 |
+
],
|
| 116 |
+
"source": [
|
| 117 |
+
"type(0)"
|
| 118 |
+
]
|
| 119 |
+
},
|
| 120 |
+
{
|
| 121 |
+
"cell_type": "code",
|
| 122 |
+
"execution_count": 4,
|
| 123 |
+
"metadata": {},
|
| 124 |
+
"outputs": [
|
| 125 |
+
{
|
| 126 |
+
"data": {
|
| 127 |
+
"text/plain": [
|
| 128 |
+
"float"
|
| 129 |
+
]
|
| 130 |
+
},
|
| 131 |
+
"execution_count": 4,
|
| 132 |
+
"metadata": {},
|
| 133 |
+
"output_type": "execute_result"
|
| 134 |
+
}
|
| 135 |
+
],
|
| 136 |
+
"source": [
|
| 137 |
+
"type(2.5)"
|
| 138 |
+
]
|
| 139 |
+
},
|
| 140 |
+
{
|
| 141 |
+
"cell_type": "markdown",
|
| 142 |
+
"metadata": {},
|
| 143 |
+
"source": [
|
| 144 |
+
"Di seguito mostriamo le operazioni aritmetiche che possono essere fatte in Python:"
|
| 145 |
+
]
|
| 146 |
+
},
|
| 147 |
+
{
|
| 148 |
+
"cell_type": "markdown",
|
| 149 |
+
"metadata": {},
|
| 150 |
+
"source": [
|
| 151 |
+
"<img src=\"img/operators in Python.jpg\">"
|
| 152 |
+
]
|
| 153 |
+
},
|
| 154 |
+
{
|
| 155 |
+
"cell_type": "markdown",
|
| 156 |
+
"metadata": {},
|
| 157 |
+
"source": [
|
| 158 |
+
"<a id='section2'></a>\n",
|
| 159 |
+
"## 2) Le funzioni"
|
| 160 |
+
]
|
| 161 |
+
},
|
| 162 |
+
{
|
| 163 |
+
"cell_type": "markdown",
|
| 164 |
+
"metadata": {},
|
| 165 |
+
"source": [
|
| 166 |
+
"Una delle funzioni più utili è **help()**, infatti grazie a questa funzione è possibile capire qualsiasi altra funzione che si può usare in Python."
|
| 167 |
+
]
|
| 168 |
+
},
|
| 169 |
+
{
|
| 170 |
+
"cell_type": "code",
|
| 171 |
+
"execution_count": 5,
|
| 172 |
+
"metadata": {},
|
| 173 |
+
"outputs": [
|
| 174 |
+
{
|
| 175 |
+
"data": {
|
| 176 |
+
"text/plain": [
|
| 177 |
+
"<function abs(x, /)>"
|
| 178 |
+
]
|
| 179 |
+
},
|
| 180 |
+
"execution_count": 5,
|
| 181 |
+
"metadata": {},
|
| 182 |
+
"output_type": "execute_result"
|
| 183 |
+
}
|
| 184 |
+
],
|
| 185 |
+
"source": [
|
| 186 |
+
"abs"
|
| 187 |
+
]
|
| 188 |
+
},
|
| 189 |
+
{
|
| 190 |
+
"cell_type": "markdown",
|
| 191 |
+
"metadata": {},
|
| 192 |
+
"source": [
|
| 193 |
+
"La funzione help() mostra: <br>\n",
|
| 194 |
+
"- L'intestazione della funzione, indicando quanti/quali argomenti prende in input la funzione;\n",
|
| 195 |
+
"- Una breve descrizione di ciò che fa la funzione. <br>"
|
| 196 |
+
]
|
| 197 |
+
},
|
| 198 |
+
{
|
| 199 |
+
"cell_type": "code",
|
| 200 |
+
"execution_count": 6,
|
| 201 |
+
"metadata": {
|
| 202 |
+
"tags": []
|
| 203 |
+
},
|
| 204 |
+
"outputs": [
|
| 205 |
+
{
|
| 206 |
+
"name": "stdout",
|
| 207 |
+
"output_type": "stream",
|
| 208 |
+
"text": [
|
| 209 |
+
"Help on built-in function print in module builtins:\n",
|
| 210 |
+
"\n",
|
| 211 |
+
"print(...)\n",
|
| 212 |
+
" print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
|
| 213 |
+
" \n",
|
| 214 |
+
" Prints the values to a stream, or to sys.stdout by default.\n",
|
| 215 |
+
" Optional keyword arguments:\n",
|
| 216 |
+
" file: a file-like object (stream); defaults to the current sys.stdout.\n",
|
| 217 |
+
" sep: string inserted between values, default a space.\n",
|
| 218 |
+
" end: string appended after the last value, default a newline.\n",
|
| 219 |
+
" flush: whether to forcibly flush the stream.\n",
|
| 220 |
+
"\n"
|
| 221 |
+
]
|
| 222 |
+
}
|
| 223 |
+
],
|
| 224 |
+
"source": [
|
| 225 |
+
"help(print)"
|
| 226 |
+
]
|
| 227 |
+
},
|
| 228 |
+
{
|
| 229 |
+
"cell_type": "markdown",
|
| 230 |
+
"metadata": {},
|
| 231 |
+
"source": [
|
| 232 |
+
"Ovviamente in Python è possibile definire funzioni personalizzate oltreché usare le funzioni già preimpostate."
|
| 233 |
+
]
|
| 234 |
+
},
|
| 235 |
+
{
|
| 236 |
+
"cell_type": "code",
|
| 237 |
+
"execution_count": 7,
|
| 238 |
+
"metadata": {},
|
| 239 |
+
"outputs": [],
|
| 240 |
+
"source": [
|
| 241 |
+
"def min_delta(a, b, c):\n",
|
| 242 |
+
" \"\"\" La funzione determina la più piccola differenza tra due numeri, \n",
|
| 243 |
+
" utilizzando a, b e c.\n",
|
| 244 |
+
" \n",
|
| 245 |
+
" >>> min_delta(1, 5, -5)\n",
|
| 246 |
+
" 4\n",
|
| 247 |
+
" \"\"\"\n",
|
| 248 |
+
" \n",
|
| 249 |
+
" delta_1 = abs(a-b)\n",
|
| 250 |
+
" delta_2 = abs(b-c)\n",
|
| 251 |
+
" delta_3 = abs(a-c)\n",
|
| 252 |
+
" return min(delta_1, delta_2, delta_3)\n"
|
| 253 |
+
]
|
| 254 |
+
},
|
| 255 |
+
{
|
| 256 |
+
"cell_type": "code",
|
| 257 |
+
"execution_count": 8,
|
| 258 |
+
"metadata": {
|
| 259 |
+
"tags": []
|
| 260 |
+
},
|
| 261 |
+
"outputs": [
|
| 262 |
+
{
|
| 263 |
+
"name": "stdout",
|
| 264 |
+
"output_type": "stream",
|
| 265 |
+
"text": [
|
| 266 |
+
"Help on function min_delta in module __main__:\n",
|
| 267 |
+
"\n",
|
| 268 |
+
"min_delta(a, b, c)\n",
|
| 269 |
+
" La funzione determina la più piccola differenza tra due numeri, \n",
|
| 270 |
+
" utilizzando a, b e c.\n",
|
| 271 |
+
" \n",
|
| 272 |
+
" >>> min_delta(1, 5, -5)\n",
|
| 273 |
+
" 4\n",
|
| 274 |
+
"\n"
|
| 275 |
+
]
|
| 276 |
+
}
|
| 277 |
+
],
|
| 278 |
+
"source": [
|
| 279 |
+
"help(min_delta)"
|
| 280 |
+
]
|
| 281 |
+
},
|
| 282 |
+
{
|
| 283 |
+
"cell_type": "markdown",
|
| 284 |
+
"metadata": {},
|
| 285 |
+
"source": [
|
| 286 |
+
"<a id='section3'></a>\n",
|
| 287 |
+
"## 3) Booleans & Condizioni"
|
| 288 |
+
]
|
| 289 |
+
},
|
| 290 |
+
{
|
| 291 |
+
"cell_type": "markdown",
|
| 292 |
+
"metadata": {},
|
| 293 |
+
"source": [
|
| 294 |
+
"I principali operatori che danno come risposta True/False sono i seguenti:"
|
| 295 |
+
]
|
| 296 |
+
},
|
| 297 |
+
{
|
| 298 |
+
"cell_type": "markdown",
|
| 299 |
+
"metadata": {},
|
| 300 |
+
"source": [
|
| 301 |
+
"<img src=\"img/comparison_operations.jpg\">"
|
| 302 |
+
]
|
| 303 |
+
},
|
| 304 |
+
{
|
| 305 |
+
"cell_type": "markdown",
|
| 306 |
+
"metadata": {},
|
| 307 |
+
"source": [
|
| 308 |
+
"<a id='section4'></a>\n",
|
| 309 |
+
"## 4) Le liste"
|
| 310 |
+
]
|
| 311 |
+
},
|
| 312 |
+
{
|
| 313 |
+
"cell_type": "markdown",
|
| 314 |
+
"metadata": {},
|
| 315 |
+
"source": [
|
| 316 |
+
"Le liste in Python sono una sequenza ordinati di valori e sono definite attraverso valori separati da una virgola e contenuti in parentesi quadre."
|
| 317 |
+
]
|
| 318 |
+
},
|
| 319 |
+
{
|
| 320 |
+
"cell_type": "code",
|
| 321 |
+
"execution_count": 9,
|
| 322 |
+
"metadata": {},
|
| 323 |
+
"outputs": [],
|
| 324 |
+
"source": [
|
| 325 |
+
"Numeri_primi = [1, 2, 3, 5, 7]"
|
| 326 |
+
]
|
| 327 |
+
},
|
| 328 |
+
{
|
| 329 |
+
"cell_type": "code",
|
| 330 |
+
"execution_count": 10,
|
| 331 |
+
"metadata": {},
|
| 332 |
+
"outputs": [
|
| 333 |
+
{
|
| 334 |
+
"data": {
|
| 335 |
+
"text/plain": [
|
| 336 |
+
"list"
|
| 337 |
+
]
|
| 338 |
+
},
|
| 339 |
+
"execution_count": 10,
|
| 340 |
+
"metadata": {},
|
| 341 |
+
"output_type": "execute_result"
|
| 342 |
+
}
|
| 343 |
+
],
|
| 344 |
+
"source": [
|
| 345 |
+
"type(Numeri_primi)"
|
| 346 |
+
]
|
| 347 |
+
},
|
| 348 |
+
{
|
| 349 |
+
"cell_type": "code",
|
| 350 |
+
"execution_count": 11,
|
| 351 |
+
"metadata": {},
|
| 352 |
+
"outputs": [],
|
| 353 |
+
"source": [
|
| 354 |
+
"Pianeti = ['Mercurio', 'Venere', 'Terra', 'Marte',\\\n",
|
| 355 |
+
" 'Giove', 'Saturno', 'Urano', 'Nettuno']"
|
| 356 |
+
]
|
| 357 |
+
},
|
| 358 |
+
{
|
| 359 |
+
"cell_type": "code",
|
| 360 |
+
"execution_count": 12,
|
| 361 |
+
"metadata": {},
|
| 362 |
+
"outputs": [
|
| 363 |
+
{
|
| 364 |
+
"data": {
|
| 365 |
+
"text/plain": [
|
| 366 |
+
"['Mercurio',\n",
|
| 367 |
+
" 'Venere',\n",
|
| 368 |
+
" 'Terra',\n",
|
| 369 |
+
" 'Marte',\n",
|
| 370 |
+
" 'Giove',\n",
|
| 371 |
+
" 'Saturno',\n",
|
| 372 |
+
" 'Urano',\n",
|
| 373 |
+
" 'Nettuno']"
|
| 374 |
+
]
|
| 375 |
+
},
|
| 376 |
+
"execution_count": 12,
|
| 377 |
+
"metadata": {},
|
| 378 |
+
"output_type": "execute_result"
|
| 379 |
+
}
|
| 380 |
+
],
|
| 381 |
+
"source": [
|
| 382 |
+
"Pianeti"
|
| 383 |
+
]
|
| 384 |
+
},
|
| 385 |
+
{
|
| 386 |
+
"cell_type": "markdown",
|
| 387 |
+
"metadata": {},
|
| 388 |
+
"source": [
|
| 389 |
+
"Una lista può contenere altre liste, ad esempio:"
|
| 390 |
+
]
|
| 391 |
+
},
|
| 392 |
+
{
|
| 393 |
+
"cell_type": "code",
|
| 394 |
+
"execution_count": 13,
|
| 395 |
+
"metadata": {},
|
| 396 |
+
"outputs": [],
|
| 397 |
+
"source": [
|
| 398 |
+
"Carte = [['J', 'Q', 'K'], ['2', '4', '8'], ['6', 'A', 'K']]\n",
|
| 399 |
+
"\n",
|
| 400 |
+
"# Per una lettura migliore è possibile anche scrivere nel seguente modo:\n",
|
| 401 |
+
"Carte = [\n",
|
| 402 |
+
" ['J', 'Q', 'K'], \n",
|
| 403 |
+
" ['2', '4', '8'], \n",
|
| 404 |
+
" ['6', 'A', 'K']\n",
|
| 405 |
+
"]"
|
| 406 |
+
]
|
| 407 |
+
},
|
| 408 |
+
{
|
| 409 |
+
"cell_type": "markdown",
|
| 410 |
+
"metadata": {},
|
| 411 |
+
"source": [
|
| 412 |
+
"Una lista può contenere un mix di elementi di tipo diverso:"
|
| 413 |
+
]
|
| 414 |
+
},
|
| 415 |
+
{
|
| 416 |
+
"cell_type": "code",
|
| 417 |
+
"execution_count": 14,
|
| 418 |
+
"metadata": {},
|
| 419 |
+
"outputs": [],
|
| 420 |
+
"source": [
|
| 421 |
+
"Elementi_preferiti = [27, 'Moto']"
|
| 422 |
+
]
|
| 423 |
+
},
|
| 424 |
+
{
|
| 425 |
+
"cell_type": "markdown",
|
| 426 |
+
"metadata": {},
|
| 427 |
+
"source": [
|
| 428 |
+
"È possibile accedere agli elementi di una lista di Python attraverso l'indicizzazione tramite parentesi quadre. <br>\n",
|
| 429 |
+
"Ad esempio, qual è il pianeta più vicino al sole?"
|
| 430 |
+
]
|
| 431 |
+
},
|
| 432 |
+
{
|
| 433 |
+
"cell_type": "code",
|
| 434 |
+
"execution_count": 15,
|
| 435 |
+
"metadata": {},
|
| 436 |
+
"outputs": [
|
| 437 |
+
{
|
| 438 |
+
"data": {
|
| 439 |
+
"text/plain": [
|
| 440 |
+
"'Mercurio'"
|
| 441 |
+
]
|
| 442 |
+
},
|
| 443 |
+
"execution_count": 15,
|
| 444 |
+
"metadata": {},
|
| 445 |
+
"output_type": "execute_result"
|
| 446 |
+
}
|
| 447 |
+
],
|
| 448 |
+
"source": [
|
| 449 |
+
"Pianeti[0]"
|
| 450 |
+
]
|
| 451 |
+
},
|
| 452 |
+
{
|
| 453 |
+
"cell_type": "markdown",
|
| 454 |
+
"metadata": {},
|
| 455 |
+
"source": [
|
| 456 |
+
"Qual è il pianeta più lontano dal sole? <br>\n",
|
| 457 |
+
"*Gli elementi alla fine di una lista possono essere identificati attraverso i numeri negativi, partendo da -1.*"
|
| 458 |
+
]
|
| 459 |
+
},
|
| 460 |
+
{
|
| 461 |
+
"cell_type": "code",
|
| 462 |
+
"execution_count": 16,
|
| 463 |
+
"metadata": {},
|
| 464 |
+
"outputs": [
|
| 465 |
+
{
|
| 466 |
+
"data": {
|
| 467 |
+
"text/plain": [
|
| 468 |
+
"'Nettuno'"
|
| 469 |
+
]
|
| 470 |
+
},
|
| 471 |
+
"execution_count": 16,
|
| 472 |
+
"metadata": {},
|
| 473 |
+
"output_type": "execute_result"
|
| 474 |
+
}
|
| 475 |
+
],
|
| 476 |
+
"source": [
|
| 477 |
+
"Pianeti[-1]"
|
| 478 |
+
]
|
| 479 |
+
},
|
| 480 |
+
{
|
| 481 |
+
"cell_type": "code",
|
| 482 |
+
"execution_count": 17,
|
| 483 |
+
"metadata": {},
|
| 484 |
+
"outputs": [
|
| 485 |
+
{
|
| 486 |
+
"data": {
|
| 487 |
+
"text/plain": [
|
| 488 |
+
"'Urano'"
|
| 489 |
+
]
|
| 490 |
+
},
|
| 491 |
+
"execution_count": 17,
|
| 492 |
+
"metadata": {},
|
| 493 |
+
"output_type": "execute_result"
|
| 494 |
+
}
|
| 495 |
+
],
|
| 496 |
+
"source": [
|
| 497 |
+
"Pianeti[-2]"
|
| 498 |
+
]
|
| 499 |
+
},
|
| 500 |
+
{
|
| 501 |
+
"cell_type": "markdown",
|
| 502 |
+
"metadata": {},
|
| 503 |
+
"source": [
|
| 504 |
+
"Quali sono i primi tre pianeti più vicini al sole? <br>\n",
|
| 505 |
+
"*Rispondiamo a questa domanda utilizzando lo **slicing***"
|
| 506 |
+
]
|
| 507 |
+
},
|
| 508 |
+
{
|
| 509 |
+
"cell_type": "code",
|
| 510 |
+
"execution_count": 18,
|
| 511 |
+
"metadata": {},
|
| 512 |
+
"outputs": [
|
| 513 |
+
{
|
| 514 |
+
"data": {
|
| 515 |
+
"text/plain": [
|
| 516 |
+
"['Mercurio', 'Venere', 'Terra']"
|
| 517 |
+
]
|
| 518 |
+
},
|
| 519 |
+
"execution_count": 18,
|
| 520 |
+
"metadata": {},
|
| 521 |
+
"output_type": "execute_result"
|
| 522 |
+
}
|
| 523 |
+
],
|
| 524 |
+
"source": [
|
| 525 |
+
"Pianeti[0:3]"
|
| 526 |
+
]
|
| 527 |
+
},
|
| 528 |
+
{
|
| 529 |
+
"cell_type": "markdown",
|
| 530 |
+
"metadata": {},
|
| 531 |
+
"source": [
|
| 532 |
+
"La notazione vista sopra \"[0:3]\" ci dice di partire da 0 e continuare fino all'indice **3, escluso**."
|
| 533 |
+
]
|
| 534 |
+
},
|
| 535 |
+
{
|
| 536 |
+
"cell_type": "markdown",
|
| 537 |
+
"metadata": {},
|
| 538 |
+
"source": [
|
| 539 |
+
"Non è necessario indicare l'inizio e la fine dell'indicizzazione qualora si volesse partire/finire con il primo/ultimo elemento di una lista."
|
| 540 |
+
]
|
| 541 |
+
},
|
| 542 |
+
{
|
| 543 |
+
"cell_type": "code",
|
| 544 |
+
"execution_count": 19,
|
| 545 |
+
"metadata": {},
|
| 546 |
+
"outputs": [
|
| 547 |
+
{
|
| 548 |
+
"data": {
|
| 549 |
+
"text/plain": [
|
| 550 |
+
"['Mercurio', 'Venere', 'Terra']"
|
| 551 |
+
]
|
| 552 |
+
},
|
| 553 |
+
"execution_count": 19,
|
| 554 |
+
"metadata": {},
|
| 555 |
+
"output_type": "execute_result"
|
| 556 |
+
}
|
| 557 |
+
],
|
| 558 |
+
"source": [
|
| 559 |
+
"Pianeti[:3]"
|
| 560 |
+
]
|
| 561 |
+
},
|
| 562 |
+
{
|
| 563 |
+
"cell_type": "code",
|
| 564 |
+
"execution_count": 20,
|
| 565 |
+
"metadata": {},
|
| 566 |
+
"outputs": [
|
| 567 |
+
{
|
| 568 |
+
"data": {
|
| 569 |
+
"text/plain": [
|
| 570 |
+
"['Marte', 'Giove', 'Saturno', 'Urano', 'Nettuno']"
|
| 571 |
+
]
|
| 572 |
+
},
|
| 573 |
+
"execution_count": 20,
|
| 574 |
+
"metadata": {},
|
| 575 |
+
"output_type": "execute_result"
|
| 576 |
+
}
|
| 577 |
+
],
|
| 578 |
+
"source": [
|
| 579 |
+
"Pianeti[3:] # Dal terzo pianeta in poi"
|
| 580 |
+
]
|
| 581 |
+
},
|
| 582 |
+
{
|
| 583 |
+
"cell_type": "code",
|
| 584 |
+
"execution_count": 21,
|
| 585 |
+
"metadata": {},
|
| 586 |
+
"outputs": [
|
| 587 |
+
{
|
| 588 |
+
"data": {
|
| 589 |
+
"text/plain": [
|
| 590 |
+
"['Saturno', 'Urano', 'Nettuno']"
|
| 591 |
+
]
|
| 592 |
+
},
|
| 593 |
+
"execution_count": 21,
|
| 594 |
+
"metadata": {},
|
| 595 |
+
"output_type": "execute_result"
|
| 596 |
+
}
|
| 597 |
+
],
|
| 598 |
+
"source": [
|
| 599 |
+
"Pianeti[-3:] # Gli ultimi 3 pianeti"
|
| 600 |
+
]
|
| 601 |
+
},
|
| 602 |
+
{
|
| 603 |
+
"cell_type": "code",
|
| 604 |
+
"execution_count": 22,
|
| 605 |
+
"metadata": {},
|
| 606 |
+
"outputs": [
|
| 607 |
+
{
|
| 608 |
+
"data": {
|
| 609 |
+
"text/plain": [
|
| 610 |
+
"['Mercurio',\n",
|
| 611 |
+
" 'Venere',\n",
|
| 612 |
+
" 'Terra',\n",
|
| 613 |
+
" 'Pianeta X',\n",
|
| 614 |
+
" 'Giove',\n",
|
| 615 |
+
" 'Saturno',\n",
|
| 616 |
+
" 'Urano',\n",
|
| 617 |
+
" 'Nettuno']"
|
| 618 |
+
]
|
| 619 |
+
},
|
| 620 |
+
"execution_count": 22,
|
| 621 |
+
"metadata": {},
|
| 622 |
+
"output_type": "execute_result"
|
| 623 |
+
}
|
| 624 |
+
],
|
| 625 |
+
"source": [
|
| 626 |
+
"Pianeti[3] = \"Pianeta X\"\n",
|
| 627 |
+
"Pianeti"
|
| 628 |
+
]
|
| 629 |
+
},
|
| 630 |
+
{
|
| 631 |
+
"cell_type": "code",
|
| 632 |
+
"execution_count": 23,
|
| 633 |
+
"metadata": {},
|
| 634 |
+
"outputs": [
|
| 635 |
+
{
|
| 636 |
+
"data": {
|
| 637 |
+
"text/plain": [
|
| 638 |
+
"['A', 'B', 'C', 'Pianeta X', 'Giove', 'Saturno', 'Urano', 'Nettuno']"
|
| 639 |
+
]
|
| 640 |
+
},
|
| 641 |
+
"execution_count": 23,
|
| 642 |
+
"metadata": {},
|
| 643 |
+
"output_type": "execute_result"
|
| 644 |
+
}
|
| 645 |
+
],
|
| 646 |
+
"source": [
|
| 647 |
+
"Pianeti[:3] = ['A', 'B', 'C']\n",
|
| 648 |
+
"Pianeti"
|
| 649 |
+
]
|
| 650 |
+
},
|
| 651 |
+
{
|
| 652 |
+
"cell_type": "markdown",
|
| 653 |
+
"metadata": {},
|
| 654 |
+
"source": [
|
| 655 |
+
"Python ha differenti funzioni che possono essere usate con le liste: <br>\n",
|
| 656 |
+
"- **len**: permette di calcolare la lunghezza di una lista; <br>\n",
|
| 657 |
+
"- **sorted**: dà come risultato la lista ordinata; <br>\n",
|
| 658 |
+
"- **sum**: somma gli elementi di una lista."
|
| 659 |
+
]
|
| 660 |
+
},
|
| 661 |
+
{
|
| 662 |
+
"cell_type": "code",
|
| 663 |
+
"execution_count": 24,
|
| 664 |
+
"metadata": {},
|
| 665 |
+
"outputs": [
|
| 666 |
+
{
|
| 667 |
+
"data": {
|
| 668 |
+
"text/plain": [
|
| 669 |
+
"8"
|
| 670 |
+
]
|
| 671 |
+
},
|
| 672 |
+
"execution_count": 24,
|
| 673 |
+
"metadata": {},
|
| 674 |
+
"output_type": "execute_result"
|
| 675 |
+
}
|
| 676 |
+
],
|
| 677 |
+
"source": [
|
| 678 |
+
"len(Pianeti)"
|
| 679 |
+
]
|
| 680 |
+
},
|
| 681 |
+
{
|
| 682 |
+
"cell_type": "code",
|
| 683 |
+
"execution_count": 25,
|
| 684 |
+
"metadata": {},
|
| 685 |
+
"outputs": [
|
| 686 |
+
{
|
| 687 |
+
"data": {
|
| 688 |
+
"text/plain": [
|
| 689 |
+
"['Giove',\n",
|
| 690 |
+
" 'Marte',\n",
|
| 691 |
+
" 'Mercurio',\n",
|
| 692 |
+
" 'Nettuno',\n",
|
| 693 |
+
" 'Saturno',\n",
|
| 694 |
+
" 'Terra',\n",
|
| 695 |
+
" 'Urano',\n",
|
| 696 |
+
" 'Venere']"
|
| 697 |
+
]
|
| 698 |
+
},
|
| 699 |
+
"execution_count": 25,
|
| 700 |
+
"metadata": {},
|
| 701 |
+
"output_type": "execute_result"
|
| 702 |
+
}
|
| 703 |
+
],
|
| 704 |
+
"source": [
|
| 705 |
+
"Pianeti = ['Mercurio', 'Venere', 'Terra', 'Marte',\\\n",
|
| 706 |
+
" 'Giove', 'Saturno', 'Urano', 'Nettuno']\n",
|
| 707 |
+
"sorted(Pianeti)"
|
| 708 |
+
]
|
| 709 |
+
},
|
| 710 |
+
{
|
| 711 |
+
"cell_type": "code",
|
| 712 |
+
"execution_count": 26,
|
| 713 |
+
"metadata": {},
|
| 714 |
+
"outputs": [
|
| 715 |
+
{
|
| 716 |
+
"data": {
|
| 717 |
+
"text/plain": [
|
| 718 |
+
"18"
|
| 719 |
+
]
|
| 720 |
+
},
|
| 721 |
+
"execution_count": 26,
|
| 722 |
+
"metadata": {},
|
| 723 |
+
"output_type": "execute_result"
|
| 724 |
+
}
|
| 725 |
+
],
|
| 726 |
+
"source": [
|
| 727 |
+
"sum(Numeri_primi)"
|
| 728 |
+
]
|
| 729 |
+
},
|
| 730 |
+
{
|
| 731 |
+
"cell_type": "markdown",
|
| 732 |
+
"metadata": {},
|
| 733 |
+
"source": [
|
| 734 |
+
"Gli oggetti in Python portano con se degli elementi: <br>\n",
|
| 735 |
+
"- I **metodi**: funzioni che possono essere eseguite partendo da un oggetto;<br>\n",
|
| 736 |
+
"- Gli **attributi**: elementi che sono collegati ad un oggetto ma non sono funzioni."
|
| 737 |
+
]
|
| 738 |
+
},
|
| 739 |
+
{
|
| 740 |
+
"cell_type": "markdown",
|
| 741 |
+
"metadata": {},
|
| 742 |
+
"source": [
|
| 743 |
+
"Un esempio di **metodo** può essere **bit_length**; ovvero un metodo che è associato ai numeri e indica i bit usati da un numero:"
|
| 744 |
+
]
|
| 745 |
+
},
|
| 746 |
+
{
|
| 747 |
+
"cell_type": "code",
|
| 748 |
+
"execution_count": 27,
|
| 749 |
+
"metadata": {},
|
| 750 |
+
"outputs": [
|
| 751 |
+
{
|
| 752 |
+
"data": {
|
| 753 |
+
"text/plain": [
|
| 754 |
+
"4"
|
| 755 |
+
]
|
| 756 |
+
},
|
| 757 |
+
"execution_count": 27,
|
| 758 |
+
"metadata": {},
|
| 759 |
+
"output_type": "execute_result"
|
| 760 |
+
}
|
| 761 |
+
],
|
| 762 |
+
"source": [
|
| 763 |
+
"x = 12\n",
|
| 764 |
+
"x.bit_length()"
|
| 765 |
+
]
|
| 766 |
+
},
|
| 767 |
+
{
|
| 768 |
+
"cell_type": "markdown",
|
| 769 |
+
"metadata": {},
|
| 770 |
+
"source": [
|
| 771 |
+
"Possiamo usare l'help di Python anche per capire cosa fa un metodo di un oggetto di Python."
|
| 772 |
+
]
|
| 773 |
+
},
|
| 774 |
+
{
|
| 775 |
+
"cell_type": "code",
|
| 776 |
+
"execution_count": 28,
|
| 777 |
+
"metadata": {
|
| 778 |
+
"tags": []
|
| 779 |
+
},
|
| 780 |
+
"outputs": [
|
| 781 |
+
{
|
| 782 |
+
"name": "stdout",
|
| 783 |
+
"output_type": "stream",
|
| 784 |
+
"text": [
|
| 785 |
+
"Help on built-in function bit_length:\n",
|
| 786 |
+
"\n",
|
| 787 |
+
"bit_length() method of builtins.int instance\n",
|
| 788 |
+
" Number of bits necessary to represent self in binary.\n",
|
| 789 |
+
" \n",
|
| 790 |
+
" >>> bin(37)\n",
|
| 791 |
+
" '0b100101'\n",
|
| 792 |
+
" >>> (37).bit_length()\n",
|
| 793 |
+
" 6\n",
|
| 794 |
+
"\n"
|
| 795 |
+
]
|
| 796 |
+
}
|
| 797 |
+
],
|
| 798 |
+
"source": [
|
| 799 |
+
"help(x.bit_length)"
|
| 800 |
+
]
|
| 801 |
+
},
|
| 802 |
+
{
|
| 803 |
+
"cell_type": "markdown",
|
| 804 |
+
"metadata": {},
|
| 805 |
+
"source": [
|
| 806 |
+
"I **metodi ** più utilizzati quando si usano le liste di Python, sono i seguenti: <br>\n",
|
| 807 |
+
"- **.append** : permette di modificare una lista aggiungendo un elemento in fondo alla lista; <br>\n",
|
| 808 |
+
"- **.pop** : rimuove e stampa l'ultimo elemento di una lista; <br>\n",
|
| 809 |
+
"- **.index** : Indica l'indice in cui si trova un determinato elemento all'interno della lista. <br>\n",
|
| 810 |
+
"Di seguito ci sono un pò di esempi. <br>\n",
|
| 811 |
+
"<br>\n",
|
| 812 |
+
"Per osservare tutti i metodi associati ad un oggetto è possibile fare: **help(*nome_oggetto*)**"
|
| 813 |
+
]
|
| 814 |
+
},
|
| 815 |
+
{
|
| 816 |
+
"cell_type": "code",
|
| 817 |
+
"execution_count": 29,
|
| 818 |
+
"metadata": {},
|
| 819 |
+
"outputs": [],
|
| 820 |
+
"source": [
|
| 821 |
+
"Pianeti.append('Plutone')"
|
| 822 |
+
]
|
| 823 |
+
},
|
| 824 |
+
{
|
| 825 |
+
"cell_type": "code",
|
| 826 |
+
"execution_count": 30,
|
| 827 |
+
"metadata": {},
|
| 828 |
+
"outputs": [
|
| 829 |
+
{
|
| 830 |
+
"data": {
|
| 831 |
+
"text/plain": [
|
| 832 |
+
"['Mercurio',\n",
|
| 833 |
+
" 'Venere',\n",
|
| 834 |
+
" 'Terra',\n",
|
| 835 |
+
" 'Marte',\n",
|
| 836 |
+
" 'Giove',\n",
|
| 837 |
+
" 'Saturno',\n",
|
| 838 |
+
" 'Urano',\n",
|
| 839 |
+
" 'Nettuno',\n",
|
| 840 |
+
" 'Plutone']"
|
| 841 |
+
]
|
| 842 |
+
},
|
| 843 |
+
"execution_count": 30,
|
| 844 |
+
"metadata": {},
|
| 845 |
+
"output_type": "execute_result"
|
| 846 |
+
}
|
| 847 |
+
],
|
| 848 |
+
"source": [
|
| 849 |
+
"Pianeti"
|
| 850 |
+
]
|
| 851 |
+
},
|
| 852 |
+
{
|
| 853 |
+
"cell_type": "markdown",
|
| 854 |
+
"metadata": {},
|
| 855 |
+
"source": [
|
| 856 |
+
"<a id='section5'></a>\n",
|
| 857 |
+
"## 5) Loops"
|
| 858 |
+
]
|
| 859 |
+
},
|
| 860 |
+
{
|
| 861 |
+
"cell_type": "code",
|
| 862 |
+
"execution_count": 31,
|
| 863 |
+
"metadata": {},
|
| 864 |
+
"outputs": [
|
| 865 |
+
{
|
| 866 |
+
"data": {
|
| 867 |
+
"text/plain": [
|
| 868 |
+
"['Mercurio',\n",
|
| 869 |
+
" 'Venere',\n",
|
| 870 |
+
" 'Terra',\n",
|
| 871 |
+
" 'Marte',\n",
|
| 872 |
+
" 'Giove',\n",
|
| 873 |
+
" 'Saturno',\n",
|
| 874 |
+
" 'Urano',\n",
|
| 875 |
+
" 'Nettuno',\n",
|
| 876 |
+
" 'Plutone']"
|
| 877 |
+
]
|
| 878 |
+
},
|
| 879 |
+
"execution_count": 31,
|
| 880 |
+
"metadata": {},
|
| 881 |
+
"output_type": "execute_result"
|
| 882 |
+
}
|
| 883 |
+
],
|
| 884 |
+
"source": [
|
| 885 |
+
"Pianeti"
|
| 886 |
+
]
|
| 887 |
+
},
|
| 888 |
+
{
|
| 889 |
+
"cell_type": "code",
|
| 890 |
+
"execution_count": 32,
|
| 891 |
+
"metadata": {
|
| 892 |
+
"tags": []
|
| 893 |
+
},
|
| 894 |
+
"outputs": [
|
| 895 |
+
{
|
| 896 |
+
"name": "stdout",
|
| 897 |
+
"output_type": "stream",
|
| 898 |
+
"text": [
|
| 899 |
+
"Mercurio Venere Terra Marte Giove Saturno Urano Nettuno Plutone "
|
| 900 |
+
]
|
| 901 |
+
}
|
| 902 |
+
],
|
| 903 |
+
"source": [
|
| 904 |
+
"# Stampo tutti i pianeti sulla stessa linea\n",
|
| 905 |
+
"for i in Pianeti:\n",
|
| 906 |
+
" print(i, end=' ') \n"
|
| 907 |
+
]
|
| 908 |
+
},
|
| 909 |
+
{
|
| 910 |
+
"cell_type": "markdown",
|
| 911 |
+
"metadata": {},
|
| 912 |
+
"source": [
|
| 913 |
+
"<a id='section6'></a>\n",
|
| 914 |
+
"## 6) I dizionari"
|
| 915 |
+
]
|
| 916 |
+
},
|
| 917 |
+
{
|
| 918 |
+
"cell_type": "markdown",
|
| 919 |
+
"metadata": {},
|
| 920 |
+
"source": [
|
| 921 |
+
"I dizionari sono delle strutture pre impostate di Python che permettono di mappare dei valori su delle chiavi. Ad esempio:"
|
| 922 |
+
]
|
| 923 |
+
},
|
| 924 |
+
{
|
| 925 |
+
"cell_type": "code",
|
| 926 |
+
"execution_count": 33,
|
| 927 |
+
"metadata": {},
|
| 928 |
+
"outputs": [],
|
| 929 |
+
"source": [
|
| 930 |
+
"numeri = {'uno': 1, 'due': 2, 'tre': 3}"
|
| 931 |
+
]
|
| 932 |
+
},
|
| 933 |
+
{
|
| 934 |
+
"cell_type": "markdown",
|
| 935 |
+
"metadata": {},
|
| 936 |
+
"source": [
|
| 937 |
+
"In questo caso 'uno', 'due' e 'tre' sono le **chiavi**, mentre 1, 2 e 3 sono i loro corrispondenti **valori**. <br>\n",
|
| 938 |
+
"È possibile accedere ai valori attraverso l'utilizzo delle parentesi quadre come si fa con le liste e con le stringhe."
|
| 939 |
+
]
|
| 940 |
+
},
|
| 941 |
+
{
|
| 942 |
+
"cell_type": "code",
|
| 943 |
+
"execution_count": 34,
|
| 944 |
+
"metadata": {},
|
| 945 |
+
"outputs": [
|
| 946 |
+
{
|
| 947 |
+
"data": {
|
| 948 |
+
"text/plain": [
|
| 949 |
+
"1"
|
| 950 |
+
]
|
| 951 |
+
},
|
| 952 |
+
"execution_count": 34,
|
| 953 |
+
"metadata": {},
|
| 954 |
+
"output_type": "execute_result"
|
| 955 |
+
}
|
| 956 |
+
],
|
| 957 |
+
"source": [
|
| 958 |
+
"numeri['uno']"
|
| 959 |
+
]
|
| 960 |
+
},
|
| 961 |
+
{
|
| 962 |
+
"cell_type": "markdown",
|
| 963 |
+
"metadata": {},
|
| 964 |
+
"source": [
|
| 965 |
+
"È possibile aggiungere dei nuovi valori al dizionario, identificando semplicemente una nuova chiave, ad esempio:"
|
| 966 |
+
]
|
| 967 |
+
},
|
| 968 |
+
{
|
| 969 |
+
"cell_type": "code",
|
| 970 |
+
"execution_count": 35,
|
| 971 |
+
"metadata": {},
|
| 972 |
+
"outputs": [
|
| 973 |
+
{
|
| 974 |
+
"data": {
|
| 975 |
+
"text/plain": [
|
| 976 |
+
"{'uno': 1, 'due': 2, 'tre': 3, 'quattro': 4}"
|
| 977 |
+
]
|
| 978 |
+
},
|
| 979 |
+
"execution_count": 35,
|
| 980 |
+
"metadata": {},
|
| 981 |
+
"output_type": "execute_result"
|
| 982 |
+
}
|
| 983 |
+
],
|
| 984 |
+
"source": [
|
| 985 |
+
"numeri['quattro'] = 4\n",
|
| 986 |
+
"numeri"
|
| 987 |
+
]
|
| 988 |
+
},
|
| 989 |
+
{
|
| 990 |
+
"cell_type": "markdown",
|
| 991 |
+
"metadata": {},
|
| 992 |
+
"source": [
|
| 993 |
+
"È possibile anche cambiare un valore associato ad una chiave già esistente:"
|
| 994 |
+
]
|
| 995 |
+
},
|
| 996 |
+
{
|
| 997 |
+
"cell_type": "code",
|
| 998 |
+
"execution_count": 36,
|
| 999 |
+
"metadata": {},
|
| 1000 |
+
"outputs": [
|
| 1001 |
+
{
|
| 1002 |
+
"data": {
|
| 1003 |
+
"text/plain": [
|
| 1004 |
+
"{'uno': 0, 'due': 2, 'tre': 3, 'quattro': 4}"
|
| 1005 |
+
]
|
| 1006 |
+
},
|
| 1007 |
+
"execution_count": 36,
|
| 1008 |
+
"metadata": {},
|
| 1009 |
+
"output_type": "execute_result"
|
| 1010 |
+
}
|
| 1011 |
+
],
|
| 1012 |
+
"source": [
|
| 1013 |
+
"numeri['uno'] = 0\n",
|
| 1014 |
+
"numeri"
|
| 1015 |
+
]
|
| 1016 |
+
},
|
| 1017 |
+
{
|
| 1018 |
+
"cell_type": "markdown",
|
| 1019 |
+
"metadata": {},
|
| 1020 |
+
"source": [
|
| 1021 |
+
"La sintassi usata per i dizionari è molto simile a quella vista per le liste."
|
| 1022 |
+
]
|
| 1023 |
+
},
|
| 1024 |
+
{
|
| 1025 |
+
"cell_type": "code",
|
| 1026 |
+
"execution_count": 37,
|
| 1027 |
+
"metadata": {},
|
| 1028 |
+
"outputs": [
|
| 1029 |
+
{
|
| 1030 |
+
"data": {
|
| 1031 |
+
"text/plain": [
|
| 1032 |
+
"{'Mercurio': 'M',\n",
|
| 1033 |
+
" 'Venere': 'V',\n",
|
| 1034 |
+
" 'Terra': 'T',\n",
|
| 1035 |
+
" 'Marte': 'M',\n",
|
| 1036 |
+
" 'Giove': 'G',\n",
|
| 1037 |
+
" 'Saturno': 'S',\n",
|
| 1038 |
+
" 'Urano': 'U',\n",
|
| 1039 |
+
" 'Nettuno': 'N',\n",
|
| 1040 |
+
" 'Plutone': 'P'}"
|
| 1041 |
+
]
|
| 1042 |
+
},
|
| 1043 |
+
"execution_count": 37,
|
| 1044 |
+
"metadata": {},
|
| 1045 |
+
"output_type": "execute_result"
|
| 1046 |
+
}
|
| 1047 |
+
],
|
| 1048 |
+
"source": [
|
| 1049 |
+
"pianeta_iniziale = {pianeta: pianeta[0] for pianeta in Pianeti}\n",
|
| 1050 |
+
"pianeta_iniziale"
|
| 1051 |
+
]
|
| 1052 |
+
},
|
| 1053 |
+
{
|
| 1054 |
+
"cell_type": "markdown",
|
| 1055 |
+
"metadata": {},
|
| 1056 |
+
"source": [
|
| 1057 |
+
"L'operatore **in** può essere usato per capire se un elemento si trova dentro un dizionario."
|
| 1058 |
+
]
|
| 1059 |
+
},
|
| 1060 |
+
{
|
| 1061 |
+
"cell_type": "code",
|
| 1062 |
+
"execution_count": 38,
|
| 1063 |
+
"metadata": {},
|
| 1064 |
+
"outputs": [
|
| 1065 |
+
{
|
| 1066 |
+
"data": {
|
| 1067 |
+
"text/plain": [
|
| 1068 |
+
"True"
|
| 1069 |
+
]
|
| 1070 |
+
},
|
| 1071 |
+
"execution_count": 38,
|
| 1072 |
+
"metadata": {},
|
| 1073 |
+
"output_type": "execute_result"
|
| 1074 |
+
}
|
| 1075 |
+
],
|
| 1076 |
+
"source": [
|
| 1077 |
+
"'Saturno' in Pianeti"
|
| 1078 |
+
]
|
| 1079 |
+
},
|
| 1080 |
+
{
|
| 1081 |
+
"cell_type": "code",
|
| 1082 |
+
"execution_count": 39,
|
| 1083 |
+
"metadata": {},
|
| 1084 |
+
"outputs": [
|
| 1085 |
+
{
|
| 1086 |
+
"data": {
|
| 1087 |
+
"text/plain": [
|
| 1088 |
+
"False"
|
| 1089 |
+
]
|
| 1090 |
+
},
|
| 1091 |
+
"execution_count": 39,
|
| 1092 |
+
"metadata": {},
|
| 1093 |
+
"output_type": "execute_result"
|
| 1094 |
+
}
|
| 1095 |
+
],
|
| 1096 |
+
"source": [
|
| 1097 |
+
"'Pianeta X' in Pianeti"
|
| 1098 |
+
]
|
| 1099 |
+
},
|
| 1100 |
+
{
|
| 1101 |
+
"cell_type": "markdown",
|
| 1102 |
+
"metadata": {},
|
| 1103 |
+
"source": [
|
| 1104 |
+
"Un loop for su un dizionario effettua il loop sulle chiavi del dizionario, ad esempio:"
|
| 1105 |
+
]
|
| 1106 |
+
},
|
| 1107 |
+
{
|
| 1108 |
+
"cell_type": "code",
|
| 1109 |
+
"execution_count": 40,
|
| 1110 |
+
"metadata": {
|
| 1111 |
+
"tags": []
|
| 1112 |
+
},
|
| 1113 |
+
"outputs": [
|
| 1114 |
+
{
|
| 1115 |
+
"name": "stdout",
|
| 1116 |
+
"output_type": "stream",
|
| 1117 |
+
"text": [
|
| 1118 |
+
"uno = 0\n",
|
| 1119 |
+
"due = 2\n",
|
| 1120 |
+
"tre = 3\n",
|
| 1121 |
+
"quattro = 4\n"
|
| 1122 |
+
]
|
| 1123 |
+
}
|
| 1124 |
+
],
|
| 1125 |
+
"source": [
|
| 1126 |
+
"for k in numeri:\n",
|
| 1127 |
+
" print(\"{} = {}\".format(k, numeri[k]))"
|
| 1128 |
+
]
|
| 1129 |
+
},
|
| 1130 |
+
{
|
| 1131 |
+
"cell_type": "markdown",
|
| 1132 |
+
"metadata": {},
|
| 1133 |
+
"source": [
|
| 1134 |
+
"È possibile accedere direttamente a tutte le chiavi o a tutti i valori di un dizionario attraverso i seguenti metodi dell'oggetto dizionario **dict.keys()** e **dict.values()**."
|
| 1135 |
+
]
|
| 1136 |
+
},
|
| 1137 |
+
{
|
| 1138 |
+
"cell_type": "code",
|
| 1139 |
+
"execution_count": 41,
|
| 1140 |
+
"metadata": {},
|
| 1141 |
+
"outputs": [
|
| 1142 |
+
{
|
| 1143 |
+
"data": {
|
| 1144 |
+
"text/plain": [
|
| 1145 |
+
"dict_keys(['uno', 'due', 'tre', 'quattro'])"
|
| 1146 |
+
]
|
| 1147 |
+
},
|
| 1148 |
+
"execution_count": 41,
|
| 1149 |
+
"metadata": {},
|
| 1150 |
+
"output_type": "execute_result"
|
| 1151 |
+
}
|
| 1152 |
+
],
|
| 1153 |
+
"source": [
|
| 1154 |
+
"numeri.keys()"
|
| 1155 |
+
]
|
| 1156 |
+
},
|
| 1157 |
+
{
|
| 1158 |
+
"cell_type": "code",
|
| 1159 |
+
"execution_count": 42,
|
| 1160 |
+
"metadata": {},
|
| 1161 |
+
"outputs": [
|
| 1162 |
+
{
|
| 1163 |
+
"data": {
|
| 1164 |
+
"text/plain": [
|
| 1165 |
+
"dict_values([0, 2, 3, 4])"
|
| 1166 |
+
]
|
| 1167 |
+
},
|
| 1168 |
+
"execution_count": 42,
|
| 1169 |
+
"metadata": {},
|
| 1170 |
+
"output_type": "execute_result"
|
| 1171 |
+
}
|
| 1172 |
+
],
|
| 1173 |
+
"source": [
|
| 1174 |
+
"numeri.values()"
|
| 1175 |
+
]
|
| 1176 |
+
},
|
| 1177 |
+
{
|
| 1178 |
+
"cell_type": "markdown",
|
| 1179 |
+
"metadata": {},
|
| 1180 |
+
"source": [
|
| 1181 |
+
"Uno dei metodi più utili quando si usano i dizionari è **dict.items()**, questo metodo ci permette di iteerare le chiavi e i valori di un dizionario simultaneamente."
|
| 1182 |
+
]
|
| 1183 |
+
},
|
| 1184 |
+
{
|
| 1185 |
+
"cell_type": "code",
|
| 1186 |
+
"execution_count": 43,
|
| 1187 |
+
"metadata": {
|
| 1188 |
+
"tags": []
|
| 1189 |
+
},
|
| 1190 |
+
"outputs": [
|
| 1191 |
+
{
|
| 1192 |
+
"name": "stdout",
|
| 1193 |
+
"output_type": "stream",
|
| 1194 |
+
"text": [
|
| 1195 |
+
" Mercurio inizia con 'M'\n",
|
| 1196 |
+
" Venere inizia con 'V'\n",
|
| 1197 |
+
" Terra inizia con 'T'\n",
|
| 1198 |
+
" Marte inizia con 'M'\n",
|
| 1199 |
+
" Giove inizia con 'G'\n",
|
| 1200 |
+
" Saturno inizia con 'S'\n",
|
| 1201 |
+
" Urano inizia con 'U'\n",
|
| 1202 |
+
" Nettuno inizia con 'N'\n",
|
| 1203 |
+
" Plutone inizia con 'P'\n"
|
| 1204 |
+
]
|
| 1205 |
+
}
|
| 1206 |
+
],
|
| 1207 |
+
"source": [
|
| 1208 |
+
"for pianeta, iniziale_pianeta in pianeta_iniziale.items():\n",
|
| 1209 |
+
" print(\"{} inizia con '{}'\".format(pianeta.rjust(10), iniziale_pianeta))"
|
| 1210 |
+
]
|
| 1211 |
+
},
|
| 1212 |
+
{
|
| 1213 |
+
"cell_type": "markdown",
|
| 1214 |
+
"metadata": {},
|
| 1215 |
+
"source": [
|
| 1216 |
+
"<a id='section7'></a>\n",
|
| 1217 |
+
"## 7) Le librerie esterne"
|
| 1218 |
+
]
|
| 1219 |
+
},
|
| 1220 |
+
{
|
| 1221 |
+
"cell_type": "markdown",
|
| 1222 |
+
"metadata": {},
|
| 1223 |
+
"source": [
|
| 1224 |
+
"Una delle qualità principali di Python è l'elevato numero di librerie personalizzate che sono state scritte per questo linguaggio di programmazione. Alcune di queste librerie sono *standard*, ovvero possono essere trovate in qualsiasi Python; tuttavia le librerie che non sono comprese di default in Python possono essere facilmente richiamate attraverso la parola chiave **import**. <br>\n",
|
| 1225 |
+
"Importiamo la libreria *math* così come abbiamo fatto nel primo script di questo notebook."
|
| 1226 |
+
]
|
| 1227 |
+
},
|
| 1228 |
+
{
|
| 1229 |
+
"cell_type": "code",
|
| 1230 |
+
"execution_count": 44,
|
| 1231 |
+
"metadata": {
|
| 1232 |
+
"tags": []
|
| 1233 |
+
},
|
| 1234 |
+
"outputs": [
|
| 1235 |
+
{
|
| 1236 |
+
"name": "stdout",
|
| 1237 |
+
"output_type": "stream",
|
| 1238 |
+
"text": [
|
| 1239 |
+
"Math è di questo tipo: <class 'module'>\n"
|
| 1240 |
+
]
|
| 1241 |
+
}
|
| 1242 |
+
],
|
| 1243 |
+
"source": [
|
| 1244 |
+
"import math\n",
|
| 1245 |
+
"\n",
|
| 1246 |
+
"print(\"Math è di questo tipo: {}\".format(type(math)))"
|
| 1247 |
+
]
|
| 1248 |
+
},
|
| 1249 |
+
{
|
| 1250 |
+
"cell_type": "markdown",
|
| 1251 |
+
"metadata": {},
|
| 1252 |
+
"source": [
|
| 1253 |
+
"Math è un modulo, ovvero una collezione di variabili definite da qualcun altro. È possibile osservare tutte le variabili contenute in Math utilizzando la funzione **dir()**."
|
| 1254 |
+
]
|
| 1255 |
+
},
|
| 1256 |
+
{
|
| 1257 |
+
"cell_type": "code",
|
| 1258 |
+
"execution_count": 45,
|
| 1259 |
+
"metadata": {
|
| 1260 |
+
"tags": []
|
| 1261 |
+
},
|
| 1262 |
+
"outputs": [
|
| 1263 |
+
{
|
| 1264 |
+
"name": "stdout",
|
| 1265 |
+
"output_type": "stream",
|
| 1266 |
+
"text": [
|
| 1267 |
+
"['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp']\n"
|
| 1268 |
+
]
|
| 1269 |
+
}
|
| 1270 |
+
],
|
| 1271 |
+
"source": [
|
| 1272 |
+
"print(dir(math))"
|
| 1273 |
+
]
|
| 1274 |
+
},
|
| 1275 |
+
{
|
| 1276 |
+
"cell_type": "code",
|
| 1277 |
+
"execution_count": 46,
|
| 1278 |
+
"metadata": {
|
| 1279 |
+
"tags": []
|
| 1280 |
+
},
|
| 1281 |
+
"outputs": [
|
| 1282 |
+
{
|
| 1283 |
+
"name": "stdout",
|
| 1284 |
+
"output_type": "stream",
|
| 1285 |
+
"text": [
|
| 1286 |
+
"I primi quattro numeri del pi-quadro sono = 3.142\n"
|
| 1287 |
+
]
|
| 1288 |
+
}
|
| 1289 |
+
],
|
| 1290 |
+
"source": [
|
| 1291 |
+
"print(\"I primi quattro numeri del pi-quadro sono = {:.4}\".format(math.pi))"
|
| 1292 |
+
]
|
| 1293 |
+
},
|
| 1294 |
+
{
|
| 1295 |
+
"cell_type": "code",
|
| 1296 |
+
"execution_count": 47,
|
| 1297 |
+
"metadata": {},
|
| 1298 |
+
"outputs": [
|
| 1299 |
+
{
|
| 1300 |
+
"data": {
|
| 1301 |
+
"text/plain": [
|
| 1302 |
+
"5.0"
|
| 1303 |
+
]
|
| 1304 |
+
},
|
| 1305 |
+
"execution_count": 47,
|
| 1306 |
+
"metadata": {},
|
| 1307 |
+
"output_type": "execute_result"
|
| 1308 |
+
}
|
| 1309 |
+
],
|
| 1310 |
+
"source": [
|
| 1311 |
+
"math.log(32,2)"
|
| 1312 |
+
]
|
| 1313 |
+
},
|
| 1314 |
+
{
|
| 1315 |
+
"cell_type": "code",
|
| 1316 |
+
"execution_count": 48,
|
| 1317 |
+
"metadata": {
|
| 1318 |
+
"tags": []
|
| 1319 |
+
},
|
| 1320 |
+
"outputs": [
|
| 1321 |
+
{
|
| 1322 |
+
"name": "stdout",
|
| 1323 |
+
"output_type": "stream",
|
| 1324 |
+
"text": [
|
| 1325 |
+
"Help on built-in function log in module math:\n",
|
| 1326 |
+
"\n",
|
| 1327 |
+
"log(...)\n",
|
| 1328 |
+
" log(x, [base=math.e])\n",
|
| 1329 |
+
" Return the logarithm of x to the given base.\n",
|
| 1330 |
+
" \n",
|
| 1331 |
+
" If the base not specified, returns the natural logarithm (base e) of x.\n",
|
| 1332 |
+
"\n"
|
| 1333 |
+
]
|
| 1334 |
+
}
|
| 1335 |
+
],
|
| 1336 |
+
"source": [
|
| 1337 |
+
"help(math.log)"
|
| 1338 |
+
]
|
| 1339 |
+
},
|
| 1340 |
+
{
|
| 1341 |
+
"cell_type": "markdown",
|
| 1342 |
+
"metadata": {},
|
| 1343 |
+
"source": [
|
| 1344 |
+
"Come abbiamo accennato all'inizio di questo notebook, quando si importa una libreria è possibile assegnarle un nome abbreviato per poterlo riusare nel codice."
|
| 1345 |
+
]
|
| 1346 |
+
},
|
| 1347 |
+
{
|
| 1348 |
+
"cell_type": "code",
|
| 1349 |
+
"execution_count": 49,
|
| 1350 |
+
"metadata": {},
|
| 1351 |
+
"outputs": [
|
| 1352 |
+
{
|
| 1353 |
+
"data": {
|
| 1354 |
+
"text/plain": [
|
| 1355 |
+
"3.141592653589793"
|
| 1356 |
+
]
|
| 1357 |
+
},
|
| 1358 |
+
"execution_count": 49,
|
| 1359 |
+
"metadata": {},
|
| 1360 |
+
"output_type": "execute_result"
|
| 1361 |
+
}
|
| 1362 |
+
],
|
| 1363 |
+
"source": [
|
| 1364 |
+
"import math as mt\n",
|
| 1365 |
+
"mt.pi"
|
| 1366 |
+
]
|
| 1367 |
+
},
|
| 1368 |
+
{
|
| 1369 |
+
"cell_type": "markdown",
|
| 1370 |
+
"metadata": {},
|
| 1371 |
+
"source": [
|
| 1372 |
+
"È possibile importare anche solo una particolare variabile contenuta all'interno della libreria senza dover importarsi tutta la libreria, in questo caso potremmo usare la seguente notazione:"
|
| 1373 |
+
]
|
| 1374 |
+
},
|
| 1375 |
+
{
|
| 1376 |
+
"cell_type": "code",
|
| 1377 |
+
"execution_count": 50,
|
| 1378 |
+
"metadata": {
|
| 1379 |
+
"tags": []
|
| 1380 |
+
},
|
| 1381 |
+
"outputs": [
|
| 1382 |
+
{
|
| 1383 |
+
"name": "stdout",
|
| 1384 |
+
"output_type": "stream",
|
| 1385 |
+
"text": [
|
| 1386 |
+
"3.141592653589793\n"
|
| 1387 |
+
]
|
| 1388 |
+
}
|
| 1389 |
+
],
|
| 1390 |
+
"source": [
|
| 1391 |
+
"from math import pi\n",
|
| 1392 |
+
"print(pi)"
|
| 1393 |
+
]
|
| 1394 |
+
},
|
| 1395 |
+
{
|
| 1396 |
+
"cell_type": "code",
|
| 1397 |
+
"execution_count": 51,
|
| 1398 |
+
"metadata": {},
|
| 1399 |
+
"outputs": [
|
| 1400 |
+
{
|
| 1401 |
+
"ename": "TypeError",
|
| 1402 |
+
"evalue": "return arrays must be of ArrayType",
|
| 1403 |
+
"output_type": "error",
|
| 1404 |
+
"traceback": [
|
| 1405 |
+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
| 1406 |
+
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
| 1407 |
+
"Cell \u001b[0;32mIn[51], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmath\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mnumpy\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(pi, \u001b[43mlog\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m32\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m)\n",
|
| 1408 |
+
"\u001b[0;31mTypeError\u001b[0m: return arrays must be of ArrayType"
|
| 1409 |
+
]
|
| 1410 |
+
}
|
| 1411 |
+
],
|
| 1412 |
+
"source": [
|
| 1413 |
+
"from math import *\n",
|
| 1414 |
+
"from numpy import *\n",
|
| 1415 |
+
"print(pi, log(32,2))"
|
| 1416 |
+
]
|
| 1417 |
+
},
|
| 1418 |
+
{
|
| 1419 |
+
"cell_type": "markdown",
|
| 1420 |
+
"metadata": {},
|
| 1421 |
+
"source": [
|
| 1422 |
+
"In questo caso abbiamo riscontrato un errore poichè la variabile **log** è contenuta sia nella libreria *math* che nella *numpy*, ma ha differenti input. Poichè abbiamo importato anche la libreria *numpy* in questo caso il log di quest'ultima libreria ha sovrascritto il log della libreria math.<br>\n",
|
| 1423 |
+
"Un modo per risolvere il problema di prima è importare solamente ciò che vogliamo davvero usare, ad esempio:"
|
| 1424 |
+
]
|
| 1425 |
+
},
|
| 1426 |
+
{
|
| 1427 |
+
"cell_type": "code",
|
| 1428 |
+
"execution_count": 52,
|
| 1429 |
+
"metadata": {
|
| 1430 |
+
"tags": []
|
| 1431 |
+
},
|
| 1432 |
+
"outputs": [
|
| 1433 |
+
{
|
| 1434 |
+
"name": "stdout",
|
| 1435 |
+
"output_type": "stream",
|
| 1436 |
+
"text": [
|
| 1437 |
+
"3.141592653589793 5.0\n"
|
| 1438 |
+
]
|
| 1439 |
+
}
|
| 1440 |
+
],
|
| 1441 |
+
"source": [
|
| 1442 |
+
"from math import log, pi\n",
|
| 1443 |
+
"from numpy import asarray\n",
|
| 1444 |
+
"print(pi, log(32,2))"
|
| 1445 |
+
]
|
| 1446 |
+
},
|
| 1447 |
+
{
|
| 1448 |
+
"cell_type": "markdown",
|
| 1449 |
+
"metadata": {},
|
| 1450 |
+
"source": [
|
| 1451 |
+
"In generale, se incontriamo degli oggetti di Python che non conosciamo possiamo utilizzare tre funzioni pre-impostate di Python:<br>\n",
|
| 1452 |
+
"- **type()** : ci dice cos'è l'oggetto;\n",
|
| 1453 |
+
"- **dir()**: ci dice cosa può fare l'oggetto;\n",
|
| 1454 |
+
"- **help()**: ci dice più in dettaglio i metodi associati all'oggetto e le loro funzionalità"
|
| 1455 |
+
]
|
| 1456 |
+
},
|
| 1457 |
+
{
|
| 1458 |
+
"cell_type": "markdown",
|
| 1459 |
+
"metadata": {},
|
| 1460 |
+
"source": [
|
| 1461 |
+
"[Clicca qui per tornare all'inizio della pagina](#start)<a id='start'></a>"
|
| 1462 |
+
]
|
| 1463 |
+
}
|
| 1464 |
+
],
|
| 1465 |
+
"metadata": {
|
| 1466 |
+
"kernelspec": {
|
| 1467 |
+
"display_name": "Python 3 (ipykernel)",
|
| 1468 |
+
"language": "python",
|
| 1469 |
+
"name": "python3"
|
| 1470 |
+
},
|
| 1471 |
+
"language_info": {
|
| 1472 |
+
"codemirror_mode": {
|
| 1473 |
+
"name": "ipython",
|
| 1474 |
+
"version": 3
|
| 1475 |
+
},
|
| 1476 |
+
"file_extension": ".py",
|
| 1477 |
+
"mimetype": "text/x-python",
|
| 1478 |
+
"name": "python",
|
| 1479 |
+
"nbconvert_exporter": "python",
|
| 1480 |
+
"pygments_lexer": "ipython3",
|
| 1481 |
+
"version": "3.10.6"
|
| 1482 |
+
},
|
| 1483 |
+
"nbTranslate": {
|
| 1484 |
+
"displayLangs": [
|
| 1485 |
+
"*"
|
| 1486 |
+
],
|
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i\n",
|
| 8 |
+
"\n",
|
| 9 |
+
"#### Prendiamo dimestichezza con Python dentro al Jupyter Notebook e con le librerie per fare grafici.\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"Provate a svolgere i seguenti esercizi: <br>\n",
|
| 12 |
+
"1) [Fattoriale](#section1)<a href='#section1'></a> <br>\n",
|
| 13 |
+
"2) [Serie di Fibonacci](#section2)<a href='#section2'></a><br>\n",
|
| 14 |
+
"3) [Esempio di Plot](#section3)<a href='#section3'></a> <br>\n",
|
| 15 |
+
"4) [Esercizio di Plot di un file root](#section4)<a href='#section4'></a> <br>\n",
|
| 16 |
+
"5) [Metodo di Eulero](#section5)<a href='#section5'></a> <br>\n",
|
| 17 |
+
"6) [Fit di curve lineari](#section6)<a href='#section6'></a><br>\n",
|
| 18 |
+
"7) [Statistica descrittiva](#section7)<a href='#section7'></a>"
|
| 19 |
+
]
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
"cell_type": "markdown",
|
| 23 |
+
"metadata": {},
|
| 24 |
+
"source": [
|
| 25 |
+
"<a id='section1'></a>\n",
|
| 26 |
+
"## 1) Fattoriale"
|
| 27 |
+
]
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"cell_type": "markdown",
|
| 31 |
+
"metadata": {},
|
| 32 |
+
"source": [
|
| 33 |
+
"Scrivere in Python una funzione fatt che calcola il fattoriale di un numero dato come parametro. <br>\n",
|
| 34 |
+
"Se siete coraggiosi provate a scriverla ricorsiva!"
|
| 35 |
+
]
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"cell_type": "code",
|
| 39 |
+
"execution_count": null,
|
| 40 |
+
"metadata": {},
|
| 41 |
+
"outputs": [],
|
| 42 |
+
"source": []
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"cell_type": "markdown",
|
| 46 |
+
"metadata": {},
|
| 47 |
+
"source": [
|
| 48 |
+
"<a id='section2'></a>\n",
|
| 49 |
+
"## 2) Serie di Fibonacci"
|
| 50 |
+
]
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"cell_type": "markdown",
|
| 54 |
+
"metadata": {},
|
| 55 |
+
"source": [
|
| 56 |
+
"Scrivere una funzione Python che restituisce i primi n numeri della serie di Fibonacci."
|
| 57 |
+
]
|
| 58 |
+
},
|
| 59 |
+
{
|
| 60 |
+
"cell_type": "code",
|
| 61 |
+
"execution_count": null,
|
| 62 |
+
"metadata": {},
|
| 63 |
+
"outputs": [],
|
| 64 |
+
"source": []
|
| 65 |
+
},
|
| 66 |
+
{
|
| 67 |
+
"cell_type": "markdown",
|
| 68 |
+
"metadata": {},
|
| 69 |
+
"source": [
|
| 70 |
+
"<a id='section3'></a>\n",
|
| 71 |
+
"## 3) Esempio di plot"
|
| 72 |
+
]
|
| 73 |
+
},
|
| 74 |
+
{
|
| 75 |
+
"cell_type": "markdown",
|
| 76 |
+
"metadata": {},
|
| 77 |
+
"source": [
|
| 78 |
+
"Per fare dei grafici è possibile utilizzare la libreria `matplotlib`."
|
| 79 |
+
]
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"cell_type": "code",
|
| 83 |
+
"execution_count": 4,
|
| 84 |
+
"metadata": {},
|
| 85 |
+
"outputs": [],
|
| 86 |
+
"source": [
|
| 87 |
+
"import numpy as np\n",
|
| 88 |
+
"import matplotlib.pyplot as plt"
|
| 89 |
+
]
|
| 90 |
+
},
|
| 91 |
+
{
|
| 92 |
+
"cell_type": "code",
|
| 93 |
+
"execution_count": 5,
|
| 94 |
+
"metadata": {},
|
| 95 |
+
"outputs": [
|
| 96 |
+
{
|
| 97 |
+
"name": "stdout",
|
| 98 |
+
"output_type": "stream",
|
| 99 |
+
"text": [
|
| 100 |
+
"Help on function linspace in module numpy:\n",
|
| 101 |
+
"\n",
|
| 102 |
+
"linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)\n",
|
| 103 |
+
" Return evenly spaced numbers over a specified interval.\n",
|
| 104 |
+
" \n",
|
| 105 |
+
" Returns `num` evenly spaced samples, calculated over the\n",
|
| 106 |
+
" interval [`start`, `stop`].\n",
|
| 107 |
+
" \n",
|
| 108 |
+
" The endpoint of the interval can optionally be excluded.\n",
|
| 109 |
+
" \n",
|
| 110 |
+
" .. versionchanged:: 1.16.0\n",
|
| 111 |
+
" Non-scalar `start` and `stop` are now supported.\n",
|
| 112 |
+
" \n",
|
| 113 |
+
" Parameters\n",
|
| 114 |
+
" ----------\n",
|
| 115 |
+
" start : array_like\n",
|
| 116 |
+
" The starting value of the sequence.\n",
|
| 117 |
+
" stop : array_like\n",
|
| 118 |
+
" The end value of the sequence, unless `endpoint` is set to False.\n",
|
| 119 |
+
" In that case, the sequence consists of all but the last of ``num + 1``\n",
|
| 120 |
+
" evenly spaced samples, so that `stop` is excluded. Note that the step\n",
|
| 121 |
+
" size changes when `endpoint` is False.\n",
|
| 122 |
+
" num : int, optional\n",
|
| 123 |
+
" Number of samples to generate. Default is 50. Must be non-negative.\n",
|
| 124 |
+
" endpoint : bool, optional\n",
|
| 125 |
+
" If True, `stop` is the last sample. Otherwise, it is not included.\n",
|
| 126 |
+
" Default is True.\n",
|
| 127 |
+
" retstep : bool, optional\n",
|
| 128 |
+
" If True, return (`samples`, `step`), where `step` is the spacing\n",
|
| 129 |
+
" between samples.\n",
|
| 130 |
+
" dtype : dtype, optional\n",
|
| 131 |
+
" The type of the output array. If `dtype` is not given, infer the data\n",
|
| 132 |
+
" type from the other input arguments.\n",
|
| 133 |
+
" \n",
|
| 134 |
+
" .. versionadded:: 1.9.0\n",
|
| 135 |
+
" \n",
|
| 136 |
+
" axis : int, optional\n",
|
| 137 |
+
" The axis in the result to store the samples. Relevant only if start\n",
|
| 138 |
+
" or stop are array-like. By default (0), the samples will be along a\n",
|
| 139 |
+
" new axis inserted at the beginning. Use -1 to get an axis at the end.\n",
|
| 140 |
+
" \n",
|
| 141 |
+
" .. versionadded:: 1.16.0\n",
|
| 142 |
+
" \n",
|
| 143 |
+
" Returns\n",
|
| 144 |
+
" -------\n",
|
| 145 |
+
" samples : ndarray\n",
|
| 146 |
+
" There are `num` equally spaced samples in the closed interval\n",
|
| 147 |
+
" ``[start, stop]`` or the half-open interval ``[start, stop)``\n",
|
| 148 |
+
" (depending on whether `endpoint` is True or False).\n",
|
| 149 |
+
" step : float, optional\n",
|
| 150 |
+
" Only returned if `retstep` is True\n",
|
| 151 |
+
" \n",
|
| 152 |
+
" Size of spacing between samples.\n",
|
| 153 |
+
" \n",
|
| 154 |
+
" \n",
|
| 155 |
+
" See Also\n",
|
| 156 |
+
" --------\n",
|
| 157 |
+
" arange : Similar to `linspace`, but uses a step size (instead of the\n",
|
| 158 |
+
" number of samples).\n",
|
| 159 |
+
" geomspace : Similar to `linspace`, but with numbers spaced evenly on a log\n",
|
| 160 |
+
" scale (a geometric progression).\n",
|
| 161 |
+
" logspace : Similar to `geomspace`, but with the end points specified as\n",
|
| 162 |
+
" logarithms.\n",
|
| 163 |
+
" \n",
|
| 164 |
+
" Examples\n",
|
| 165 |
+
" --------\n",
|
| 166 |
+
" >>> np.linspace(2.0, 3.0, num=5)\n",
|
| 167 |
+
" array([2. , 2.25, 2.5 , 2.75, 3. ])\n",
|
| 168 |
+
" >>> np.linspace(2.0, 3.0, num=5, endpoint=False)\n",
|
| 169 |
+
" array([2. , 2.2, 2.4, 2.6, 2.8])\n",
|
| 170 |
+
" >>> np.linspace(2.0, 3.0, num=5, retstep=True)\n",
|
| 171 |
+
" (array([2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)\n",
|
| 172 |
+
" \n",
|
| 173 |
+
" Graphical illustration:\n",
|
| 174 |
+
" \n",
|
| 175 |
+
" >>> import matplotlib.pyplot as plt\n",
|
| 176 |
+
" >>> N = 8\n",
|
| 177 |
+
" >>> y = np.zeros(N)\n",
|
| 178 |
+
" >>> x1 = np.linspace(0, 10, N, endpoint=True)\n",
|
| 179 |
+
" >>> x2 = np.linspace(0, 10, N, endpoint=False)\n",
|
| 180 |
+
" >>> plt.plot(x1, y, 'o')\n",
|
| 181 |
+
" [<matplotlib.lines.Line2D object at 0x...>]\n",
|
| 182 |
+
" >>> plt.plot(x2, y + 0.5, 'o')\n",
|
| 183 |
+
" [<matplotlib.lines.Line2D object at 0x...>]\n",
|
| 184 |
+
" >>> plt.ylim([-0.5, 1])\n",
|
| 185 |
+
" (-0.5, 1)\n",
|
| 186 |
+
" >>> plt.show()\n",
|
| 187 |
+
"\n"
|
| 188 |
+
]
|
| 189 |
+
}
|
| 190 |
+
],
|
| 191 |
+
"source": [
|
| 192 |
+
"help(np.linspace)"
|
| 193 |
+
]
|
| 194 |
+
},
|
| 195 |
+
{
|
| 196 |
+
"cell_type": "markdown",
|
| 197 |
+
"metadata": {},
|
| 198 |
+
"source": [
|
| 199 |
+
"Proviamo quindi a disegnare delle funzioni, ad esempio funzioni trigonometriche:"
|
| 200 |
+
]
|
| 201 |
+
},
|
| 202 |
+
{
|
| 203 |
+
"cell_type": "code",
|
| 204 |
+
"execution_count": 6,
|
| 205 |
+
"metadata": {},
|
| 206 |
+
"outputs": [],
|
| 207 |
+
"source": []
|
| 208 |
+
},
|
| 209 |
+
{
|
| 210 |
+
"cell_type": "markdown",
|
| 211 |
+
"metadata": {},
|
| 212 |
+
"source": [
|
| 213 |
+
"<a id='section4'></a>\n",
|
| 214 |
+
"## 4) Esercizio di Plot di un file root"
|
| 215 |
+
]
|
| 216 |
+
},
|
| 217 |
+
{
|
| 218 |
+
"cell_type": "markdown",
|
| 219 |
+
"metadata": {},
|
| 220 |
+
"source": [
|
| 221 |
+
"In questo esercizio utilizzeremo anche una libreria aggiuntiva che si chiama `uproot` e che serve a leggere i files in formato root."
|
| 222 |
+
]
|
| 223 |
+
},
|
| 224 |
+
{
|
| 225 |
+
"cell_type": "code",
|
| 226 |
+
"execution_count": 1,
|
| 227 |
+
"metadata": {},
|
| 228 |
+
"outputs": [
|
| 229 |
+
{
|
| 230 |
+
"name": "stdout",
|
| 231 |
+
"output_type": "stream",
|
| 232 |
+
"text": [
|
| 233 |
+
"Requirement already satisfied: uproot in /opt/anaconda3/envs/jupyter/lib/python3.9/site-packages (4.2.0)\n",
|
| 234 |
+
"Requirement already satisfied: numpy in /opt/anaconda3/envs/jupyter/lib/python3.9/site-packages (from uproot) (1.22.3)\n",
|
| 235 |
+
"Requirement already satisfied: setuptools in /opt/anaconda3/envs/jupyter/lib/python3.9/site-packages (from uproot) (60.9.3)\n"
|
| 236 |
+
]
|
| 237 |
+
}
|
| 238 |
+
],
|
| 239 |
+
"source": [
|
| 240 |
+
"!pip install uproot"
|
| 241 |
+
]
|
| 242 |
+
},
|
| 243 |
+
{
|
| 244 |
+
"cell_type": "code",
|
| 245 |
+
"execution_count": 2,
|
| 246 |
+
"metadata": {},
|
| 247 |
+
"outputs": [
|
| 248 |
+
{
|
| 249 |
+
"name": "stdout",
|
| 250 |
+
"output_type": "stream",
|
| 251 |
+
"text": [
|
| 252 |
+
" f_mass4l f_massjj\n",
|
| 253 |
+
"0 91.098129 -999.000000\n",
|
| 254 |
+
"1 201.847610 -999.000000\n",
|
| 255 |
+
"2 89.279076 -999.000000\n",
|
| 256 |
+
"3 586.597412 1353.025513\n",
|
| 257 |
+
"4 135.589798 -999.000000\n",
|
| 258 |
+
"... ... ...\n",
|
| 259 |
+
"58102 89.582817 -999.000000\n",
|
| 260 |
+
"58103 252.845184 -999.000000\n",
|
| 261 |
+
"58104 90.129845 -999.000000\n",
|
| 262 |
+
"58105 250.977417 -999.000000\n",
|
| 263 |
+
"58106 229.470154 -999.000000\n",
|
| 264 |
+
"\n",
|
| 265 |
+
"[58107 rows x 2 columns]\n"
|
| 266 |
+
]
|
| 267 |
+
}
|
| 268 |
+
],
|
| 269 |
+
"source": [
|
| 270 |
+
"import uproot\n",
|
| 271 |
+
"import numpy as np\n",
|
| 272 |
+
"import pandas as pd\n",
|
| 273 |
+
"import matplotlib.pyplot as plt\n",
|
| 274 |
+
"%matplotlib inline\n",
|
| 275 |
+
"\n",
|
| 276 |
+
"treename = 'HZZ4LeptonsAnalysisReduced'\n",
|
| 277 |
+
"filename ='data/ntuple_4mu_bkg.root'\n",
|
| 278 |
+
"VARS = ['f_mass4l', 'f_massjj']\n",
|
| 279 |
+
"\n",
|
| 280 |
+
"upfile = uproot.open(filename)\n",
|
| 281 |
+
"params = upfile[treename].arrays(VARS)\n",
|
| 282 |
+
"\n",
|
| 283 |
+
"df = pd.DataFrame(params.tolist())\n",
|
| 284 |
+
"print (df)"
|
| 285 |
+
]
|
| 286 |
+
},
|
| 287 |
+
{
|
| 288 |
+
"cell_type": "markdown",
|
| 289 |
+
"metadata": {},
|
| 290 |
+
"source": [
|
| 291 |
+
"Proviamo ora a plottare questi dati (facciamo un istogramma, o altre rappresentazioni che doveste trovare utili)."
|
| 292 |
+
]
|
| 293 |
+
},
|
| 294 |
+
{
|
| 295 |
+
"cell_type": "code",
|
| 296 |
+
"execution_count": null,
|
| 297 |
+
"metadata": {},
|
| 298 |
+
"outputs": [],
|
| 299 |
+
"source": []
|
| 300 |
+
},
|
| 301 |
+
{
|
| 302 |
+
"cell_type": "markdown",
|
| 303 |
+
"metadata": {},
|
| 304 |
+
"source": [
|
| 305 |
+
"<a id='section5'></a>\n",
|
| 306 |
+
"## 5) Metodo di Eulero"
|
| 307 |
+
]
|
| 308 |
+
},
|
| 309 |
+
{
|
| 310 |
+
"cell_type": "markdown",
|
| 311 |
+
"metadata": {},
|
| 312 |
+
"source": [
|
| 313 |
+
"Cominciamo con le equazioni differenziali per la caduta libera:\n",
|
| 314 |
+
"\n",
|
| 315 |
+
"$$v = \\frac{dx}{dt} \\qquad a = \\frac{dv}{dt}$$\n",
|
| 316 |
+
"\n",
|
| 317 |
+
"dove $a$ è una costante. Possiamo riorganizzare queste equazioni:\n",
|
| 318 |
+
"\n",
|
| 319 |
+
"$$dx = v \\; dt \\qquad dv = a \\; dt$$\n",
|
| 320 |
+
"\n",
|
| 321 |
+
"Ricordati cosa significano $dx$, $dt$ e $dv$: questi sono i cambiamenti infinitesimi di posizione, tempo e velocità. Questa forma delle equazioni suggerisce un possibile modo di approssimare la soluzione alle equazioni differenziali: prendere piccoli passi di tempo $dt$ e calcolare la nuova $x$ e $v$ per ogni fase temporale.\n",
|
| 322 |
+
"\n",
|
| 323 |
+
"$$x_{new} = x_{old} + v \\; dt \\qquad v_{new} = v_{old} + a \\; dt$$\n",
|
| 324 |
+
"\n",
|
| 325 |
+
"Questo è chiamato \"metodo di Eulero\". Dobbiamo compiere piccoli passi per ottenere una buona soluzione, il che significa che dobbiamo compiere molti passi per arrivare ovunque. Ecco a cosa serve il computer: fare tutti quei calcoli noiosi!\n",
|
| 326 |
+
"\n",
|
| 327 |
+
"Proviamo a scrivere un programma che, dato un oggetto con un'altezza iniziale e una velocità iniziate, descriva il moto di caduta libera di questo oggetto e ne disegni la traiettoria seguita."
|
| 328 |
+
]
|
| 329 |
+
},
|
| 330 |
+
{
|
| 331 |
+
"cell_type": "code",
|
| 332 |
+
"execution_count": null,
|
| 333 |
+
"metadata": {},
|
| 334 |
+
"outputs": [],
|
| 335 |
+
"source": []
|
| 336 |
+
},
|
| 337 |
+
{
|
| 338 |
+
"cell_type": "markdown",
|
| 339 |
+
"metadata": {},
|
| 340 |
+
"source": [
|
| 341 |
+
"<a id='section6'></a>\n",
|
| 342 |
+
"## 6) Fit di curve lineare"
|
| 343 |
+
]
|
| 344 |
+
},
|
| 345 |
+
{
|
| 346 |
+
"cell_type": "markdown",
|
| 347 |
+
"metadata": {},
|
| 348 |
+
"source": [
|
| 349 |
+
"È spesso il caso nei laboratori di fisica introduttiva (e in Advanced Physics Labs!) Che abbiamo un set di dati e un modello teorico per i dati, e vorremmo trovare i parametri del modello che rendono il modello più adatto al set di dati.\n",
|
| 350 |
+
"\n",
|
| 351 |
+
"Ad esempio, uno studente del mio laboratorio stava recentemente lavorando a un sensore che avrebbe dovuto tracciare la posizione angolare di un magnete rotante, ma sembrava che il sensore fosse calibrato male. Era difficile dirlo con certezza, tuttavia, poiché il sensore (se funzionava correttamente) riportava la posizione in unità di $\\frac{1}{1024}^{ths}$ di una rivoluzione, e non era affatto spento se lo era affatto. Lo spostamento manuale del magnete non era sufficientemente preciso per determinare se l'errore di calibrazione fosse reale e se fosse reale se fosse coerente.\n",
|
| 352 |
+
"\n",
|
| 353 |
+
"Per indagare ulteriormente sul sistema, ha registrato la posizione del magnete riportata dal sensore ad ogni rotazione, per 10 rotazioni. Poteva solo \"osservare\" le rotazioni, quindi i suoi dati non erano esatti ma era certamente entro 2 gradi (5,7 unità del sensore) di \"zero\" ogni volta. I suoi dati sono di seguito.\n",
|
| 354 |
+
"\n",
|
| 355 |
+
"| turns | position |\n",
|
| 356 |
+
"|---|---|\n",
|
| 357 |
+
"|0 | 0 |\n",
|
| 358 |
+
"|1 | -2 |\n",
|
| 359 |
+
"|2 | -11 |\n",
|
| 360 |
+
"|3 | -15 |\n",
|
| 361 |
+
"|4 | -24 |\n",
|
| 362 |
+
"|5 | -32 |\n",
|
| 363 |
+
"|6 | -40 |\n",
|
| 364 |
+
"|7 | -50 |\n",
|
| 365 |
+
"|8 | -52 |\n",
|
| 366 |
+
"|9 | -62 |\n",
|
| 367 |
+
"|10 | -65 |\n",
|
| 368 |
+
"\n",
|
| 369 |
+
"Sulla base di questi dati, potremmo dire che il sensore è stato calibrato male o è stato solo un errore casuale? Se è stato calibrato male, di quanto è stato calibrato male? In altre parole, se il sensore doveva emettere 1024 impulsi per rotazione, quanti impulsi stava effettivamente emettendo per rotazione?\n",
|
| 370 |
+
"\n",
|
| 371 |
+
"Per rispondere a questa domanda, traccia i dati. I punti dati sono sparsi nella media o c'è una tendenza definita? La tendenza è lineare? Se i dati sono lineari, qual è la pendenza? Quanti impulsi per giro generava realmente il sensore?\n",
|
| 372 |
+
"\n",
|
| 373 |
+
"Metti i dati in due elenchi Python:\n",
|
| 374 |
+
"\n",
|
| 375 |
+
"``\n",
|
| 376 |
+
"turns = [0, 1, 2, 3, ... 10]\n",
|
| 377 |
+
"position = [0, -2, -11, ... -65]\n",
|
| 378 |
+
"``\n",
|
| 379 |
+
"\n",
|
| 380 |
+
"La funzione `polyfit()` in python esegue l'adattamento della curva dei polinomi, inclusi i polinomi del primo ordine (lineari)."
|
| 381 |
+
]
|
| 382 |
+
},
|
| 383 |
+
{
|
| 384 |
+
"cell_type": "code",
|
| 385 |
+
"execution_count": null,
|
| 386 |
+
"metadata": {},
|
| 387 |
+
"outputs": [],
|
| 388 |
+
"source": []
|
| 389 |
+
},
|
| 390 |
+
{
|
| 391 |
+
"cell_type": "markdown",
|
| 392 |
+
"metadata": {},
|
| 393 |
+
"source": [
|
| 394 |
+
"<a id='section7'></a>\n",
|
| 395 |
+
"## Statistica descrittiva"
|
| 396 |
+
]
|
| 397 |
+
},
|
| 398 |
+
{
|
| 399 |
+
"cell_type": "markdown",
|
| 400 |
+
"metadata": {},
|
| 401 |
+
"source": [
|
| 402 |
+
"Calcola le misure di tedenza e viaribility del seguente dataset <a href='https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_boston.html#sklearn.datasets.load_boston' > riguardante i prezzi delle case di Boston</a>:"
|
| 403 |
+
]
|
| 404 |
+
},
|
| 405 |
+
{
|
| 406 |
+
"cell_type": "code",
|
| 407 |
+
"execution_count": null,
|
| 408 |
+
"metadata": {},
|
| 409 |
+
"outputs": [],
|
| 410 |
+
"source": []
|
| 411 |
+
},
|
| 412 |
+
{
|
| 413 |
+
"cell_type": "markdown",
|
| 414 |
+
"metadata": {},
|
| 415 |
+
"source": [
|
| 416 |
+
"[Clicca qui per tornare all'inizio della pagina](#start)<a id='start'></a>"
|
| 417 |
+
]
|
| 418 |
+
}
|
| 419 |
+
],
|
| 420 |
+
"metadata": {
|
| 421 |
+
"kernelspec": {
|
| 422 |
+
"display_name": "Python 3 (ipykernel)",
|
| 423 |
+
"language": "python",
|
| 424 |
+
"name": "python3"
|
| 425 |
+
},
|
| 426 |
+
"language_info": {
|
| 427 |
+
"codemirror_mode": {
|
| 428 |
+
"name": "ipython",
|
| 429 |
+
"version": 3
|
| 430 |
+
},
|
| 431 |
+
"file_extension": ".py",
|
| 432 |
+
"mimetype": "text/x-python",
|
| 433 |
+
"name": "python",
|
| 434 |
+
"nbconvert_exporter": "python",
|
| 435 |
+
"pygments_lexer": "ipython3",
|
| 436 |
+
"version": "3.10.6"
|
| 437 |
+
},
|
| 438 |
+
"nbTranslate": {
|
| 439 |
+
"displayLangs": [
|
| 440 |
+
"*"
|
| 441 |
+
],
|
| 442 |
+
"hotkey": "alt-t",
|
| 443 |
+
"langInMainMenu": true,
|
| 444 |
+
"sourceLang": "en",
|
| 445 |
+
"targetLang": "fr",
|
| 446 |
+
"useGoogleTranslate": true
|
| 447 |
+
}
|
| 448 |
+
},
|
| 449 |
+
"nbformat": 4,
|
| 450 |
+
"nbformat_minor": 2
|
| 451 |
+
}
|
Lezione 0.S - Soluzioni.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Lezione 1.A - Pandas e Feature Engineering.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Lezione 1.E - Esercizi.ipynb
ADDED
|
@@ -0,0 +1,874 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"<a id='start'></a>\n",
|
| 8 |
+
"# Lezione 1 - Esercizi\n",
|
| 9 |
+
"\n",
|
| 10 |
+
"#### Utilizziamo ed esploariamo un dataset di Pandas.\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"Provate a svolgere i seguenti esercizi: <br>\n",
|
| 13 |
+
"1) [Plottare grafici per età di uomini e donne](#section1)<a href='#section1'></a> <br>\n",
|
| 14 |
+
"2) [Rappresentare i morti/sopravvissuti per ogni classe](#section2)<a href='#section2'></a><br>\n",
|
| 15 |
+
"3) [Fitting di un istogramma](#section4)<a href='#section4'></a> <br>\n",
|
| 16 |
+
"4) [Correzione tra prezzo biglietto ed età?](#section5)<a href='#section5'></a> <br>\n",
|
| 17 |
+
"5) [Statistica descrittiva](#section7)<a href='#section7'></a>\n",
|
| 18 |
+
"6) [Getti di particelle](#section8)<a href='#section8'></a>\n",
|
| 19 |
+
"7) [PCA e Isomap](#section9)<a href='#section9'></a>"
|
| 20 |
+
]
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"cell_type": "markdown",
|
| 24 |
+
"metadata": {},
|
| 25 |
+
"source": [
|
| 26 |
+
"Importiamo il dataset del Titanic"
|
| 27 |
+
]
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"cell_type": "code",
|
| 31 |
+
"execution_count": 2,
|
| 32 |
+
"metadata": {},
|
| 33 |
+
"outputs": [
|
| 34 |
+
{
|
| 35 |
+
"data": {
|
| 36 |
+
"text/html": [
|
| 37 |
+
"<div>\n",
|
| 38 |
+
"<style scoped>\n",
|
| 39 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 40 |
+
" vertical-align: middle;\n",
|
| 41 |
+
" }\n",
|
| 42 |
+
"\n",
|
| 43 |
+
" .dataframe tbody tr th {\n",
|
| 44 |
+
" vertical-align: top;\n",
|
| 45 |
+
" }\n",
|
| 46 |
+
"\n",
|
| 47 |
+
" .dataframe thead th {\n",
|
| 48 |
+
" text-align: right;\n",
|
| 49 |
+
" }\n",
|
| 50 |
+
"</style>\n",
|
| 51 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 52 |
+
" <thead>\n",
|
| 53 |
+
" <tr style=\"text-align: right;\">\n",
|
| 54 |
+
" <th></th>\n",
|
| 55 |
+
" <th>PassengerId</th>\n",
|
| 56 |
+
" <th>Survived</th>\n",
|
| 57 |
+
" <th>Pclass</th>\n",
|
| 58 |
+
" <th>Name</th>\n",
|
| 59 |
+
" <th>Sex</th>\n",
|
| 60 |
+
" <th>Age</th>\n",
|
| 61 |
+
" <th>SibSp</th>\n",
|
| 62 |
+
" <th>Parch</th>\n",
|
| 63 |
+
" <th>Ticket</th>\n",
|
| 64 |
+
" <th>Fare</th>\n",
|
| 65 |
+
" <th>Cabin</th>\n",
|
| 66 |
+
" <th>Embarked</th>\n",
|
| 67 |
+
" </tr>\n",
|
| 68 |
+
" </thead>\n",
|
| 69 |
+
" <tbody>\n",
|
| 70 |
+
" <tr>\n",
|
| 71 |
+
" <th>0</th>\n",
|
| 72 |
+
" <td>1</td>\n",
|
| 73 |
+
" <td>0</td>\n",
|
| 74 |
+
" <td>3</td>\n",
|
| 75 |
+
" <td>Braund, Mr. Owen Harris</td>\n",
|
| 76 |
+
" <td>male</td>\n",
|
| 77 |
+
" <td>22.0</td>\n",
|
| 78 |
+
" <td>1</td>\n",
|
| 79 |
+
" <td>0</td>\n",
|
| 80 |
+
" <td>A/5 21171</td>\n",
|
| 81 |
+
" <td>7.2500</td>\n",
|
| 82 |
+
" <td>NaN</td>\n",
|
| 83 |
+
" <td>S</td>\n",
|
| 84 |
+
" </tr>\n",
|
| 85 |
+
" <tr>\n",
|
| 86 |
+
" <th>1</th>\n",
|
| 87 |
+
" <td>2</td>\n",
|
| 88 |
+
" <td>1</td>\n",
|
| 89 |
+
" <td>1</td>\n",
|
| 90 |
+
" <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
|
| 91 |
+
" <td>female</td>\n",
|
| 92 |
+
" <td>38.0</td>\n",
|
| 93 |
+
" <td>1</td>\n",
|
| 94 |
+
" <td>0</td>\n",
|
| 95 |
+
" <td>PC 17599</td>\n",
|
| 96 |
+
" <td>71.2833</td>\n",
|
| 97 |
+
" <td>C85</td>\n",
|
| 98 |
+
" <td>C</td>\n",
|
| 99 |
+
" </tr>\n",
|
| 100 |
+
" <tr>\n",
|
| 101 |
+
" <th>2</th>\n",
|
| 102 |
+
" <td>3</td>\n",
|
| 103 |
+
" <td>1</td>\n",
|
| 104 |
+
" <td>3</td>\n",
|
| 105 |
+
" <td>Heikkinen, Miss. Laina</td>\n",
|
| 106 |
+
" <td>female</td>\n",
|
| 107 |
+
" <td>26.0</td>\n",
|
| 108 |
+
" <td>0</td>\n",
|
| 109 |
+
" <td>0</td>\n",
|
| 110 |
+
" <td>STON/O2. 3101282</td>\n",
|
| 111 |
+
" <td>7.9250</td>\n",
|
| 112 |
+
" <td>NaN</td>\n",
|
| 113 |
+
" <td>S</td>\n",
|
| 114 |
+
" </tr>\n",
|
| 115 |
+
" <tr>\n",
|
| 116 |
+
" <th>3</th>\n",
|
| 117 |
+
" <td>4</td>\n",
|
| 118 |
+
" <td>1</td>\n",
|
| 119 |
+
" <td>1</td>\n",
|
| 120 |
+
" <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
|
| 121 |
+
" <td>female</td>\n",
|
| 122 |
+
" <td>35.0</td>\n",
|
| 123 |
+
" <td>1</td>\n",
|
| 124 |
+
" <td>0</td>\n",
|
| 125 |
+
" <td>113803</td>\n",
|
| 126 |
+
" <td>53.1000</td>\n",
|
| 127 |
+
" <td>C123</td>\n",
|
| 128 |
+
" <td>S</td>\n",
|
| 129 |
+
" </tr>\n",
|
| 130 |
+
" <tr>\n",
|
| 131 |
+
" <th>4</th>\n",
|
| 132 |
+
" <td>5</td>\n",
|
| 133 |
+
" <td>0</td>\n",
|
| 134 |
+
" <td>3</td>\n",
|
| 135 |
+
" <td>Allen, Mr. William Henry</td>\n",
|
| 136 |
+
" <td>male</td>\n",
|
| 137 |
+
" <td>35.0</td>\n",
|
| 138 |
+
" <td>0</td>\n",
|
| 139 |
+
" <td>0</td>\n",
|
| 140 |
+
" <td>373450</td>\n",
|
| 141 |
+
" <td>8.0500</td>\n",
|
| 142 |
+
" <td>NaN</td>\n",
|
| 143 |
+
" <td>S</td>\n",
|
| 144 |
+
" </tr>\n",
|
| 145 |
+
" </tbody>\n",
|
| 146 |
+
"</table>\n",
|
| 147 |
+
"</div>"
|
| 148 |
+
],
|
| 149 |
+
"text/plain": [
|
| 150 |
+
" PassengerId Survived Pclass \\\n",
|
| 151 |
+
"0 1 0 3 \n",
|
| 152 |
+
"1 2 1 1 \n",
|
| 153 |
+
"2 3 1 3 \n",
|
| 154 |
+
"3 4 1 1 \n",
|
| 155 |
+
"4 5 0 3 \n",
|
| 156 |
+
"\n",
|
| 157 |
+
" Name Sex Age SibSp \\\n",
|
| 158 |
+
"0 Braund, Mr. Owen Harris male 22.0 1 \n",
|
| 159 |
+
"1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n",
|
| 160 |
+
"2 Heikkinen, Miss. Laina female 26.0 0 \n",
|
| 161 |
+
"3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 \n",
|
| 162 |
+
"4 Allen, Mr. William Henry male 35.0 0 \n",
|
| 163 |
+
"\n",
|
| 164 |
+
" Parch Ticket Fare Cabin Embarked \n",
|
| 165 |
+
"0 0 A/5 21171 7.2500 NaN S \n",
|
| 166 |
+
"1 0 PC 17599 71.2833 C85 C \n",
|
| 167 |
+
"2 0 STON/O2. 3101282 7.9250 NaN S \n",
|
| 168 |
+
"3 0 113803 53.1000 C123 S \n",
|
| 169 |
+
"4 0 373450 8.0500 NaN S "
|
| 170 |
+
]
|
| 171 |
+
},
|
| 172 |
+
"execution_count": 2,
|
| 173 |
+
"metadata": {},
|
| 174 |
+
"output_type": "execute_result"
|
| 175 |
+
}
|
| 176 |
+
],
|
| 177 |
+
"source": [
|
| 178 |
+
"import pandas as pd\n",
|
| 179 |
+
"\n",
|
| 180 |
+
"titanic = pd.read_csv(\"data/train_dataset_titanic.csv\")\n",
|
| 181 |
+
"titanic.head()"
|
| 182 |
+
]
|
| 183 |
+
},
|
| 184 |
+
{
|
| 185 |
+
"cell_type": "markdown",
|
| 186 |
+
"metadata": {},
|
| 187 |
+
"source": [
|
| 188 |
+
"<a id='section1'></a>\n",
|
| 189 |
+
"## Plottare grafici per età di uomini e donne"
|
| 190 |
+
]
|
| 191 |
+
},
|
| 192 |
+
{
|
| 193 |
+
"cell_type": "markdown",
|
| 194 |
+
"metadata": {},
|
| 195 |
+
"source": [
|
| 196 |
+
"Effettuare dei calcoli sulle età degli uomini e delle donne.<br>\n",
|
| 197 |
+
"Per risolvere l'esercizio:\n",
|
| 198 |
+
"1. Calcolare le principali statistiche delle serie delle età delle donne e degli uomini\n",
|
| 199 |
+
"2. Rappresentare le età degli uomini e delle donne su due distinti istrogrammi, per ognuno dei due istogrammi provare a fittare i dati con una fistribuzione normale e disegnare la funzione di fit calcolata."
|
| 200 |
+
]
|
| 201 |
+
},
|
| 202 |
+
{
|
| 203 |
+
"cell_type": "code",
|
| 204 |
+
"execution_count": null,
|
| 205 |
+
"metadata": {},
|
| 206 |
+
"outputs": [],
|
| 207 |
+
"source": []
|
| 208 |
+
},
|
| 209 |
+
{
|
| 210 |
+
"cell_type": "markdown",
|
| 211 |
+
"metadata": {},
|
| 212 |
+
"source": [
|
| 213 |
+
"<a id='section2'></a>\n",
|
| 214 |
+
"## Rappresentare i morti/sopravvissuti per ogni classe"
|
| 215 |
+
]
|
| 216 |
+
},
|
| 217 |
+
{
|
| 218 |
+
"cell_type": "markdown",
|
| 219 |
+
"metadata": {},
|
| 220 |
+
"source": [
|
| 221 |
+
"Rappresentare in un grafico a pila il numero di morti e sopravvissuti per ogni classe di servizio del viaggio (prima classe, seconda classe, terza classe)."
|
| 222 |
+
]
|
| 223 |
+
},
|
| 224 |
+
{
|
| 225 |
+
"cell_type": "code",
|
| 226 |
+
"execution_count": null,
|
| 227 |
+
"metadata": {},
|
| 228 |
+
"outputs": [],
|
| 229 |
+
"source": []
|
| 230 |
+
},
|
| 231 |
+
{
|
| 232 |
+
"cell_type": "markdown",
|
| 233 |
+
"metadata": {},
|
| 234 |
+
"source": [
|
| 235 |
+
"<a id='section4'></a>\n",
|
| 236 |
+
"## Fitting di un istogramma "
|
| 237 |
+
]
|
| 238 |
+
},
|
| 239 |
+
{
|
| 240 |
+
"cell_type": "markdown",
|
| 241 |
+
"metadata": {},
|
| 242 |
+
"source": [
|
| 243 |
+
"Scaricate il file `data1.txt` e realizzate un programma che legga e fitti i dati in esso contenuti. Il fitting potrà essere effettuato con una funzione gaussiana. Quindi fittate i dati e mostrate i risultati del fitting. \n",
|
| 244 |
+
"\n",
|
| 245 |
+
"Fare lo stesso con i dati nel file `data2.txt`.\n",
|
| 246 |
+
"In questo caso il fitting dovrà avvenire con una funzione risultato della composizione di più funzioni (una per il background e una per i segnali)."
|
| 247 |
+
]
|
| 248 |
+
},
|
| 249 |
+
{
|
| 250 |
+
"cell_type": "code",
|
| 251 |
+
"execution_count": null,
|
| 252 |
+
"metadata": {},
|
| 253 |
+
"outputs": [],
|
| 254 |
+
"source": []
|
| 255 |
+
},
|
| 256 |
+
{
|
| 257 |
+
"cell_type": "markdown",
|
| 258 |
+
"metadata": {},
|
| 259 |
+
"source": [
|
| 260 |
+
"Proviamo a costruire il fit \"a mano\" partendo da una definzione di una funzione di fit e usando il metodo `curve_fit` (ci verrà comodo per l'altra parte dell'esercizio!)."
|
| 261 |
+
]
|
| 262 |
+
},
|
| 263 |
+
{
|
| 264 |
+
"cell_type": "code",
|
| 265 |
+
"execution_count": null,
|
| 266 |
+
"metadata": {},
|
| 267 |
+
"outputs": [],
|
| 268 |
+
"source": []
|
| 269 |
+
},
|
| 270 |
+
{
|
| 271 |
+
"cell_type": "markdown",
|
| 272 |
+
"metadata": {},
|
| 273 |
+
"source": [
|
| 274 |
+
"Così facendo ci è anche possibile creare una funzione di fit sull'istogramma non normalizzato (aggiungendo un fattore di scala K alla funzione della gaussiana)."
|
| 275 |
+
]
|
| 276 |
+
},
|
| 277 |
+
{
|
| 278 |
+
"cell_type": "markdown",
|
| 279 |
+
"metadata": {},
|
| 280 |
+
"source": [
|
| 281 |
+
"Proviamo ora a fittare i dati nel scondo file, usando una funziona di fit che è la composizione di background e segnali."
|
| 282 |
+
]
|
| 283 |
+
},
|
| 284 |
+
{
|
| 285 |
+
"cell_type": "code",
|
| 286 |
+
"execution_count": null,
|
| 287 |
+
"metadata": {},
|
| 288 |
+
"outputs": [],
|
| 289 |
+
"source": []
|
| 290 |
+
},
|
| 291 |
+
{
|
| 292 |
+
"cell_type": "markdown",
|
| 293 |
+
"metadata": {},
|
| 294 |
+
"source": [
|
| 295 |
+
"<a id='section5'></a>\n",
|
| 296 |
+
"## Correlazione tra prezzo biglietto ed età? "
|
| 297 |
+
]
|
| 298 |
+
},
|
| 299 |
+
{
|
| 300 |
+
"cell_type": "markdown",
|
| 301 |
+
"metadata": {},
|
| 302 |
+
"source": [
|
| 303 |
+
"Verificare se esiste una correlazione tra l'età del passeggero e il prezzo pagato per il biglietto. <br>\n",
|
| 304 |
+
"La correlezione può essere vista ad occhio facendo un grafico scatter o calcolata con un indice di correlazione."
|
| 305 |
+
]
|
| 306 |
+
},
|
| 307 |
+
{
|
| 308 |
+
"cell_type": "code",
|
| 309 |
+
"execution_count": null,
|
| 310 |
+
"metadata": {},
|
| 311 |
+
"outputs": [],
|
| 312 |
+
"source": []
|
| 313 |
+
},
|
| 314 |
+
{
|
| 315 |
+
"cell_type": "markdown",
|
| 316 |
+
"metadata": {},
|
| 317 |
+
"source": [
|
| 318 |
+
"<a id='section8'></a>\n",
|
| 319 |
+
"## Getti di particelle"
|
| 320 |
+
]
|
| 321 |
+
},
|
| 322 |
+
{
|
| 323 |
+
"cell_type": "markdown",
|
| 324 |
+
"metadata": {},
|
| 325 |
+
"source": [
|
| 326 |
+
"Esploriamo il file `jetImage.h5`. Il file è costituito da un insieme di getti, provenienti da diversi tipi di particelle\n",
|
| 327 |
+
"- quark leggeri\n",
|
| 328 |
+
"- gluoni\n",
|
| 329 |
+
"- Bosoni W che decadono a due quark leggeri\n",
|
| 330 |
+
"- Bosoni Z che decadono a due quark leggeri\n",
|
| 331 |
+
"- quark top che decadono a tre quark light (tramite decadimenti W+b)\n",
|
| 332 |
+
"\n",
|
| 333 |
+
"Per ogni getto memorizziamo le informazioni della doccia che genera, in diversi formati:\n",
|
| 334 |
+
"- un elenco dei suoi componenti (fino a 188). Ogni costituente è rappresentato da una matrice di 16 quantità (ad esempio, energia e direzione delle particelle, assolute e relative all'asse del getto)\n",
|
| 335 |
+
"- un elenco di caratteristiche che quantificano la cinematica del getto (energia e direzione) e la forma collettiva dei suoi costituenti (caratteristiche che quantificano quanti rebbi contiene il jetc)\n",
|
| 336 |
+
"- l'immagine del getto, ovvero la mappa di temperatura del flusso di energia (tecnicamente, il momento trasverso) in funzione della distanza angolare dall'asse del getto\n",
|
| 337 |
+
"\n",
|
| 338 |
+
"Costruiamo un istogramma con la distribuzione dei momenti trasversali delle costituenti dei getti. Selezioniamo il **momento trasversale**, etichettato j1_pt."
|
| 339 |
+
]
|
| 340 |
+
},
|
| 341 |
+
{
|
| 342 |
+
"cell_type": "code",
|
| 343 |
+
"execution_count": null,
|
| 344 |
+
"metadata": {},
|
| 345 |
+
"outputs": [],
|
| 346 |
+
"source": []
|
| 347 |
+
},
|
| 348 |
+
{
|
| 349 |
+
"cell_type": "markdown",
|
| 350 |
+
"metadata": {},
|
| 351 |
+
"source": [
|
| 352 |
+
"Creiamo quindi un'immagine bidimensionale delle medie dei getti."
|
| 353 |
+
]
|
| 354 |
+
},
|
| 355 |
+
{
|
| 356 |
+
"cell_type": "code",
|
| 357 |
+
"execution_count": null,
|
| 358 |
+
"metadata": {},
|
| 359 |
+
"outputs": [],
|
| 360 |
+
"source": []
|
| 361 |
+
},
|
| 362 |
+
{
|
| 363 |
+
"cell_type": "markdown",
|
| 364 |
+
"metadata": {},
|
| 365 |
+
"source": [
|
| 366 |
+
"Facciamo lo stesso per le immagini ECAL and HCAL."
|
| 367 |
+
]
|
| 368 |
+
},
|
| 369 |
+
{
|
| 370 |
+
"cell_type": "code",
|
| 371 |
+
"execution_count": null,
|
| 372 |
+
"metadata": {},
|
| 373 |
+
"outputs": [],
|
| 374 |
+
"source": []
|
| 375 |
+
},
|
| 376 |
+
{
|
| 377 |
+
"cell_type": "markdown",
|
| 378 |
+
"metadata": {},
|
| 379 |
+
"source": [
|
| 380 |
+
"<a id='section9'></a>\n",
|
| 381 |
+
"## PCA e Isomap"
|
| 382 |
+
]
|
| 383 |
+
},
|
| 384 |
+
{
|
| 385 |
+
"cell_type": "markdown",
|
| 386 |
+
"metadata": {},
|
| 387 |
+
"source": [
|
| 388 |
+
"Useremo il set di dati sul cancro al seno è un dato multivariato a valori reali costituito da due classi, in cui ogni classe indica se un paziente ha o meno un cancro al seno. Le due categorie sono: maligne e benigne.\n",
|
| 389 |
+
"\n",
|
| 390 |
+
"La classe maligna ha 212 campioni, mentre la classe benigna ha 357 campioni.\n",
|
| 391 |
+
"\n",
|
| 392 |
+
"Ha 30 caratteristiche condivise tra tutte le classi: raggio, trama, perimetro, area, levigatezza, dimensione frattale, ecc.\n",
|
| 393 |
+
"\n",
|
| 394 |
+
"Puoi scaricare il set di dati sul cancro con l'aiuto della libreria sklearn."
|
| 395 |
+
]
|
| 396 |
+
},
|
| 397 |
+
{
|
| 398 |
+
"cell_type": "code",
|
| 399 |
+
"execution_count": 2,
|
| 400 |
+
"metadata": {},
|
| 401 |
+
"outputs": [
|
| 402 |
+
{
|
| 403 |
+
"data": {
|
| 404 |
+
"text/html": [
|
| 405 |
+
"<div>\n",
|
| 406 |
+
"<style scoped>\n",
|
| 407 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 408 |
+
" vertical-align: middle;\n",
|
| 409 |
+
" }\n",
|
| 410 |
+
"\n",
|
| 411 |
+
" .dataframe tbody tr th {\n",
|
| 412 |
+
" vertical-align: top;\n",
|
| 413 |
+
" }\n",
|
| 414 |
+
"\n",
|
| 415 |
+
" .dataframe thead th {\n",
|
| 416 |
+
" text-align: right;\n",
|
| 417 |
+
" }\n",
|
| 418 |
+
"</style>\n",
|
| 419 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 420 |
+
" <thead>\n",
|
| 421 |
+
" <tr style=\"text-align: right;\">\n",
|
| 422 |
+
" <th></th>\n",
|
| 423 |
+
" <th>mean radius</th>\n",
|
| 424 |
+
" <th>mean texture</th>\n",
|
| 425 |
+
" <th>mean perimeter</th>\n",
|
| 426 |
+
" <th>mean area</th>\n",
|
| 427 |
+
" <th>mean smoothness</th>\n",
|
| 428 |
+
" <th>mean compactness</th>\n",
|
| 429 |
+
" <th>mean concavity</th>\n",
|
| 430 |
+
" <th>mean concave points</th>\n",
|
| 431 |
+
" <th>mean symmetry</th>\n",
|
| 432 |
+
" <th>mean fractal dimension</th>\n",
|
| 433 |
+
" <th>...</th>\n",
|
| 434 |
+
" <th>worst texture</th>\n",
|
| 435 |
+
" <th>worst perimeter</th>\n",
|
| 436 |
+
" <th>worst area</th>\n",
|
| 437 |
+
" <th>worst smoothness</th>\n",
|
| 438 |
+
" <th>worst compactness</th>\n",
|
| 439 |
+
" <th>worst concavity</th>\n",
|
| 440 |
+
" <th>worst concave points</th>\n",
|
| 441 |
+
" <th>worst symmetry</th>\n",
|
| 442 |
+
" <th>worst fractal dimension</th>\n",
|
| 443 |
+
" <th>label</th>\n",
|
| 444 |
+
" </tr>\n",
|
| 445 |
+
" </thead>\n",
|
| 446 |
+
" <tbody>\n",
|
| 447 |
+
" <tr>\n",
|
| 448 |
+
" <th>0</th>\n",
|
| 449 |
+
" <td>17.99</td>\n",
|
| 450 |
+
" <td>10.38</td>\n",
|
| 451 |
+
" <td>122.80</td>\n",
|
| 452 |
+
" <td>1001.0</td>\n",
|
| 453 |
+
" <td>0.11840</td>\n",
|
| 454 |
+
" <td>0.27760</td>\n",
|
| 455 |
+
" <td>0.30010</td>\n",
|
| 456 |
+
" <td>0.14710</td>\n",
|
| 457 |
+
" <td>0.2419</td>\n",
|
| 458 |
+
" <td>0.07871</td>\n",
|
| 459 |
+
" <td>...</td>\n",
|
| 460 |
+
" <td>17.33</td>\n",
|
| 461 |
+
" <td>184.60</td>\n",
|
| 462 |
+
" <td>2019.0</td>\n",
|
| 463 |
+
" <td>0.16220</td>\n",
|
| 464 |
+
" <td>0.66560</td>\n",
|
| 465 |
+
" <td>0.7119</td>\n",
|
| 466 |
+
" <td>0.2654</td>\n",
|
| 467 |
+
" <td>0.4601</td>\n",
|
| 468 |
+
" <td>0.11890</td>\n",
|
| 469 |
+
" <td>Benign</td>\n",
|
| 470 |
+
" </tr>\n",
|
| 471 |
+
" <tr>\n",
|
| 472 |
+
" <th>1</th>\n",
|
| 473 |
+
" <td>20.57</td>\n",
|
| 474 |
+
" <td>17.77</td>\n",
|
| 475 |
+
" <td>132.90</td>\n",
|
| 476 |
+
" <td>1326.0</td>\n",
|
| 477 |
+
" <td>0.08474</td>\n",
|
| 478 |
+
" <td>0.07864</td>\n",
|
| 479 |
+
" <td>0.08690</td>\n",
|
| 480 |
+
" <td>0.07017</td>\n",
|
| 481 |
+
" <td>0.1812</td>\n",
|
| 482 |
+
" <td>0.05667</td>\n",
|
| 483 |
+
" <td>...</td>\n",
|
| 484 |
+
" <td>23.41</td>\n",
|
| 485 |
+
" <td>158.80</td>\n",
|
| 486 |
+
" <td>1956.0</td>\n",
|
| 487 |
+
" <td>0.12380</td>\n",
|
| 488 |
+
" <td>0.18660</td>\n",
|
| 489 |
+
" <td>0.2416</td>\n",
|
| 490 |
+
" <td>0.1860</td>\n",
|
| 491 |
+
" <td>0.2750</td>\n",
|
| 492 |
+
" <td>0.08902</td>\n",
|
| 493 |
+
" <td>Benign</td>\n",
|
| 494 |
+
" </tr>\n",
|
| 495 |
+
" <tr>\n",
|
| 496 |
+
" <th>2</th>\n",
|
| 497 |
+
" <td>19.69</td>\n",
|
| 498 |
+
" <td>21.25</td>\n",
|
| 499 |
+
" <td>130.00</td>\n",
|
| 500 |
+
" <td>1203.0</td>\n",
|
| 501 |
+
" <td>0.10960</td>\n",
|
| 502 |
+
" <td>0.15990</td>\n",
|
| 503 |
+
" <td>0.19740</td>\n",
|
| 504 |
+
" <td>0.12790</td>\n",
|
| 505 |
+
" <td>0.2069</td>\n",
|
| 506 |
+
" <td>0.05999</td>\n",
|
| 507 |
+
" <td>...</td>\n",
|
| 508 |
+
" <td>25.53</td>\n",
|
| 509 |
+
" <td>152.50</td>\n",
|
| 510 |
+
" <td>1709.0</td>\n",
|
| 511 |
+
" <td>0.14440</td>\n",
|
| 512 |
+
" <td>0.42450</td>\n",
|
| 513 |
+
" <td>0.4504</td>\n",
|
| 514 |
+
" <td>0.2430</td>\n",
|
| 515 |
+
" <td>0.3613</td>\n",
|
| 516 |
+
" <td>0.08758</td>\n",
|
| 517 |
+
" <td>Benign</td>\n",
|
| 518 |
+
" </tr>\n",
|
| 519 |
+
" <tr>\n",
|
| 520 |
+
" <th>3</th>\n",
|
| 521 |
+
" <td>11.42</td>\n",
|
| 522 |
+
" <td>20.38</td>\n",
|
| 523 |
+
" <td>77.58</td>\n",
|
| 524 |
+
" <td>386.1</td>\n",
|
| 525 |
+
" <td>0.14250</td>\n",
|
| 526 |
+
" <td>0.28390</td>\n",
|
| 527 |
+
" <td>0.24140</td>\n",
|
| 528 |
+
" <td>0.10520</td>\n",
|
| 529 |
+
" <td>0.2597</td>\n",
|
| 530 |
+
" <td>0.09744</td>\n",
|
| 531 |
+
" <td>...</td>\n",
|
| 532 |
+
" <td>26.50</td>\n",
|
| 533 |
+
" <td>98.87</td>\n",
|
| 534 |
+
" <td>567.7</td>\n",
|
| 535 |
+
" <td>0.20980</td>\n",
|
| 536 |
+
" <td>0.86630</td>\n",
|
| 537 |
+
" <td>0.6869</td>\n",
|
| 538 |
+
" <td>0.2575</td>\n",
|
| 539 |
+
" <td>0.6638</td>\n",
|
| 540 |
+
" <td>0.17300</td>\n",
|
| 541 |
+
" <td>Benign</td>\n",
|
| 542 |
+
" </tr>\n",
|
| 543 |
+
" <tr>\n",
|
| 544 |
+
" <th>4</th>\n",
|
| 545 |
+
" <td>20.29</td>\n",
|
| 546 |
+
" <td>14.34</td>\n",
|
| 547 |
+
" <td>135.10</td>\n",
|
| 548 |
+
" <td>1297.0</td>\n",
|
| 549 |
+
" <td>0.10030</td>\n",
|
| 550 |
+
" <td>0.13280</td>\n",
|
| 551 |
+
" <td>0.19800</td>\n",
|
| 552 |
+
" <td>0.10430</td>\n",
|
| 553 |
+
" <td>0.1809</td>\n",
|
| 554 |
+
" <td>0.05883</td>\n",
|
| 555 |
+
" <td>...</td>\n",
|
| 556 |
+
" <td>16.67</td>\n",
|
| 557 |
+
" <td>152.20</td>\n",
|
| 558 |
+
" <td>1575.0</td>\n",
|
| 559 |
+
" <td>0.13740</td>\n",
|
| 560 |
+
" <td>0.20500</td>\n",
|
| 561 |
+
" <td>0.4000</td>\n",
|
| 562 |
+
" <td>0.1625</td>\n",
|
| 563 |
+
" <td>0.2364</td>\n",
|
| 564 |
+
" <td>0.07678</td>\n",
|
| 565 |
+
" <td>Benign</td>\n",
|
| 566 |
+
" </tr>\n",
|
| 567 |
+
" <tr>\n",
|
| 568 |
+
" <th>...</th>\n",
|
| 569 |
+
" <td>...</td>\n",
|
| 570 |
+
" <td>...</td>\n",
|
| 571 |
+
" <td>...</td>\n",
|
| 572 |
+
" <td>...</td>\n",
|
| 573 |
+
" <td>...</td>\n",
|
| 574 |
+
" <td>...</td>\n",
|
| 575 |
+
" <td>...</td>\n",
|
| 576 |
+
" <td>...</td>\n",
|
| 577 |
+
" <td>...</td>\n",
|
| 578 |
+
" <td>...</td>\n",
|
| 579 |
+
" <td>...</td>\n",
|
| 580 |
+
" <td>...</td>\n",
|
| 581 |
+
" <td>...</td>\n",
|
| 582 |
+
" <td>...</td>\n",
|
| 583 |
+
" <td>...</td>\n",
|
| 584 |
+
" <td>...</td>\n",
|
| 585 |
+
" <td>...</td>\n",
|
| 586 |
+
" <td>...</td>\n",
|
| 587 |
+
" <td>...</td>\n",
|
| 588 |
+
" <td>...</td>\n",
|
| 589 |
+
" <td>...</td>\n",
|
| 590 |
+
" </tr>\n",
|
| 591 |
+
" <tr>\n",
|
| 592 |
+
" <th>564</th>\n",
|
| 593 |
+
" <td>21.56</td>\n",
|
| 594 |
+
" <td>22.39</td>\n",
|
| 595 |
+
" <td>142.00</td>\n",
|
| 596 |
+
" <td>1479.0</td>\n",
|
| 597 |
+
" <td>0.11100</td>\n",
|
| 598 |
+
" <td>0.11590</td>\n",
|
| 599 |
+
" <td>0.24390</td>\n",
|
| 600 |
+
" <td>0.13890</td>\n",
|
| 601 |
+
" <td>0.1726</td>\n",
|
| 602 |
+
" <td>0.05623</td>\n",
|
| 603 |
+
" <td>...</td>\n",
|
| 604 |
+
" <td>26.40</td>\n",
|
| 605 |
+
" <td>166.10</td>\n",
|
| 606 |
+
" <td>2027.0</td>\n",
|
| 607 |
+
" <td>0.14100</td>\n",
|
| 608 |
+
" <td>0.21130</td>\n",
|
| 609 |
+
" <td>0.4107</td>\n",
|
| 610 |
+
" <td>0.2216</td>\n",
|
| 611 |
+
" <td>0.2060</td>\n",
|
| 612 |
+
" <td>0.07115</td>\n",
|
| 613 |
+
" <td>Benign</td>\n",
|
| 614 |
+
" </tr>\n",
|
| 615 |
+
" <tr>\n",
|
| 616 |
+
" <th>565</th>\n",
|
| 617 |
+
" <td>20.13</td>\n",
|
| 618 |
+
" <td>28.25</td>\n",
|
| 619 |
+
" <td>131.20</td>\n",
|
| 620 |
+
" <td>1261.0</td>\n",
|
| 621 |
+
" <td>0.09780</td>\n",
|
| 622 |
+
" <td>0.10340</td>\n",
|
| 623 |
+
" <td>0.14400</td>\n",
|
| 624 |
+
" <td>0.09791</td>\n",
|
| 625 |
+
" <td>0.1752</td>\n",
|
| 626 |
+
" <td>0.05533</td>\n",
|
| 627 |
+
" <td>...</td>\n",
|
| 628 |
+
" <td>38.25</td>\n",
|
| 629 |
+
" <td>155.00</td>\n",
|
| 630 |
+
" <td>1731.0</td>\n",
|
| 631 |
+
" <td>0.11660</td>\n",
|
| 632 |
+
" <td>0.19220</td>\n",
|
| 633 |
+
" <td>0.3215</td>\n",
|
| 634 |
+
" <td>0.1628</td>\n",
|
| 635 |
+
" <td>0.2572</td>\n",
|
| 636 |
+
" <td>0.06637</td>\n",
|
| 637 |
+
" <td>Benign</td>\n",
|
| 638 |
+
" </tr>\n",
|
| 639 |
+
" <tr>\n",
|
| 640 |
+
" <th>566</th>\n",
|
| 641 |
+
" <td>16.60</td>\n",
|
| 642 |
+
" <td>28.08</td>\n",
|
| 643 |
+
" <td>108.30</td>\n",
|
| 644 |
+
" <td>858.1</td>\n",
|
| 645 |
+
" <td>0.08455</td>\n",
|
| 646 |
+
" <td>0.10230</td>\n",
|
| 647 |
+
" <td>0.09251</td>\n",
|
| 648 |
+
" <td>0.05302</td>\n",
|
| 649 |
+
" <td>0.1590</td>\n",
|
| 650 |
+
" <td>0.05648</td>\n",
|
| 651 |
+
" <td>...</td>\n",
|
| 652 |
+
" <td>34.12</td>\n",
|
| 653 |
+
" <td>126.70</td>\n",
|
| 654 |
+
" <td>1124.0</td>\n",
|
| 655 |
+
" <td>0.11390</td>\n",
|
| 656 |
+
" <td>0.30940</td>\n",
|
| 657 |
+
" <td>0.3403</td>\n",
|
| 658 |
+
" <td>0.1418</td>\n",
|
| 659 |
+
" <td>0.2218</td>\n",
|
| 660 |
+
" <td>0.07820</td>\n",
|
| 661 |
+
" <td>Benign</td>\n",
|
| 662 |
+
" </tr>\n",
|
| 663 |
+
" <tr>\n",
|
| 664 |
+
" <th>567</th>\n",
|
| 665 |
+
" <td>20.60</td>\n",
|
| 666 |
+
" <td>29.33</td>\n",
|
| 667 |
+
" <td>140.10</td>\n",
|
| 668 |
+
" <td>1265.0</td>\n",
|
| 669 |
+
" <td>0.11780</td>\n",
|
| 670 |
+
" <td>0.27700</td>\n",
|
| 671 |
+
" <td>0.35140</td>\n",
|
| 672 |
+
" <td>0.15200</td>\n",
|
| 673 |
+
" <td>0.2397</td>\n",
|
| 674 |
+
" <td>0.07016</td>\n",
|
| 675 |
+
" <td>...</td>\n",
|
| 676 |
+
" <td>39.42</td>\n",
|
| 677 |
+
" <td>184.60</td>\n",
|
| 678 |
+
" <td>1821.0</td>\n",
|
| 679 |
+
" <td>0.16500</td>\n",
|
| 680 |
+
" <td>0.86810</td>\n",
|
| 681 |
+
" <td>0.9387</td>\n",
|
| 682 |
+
" <td>0.2650</td>\n",
|
| 683 |
+
" <td>0.4087</td>\n",
|
| 684 |
+
" <td>0.12400</td>\n",
|
| 685 |
+
" <td>Benign</td>\n",
|
| 686 |
+
" </tr>\n",
|
| 687 |
+
" <tr>\n",
|
| 688 |
+
" <th>568</th>\n",
|
| 689 |
+
" <td>7.76</td>\n",
|
| 690 |
+
" <td>24.54</td>\n",
|
| 691 |
+
" <td>47.92</td>\n",
|
| 692 |
+
" <td>181.0</td>\n",
|
| 693 |
+
" <td>0.05263</td>\n",
|
| 694 |
+
" <td>0.04362</td>\n",
|
| 695 |
+
" <td>0.00000</td>\n",
|
| 696 |
+
" <td>0.00000</td>\n",
|
| 697 |
+
" <td>0.1587</td>\n",
|
| 698 |
+
" <td>0.05884</td>\n",
|
| 699 |
+
" <td>...</td>\n",
|
| 700 |
+
" <td>30.37</td>\n",
|
| 701 |
+
" <td>59.16</td>\n",
|
| 702 |
+
" <td>268.6</td>\n",
|
| 703 |
+
" <td>0.08996</td>\n",
|
| 704 |
+
" <td>0.06444</td>\n",
|
| 705 |
+
" <td>0.0000</td>\n",
|
| 706 |
+
" <td>0.0000</td>\n",
|
| 707 |
+
" <td>0.2871</td>\n",
|
| 708 |
+
" <td>0.07039</td>\n",
|
| 709 |
+
" <td>Malignant</td>\n",
|
| 710 |
+
" </tr>\n",
|
| 711 |
+
" </tbody>\n",
|
| 712 |
+
"</table>\n",
|
| 713 |
+
"<p>569 rows × 31 columns</p>\n",
|
| 714 |
+
"</div>"
|
| 715 |
+
],
|
| 716 |
+
"text/plain": [
|
| 717 |
+
" mean radius mean texture mean perimeter mean area mean smoothness \\\n",
|
| 718 |
+
"0 17.99 10.38 122.80 1001.0 0.11840 \n",
|
| 719 |
+
"1 20.57 17.77 132.90 1326.0 0.08474 \n",
|
| 720 |
+
"2 19.69 21.25 130.00 1203.0 0.10960 \n",
|
| 721 |
+
"3 11.42 20.38 77.58 386.1 0.14250 \n",
|
| 722 |
+
"4 20.29 14.34 135.10 1297.0 0.10030 \n",
|
| 723 |
+
".. ... ... ... ... ... \n",
|
| 724 |
+
"564 21.56 22.39 142.00 1479.0 0.11100 \n",
|
| 725 |
+
"565 20.13 28.25 131.20 1261.0 0.09780 \n",
|
| 726 |
+
"566 16.60 28.08 108.30 858.1 0.08455 \n",
|
| 727 |
+
"567 20.60 29.33 140.10 1265.0 0.11780 \n",
|
| 728 |
+
"568 7.76 24.54 47.92 181.0 0.05263 \n",
|
| 729 |
+
"\n",
|
| 730 |
+
" mean compactness mean concavity mean concave points mean symmetry \\\n",
|
| 731 |
+
"0 0.27760 0.30010 0.14710 0.2419 \n",
|
| 732 |
+
"1 0.07864 0.08690 0.07017 0.1812 \n",
|
| 733 |
+
"2 0.15990 0.19740 0.12790 0.2069 \n",
|
| 734 |
+
"3 0.28390 0.24140 0.10520 0.2597 \n",
|
| 735 |
+
"4 0.13280 0.19800 0.10430 0.1809 \n",
|
| 736 |
+
".. ... ... ... ... \n",
|
| 737 |
+
"564 0.11590 0.24390 0.13890 0.1726 \n",
|
| 738 |
+
"565 0.10340 0.14400 0.09791 0.1752 \n",
|
| 739 |
+
"566 0.10230 0.09251 0.05302 0.1590 \n",
|
| 740 |
+
"567 0.27700 0.35140 0.15200 0.2397 \n",
|
| 741 |
+
"568 0.04362 0.00000 0.00000 0.1587 \n",
|
| 742 |
+
"\n",
|
| 743 |
+
" mean fractal dimension ... worst texture worst perimeter worst area \\\n",
|
| 744 |
+
"0 0.07871 ... 17.33 184.60 2019.0 \n",
|
| 745 |
+
"1 0.05667 ... 23.41 158.80 1956.0 \n",
|
| 746 |
+
"2 0.05999 ... 25.53 152.50 1709.0 \n",
|
| 747 |
+
"3 0.09744 ... 26.50 98.87 567.7 \n",
|
| 748 |
+
"4 0.05883 ... 16.67 152.20 1575.0 \n",
|
| 749 |
+
".. ... ... ... ... ... \n",
|
| 750 |
+
"564 0.05623 ... 26.40 166.10 2027.0 \n",
|
| 751 |
+
"565 0.05533 ... 38.25 155.00 1731.0 \n",
|
| 752 |
+
"566 0.05648 ... 34.12 126.70 1124.0 \n",
|
| 753 |
+
"567 0.07016 ... 39.42 184.60 1821.0 \n",
|
| 754 |
+
"568 0.05884 ... 30.37 59.16 268.6 \n",
|
| 755 |
+
"\n",
|
| 756 |
+
" worst smoothness worst compactness worst concavity \\\n",
|
| 757 |
+
"0 0.16220 0.66560 0.7119 \n",
|
| 758 |
+
"1 0.12380 0.18660 0.2416 \n",
|
| 759 |
+
"2 0.14440 0.42450 0.4504 \n",
|
| 760 |
+
"3 0.20980 0.86630 0.6869 \n",
|
| 761 |
+
"4 0.13740 0.20500 0.4000 \n",
|
| 762 |
+
".. ... ... ... \n",
|
| 763 |
+
"564 0.14100 0.21130 0.4107 \n",
|
| 764 |
+
"565 0.11660 0.19220 0.3215 \n",
|
| 765 |
+
"566 0.11390 0.30940 0.3403 \n",
|
| 766 |
+
"567 0.16500 0.86810 0.9387 \n",
|
| 767 |
+
"568 0.08996 0.06444 0.0000 \n",
|
| 768 |
+
"\n",
|
| 769 |
+
" worst concave points worst symmetry worst fractal dimension label \n",
|
| 770 |
+
"0 0.2654 0.4601 0.11890 Benign \n",
|
| 771 |
+
"1 0.1860 0.2750 0.08902 Benign \n",
|
| 772 |
+
"2 0.2430 0.3613 0.08758 Benign \n",
|
| 773 |
+
"3 0.2575 0.6638 0.17300 Benign \n",
|
| 774 |
+
"4 0.1625 0.2364 0.07678 Benign \n",
|
| 775 |
+
".. ... ... ... ... \n",
|
| 776 |
+
"564 0.2216 0.2060 0.07115 Benign \n",
|
| 777 |
+
"565 0.1628 0.2572 0.06637 Benign \n",
|
| 778 |
+
"566 0.1418 0.2218 0.07820 Benign \n",
|
| 779 |
+
"567 0.2650 0.4087 0.12400 Benign \n",
|
| 780 |
+
"568 0.0000 0.2871 0.07039 Malignant \n",
|
| 781 |
+
"\n",
|
| 782 |
+
"[569 rows x 31 columns]"
|
| 783 |
+
]
|
| 784 |
+
},
|
| 785 |
+
"execution_count": 2,
|
| 786 |
+
"metadata": {},
|
| 787 |
+
"output_type": "execute_result"
|
| 788 |
+
}
|
| 789 |
+
],
|
| 790 |
+
"source": [
|
| 791 |
+
"from sklearn.datasets import load_breast_cancer\n",
|
| 792 |
+
"import numpy as np\n",
|
| 793 |
+
"import pandas as pd\n",
|
| 794 |
+
"\n",
|
| 795 |
+
"breast = load_breast_cancer()\n",
|
| 796 |
+
"\n",
|
| 797 |
+
"breast_data = breast.data\n",
|
| 798 |
+
"breast_labels = breast.target\n",
|
| 799 |
+
"features = breast.feature_names\n",
|
| 800 |
+
"\n",
|
| 801 |
+
"labels = np.reshape(breast_labels,(569,1))\n",
|
| 802 |
+
"final_breast_data = np.concatenate([breast_data,labels],axis=1)\n",
|
| 803 |
+
"\n",
|
| 804 |
+
"breast_dataset = pd.DataFrame(final_breast_data)\n",
|
| 805 |
+
"features_labels = np.append(features,'label')\n",
|
| 806 |
+
"breast_dataset.columns = features_labels\n",
|
| 807 |
+
"\n",
|
| 808 |
+
"breast_dataset['label'].replace(0, 'Benign', inplace=True)\n",
|
| 809 |
+
"breast_dataset['label'].replace(1, 'Malignant', inplace=True)\n",
|
| 810 |
+
"\n",
|
| 811 |
+
"breast_dataset"
|
| 812 |
+
]
|
| 813 |
+
},
|
| 814 |
+
{
|
| 815 |
+
"cell_type": "code",
|
| 816 |
+
"execution_count": null,
|
| 817 |
+
"metadata": {},
|
| 818 |
+
"outputs": [],
|
| 819 |
+
"source": []
|
| 820 |
+
},
|
| 821 |
+
{
|
| 822 |
+
"cell_type": "markdown",
|
| 823 |
+
"metadata": {},
|
| 824 |
+
"source": [
|
| 825 |
+
"Facciamo ora la stessa cosa usando Isomap:"
|
| 826 |
+
]
|
| 827 |
+
},
|
| 828 |
+
{
|
| 829 |
+
"cell_type": "code",
|
| 830 |
+
"execution_count": null,
|
| 831 |
+
"metadata": {},
|
| 832 |
+
"outputs": [],
|
| 833 |
+
"source": []
|
| 834 |
+
},
|
| 835 |
+
{
|
| 836 |
+
"cell_type": "markdown",
|
| 837 |
+
"metadata": {},
|
| 838 |
+
"source": [
|
| 839 |
+
"[Clicca qui per tornare all'inizio della pagina](#start)<a id='start'></a>"
|
| 840 |
+
]
|
| 841 |
+
}
|
| 842 |
+
],
|
| 843 |
+
"metadata": {
|
| 844 |
+
"kernelspec": {
|
| 845 |
+
"display_name": "Python 3 (ipykernel)",
|
| 846 |
+
"language": "python",
|
| 847 |
+
"name": "python3"
|
| 848 |
+
},
|
| 849 |
+
"language_info": {
|
| 850 |
+
"codemirror_mode": {
|
| 851 |
+
"name": "ipython",
|
| 852 |
+
"version": 3
|
| 853 |
+
},
|
| 854 |
+
"file_extension": ".py",
|
| 855 |
+
"mimetype": "text/x-python",
|
| 856 |
+
"name": "python",
|
| 857 |
+
"nbconvert_exporter": "python",
|
| 858 |
+
"pygments_lexer": "ipython3",
|
| 859 |
+
"version": "3.10.6"
|
| 860 |
+
},
|
| 861 |
+
"nbTranslate": {
|
| 862 |
+
"displayLangs": [
|
| 863 |
+
"*"
|
| 864 |
+
],
|
| 865 |
+
"hotkey": "alt-t",
|
| 866 |
+
"langInMainMenu": true,
|
| 867 |
+
"sourceLang": "en",
|
| 868 |
+
"targetLang": "fr",
|
| 869 |
+
"useGoogleTranslate": true
|
| 870 |
+
}
|
| 871 |
+
},
|
| 872 |
+
"nbformat": 4,
|
| 873 |
+
"nbformat_minor": 2
|
| 874 |
+
}
|
Lezione 1.S - Soluzioni.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Lezione 2.A - Regressione e Classificazione.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Lezione 2.E - Esercizi.ipynb
ADDED
|
@@ -0,0 +1,891 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"<a id='start'></a>\n",
|
| 8 |
+
"# Regressione lineare Esercizi\n",
|
| 9 |
+
"\n",
|
| 10 |
+
"In questo notebook vengono presentati degli esercizi sulla regressione lineare. <br>\n",
|
| 11 |
+
"Provate a svolgere i seguenti esercizi: <br>\n",
|
| 12 |
+
"1) [Regressione semplice](#section1)<br>\n",
|
| 13 |
+
"2) [Prezzi case](#section2)<br>\n",
|
| 14 |
+
"3) [Regressione multipla](#section3)<br>\n",
|
| 15 |
+
"4) [Regressione polinomiale](#section5)"
|
| 16 |
+
]
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"cell_type": "markdown",
|
| 20 |
+
"metadata": {},
|
| 21 |
+
"source": [
|
| 22 |
+
"<a id='section1'></a>\n",
|
| 23 |
+
"## Regressione semplice"
|
| 24 |
+
]
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"cell_type": "markdown",
|
| 28 |
+
"metadata": {},
|
| 29 |
+
"source": [
|
| 30 |
+
"Provare ad effettuare un'analisi di regressione su questi dati:"
|
| 31 |
+
]
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"cell_type": "markdown",
|
| 35 |
+
"metadata": {},
|
| 36 |
+
"source": [
|
| 37 |
+
"<img src=\"img/python-linear-regression.png\">"
|
| 38 |
+
]
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"cell_type": "code",
|
| 42 |
+
"execution_count": null,
|
| 43 |
+
"metadata": {},
|
| 44 |
+
"outputs": [],
|
| 45 |
+
"source": []
|
| 46 |
+
},
|
| 47 |
+
{
|
| 48 |
+
"cell_type": "markdown",
|
| 49 |
+
"metadata": {},
|
| 50 |
+
"source": [
|
| 51 |
+
"<a id='section2'></a>\n",
|
| 52 |
+
"## Prezzi case"
|
| 53 |
+
]
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"cell_type": "markdown",
|
| 57 |
+
"metadata": {},
|
| 58 |
+
"source": [
|
| 59 |
+
"Hai un dataset coi pressi delle case.\n",
|
| 60 |
+
"\n",
|
| 61 |
+
"I dati sono salvati nel file: 'real_estate_price_size.csv'. \n",
|
| 62 |
+
"\n",
|
| 63 |
+
"Devi creare una regressione lineare semplice, utilizzando i dati.\n",
|
| 64 |
+
"\n",
|
| 65 |
+
"In questo esercizio la veriabile dipendente sarà 'price', mentre la variaibli indipendente sarà 'size'."
|
| 66 |
+
]
|
| 67 |
+
},
|
| 68 |
+
{
|
| 69 |
+
"cell_type": "code",
|
| 70 |
+
"execution_count": null,
|
| 71 |
+
"metadata": {},
|
| 72 |
+
"outputs": [],
|
| 73 |
+
"source": []
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "markdown",
|
| 77 |
+
"metadata": {},
|
| 78 |
+
"source": [
|
| 79 |
+
"<a id='section3'></a>\n",
|
| 80 |
+
"## Regressione multipla"
|
| 81 |
+
]
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
"cell_type": "markdown",
|
| 85 |
+
"metadata": {},
|
| 86 |
+
"source": [
|
| 87 |
+
"Effettuare una regressione multipla sui dati contenuti nel file 'ex1data2.csv' ce contiene i dati dei prezzi delle case (variabile dipendente) e i dati sul numero di stanze e la dimensione della casa (variabili indipendenti)."
|
| 88 |
+
]
|
| 89 |
+
},
|
| 90 |
+
{
|
| 91 |
+
"cell_type": "code",
|
| 92 |
+
"execution_count": null,
|
| 93 |
+
"metadata": {},
|
| 94 |
+
"outputs": [],
|
| 95 |
+
"source": []
|
| 96 |
+
},
|
| 97 |
+
{
|
| 98 |
+
"cell_type": "markdown",
|
| 99 |
+
"metadata": {},
|
| 100 |
+
"source": [
|
| 101 |
+
"<a id='section5'></a>\n",
|
| 102 |
+
"## Regressione polinomiale"
|
| 103 |
+
]
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"cell_type": "markdown",
|
| 107 |
+
"metadata": {},
|
| 108 |
+
"source": [
|
| 109 |
+
"Leggendo il dataset 'mussel.csv' provate a trovare un'equazione polinomiale che permetta un buon fit dei dati in esso presenti."
|
| 110 |
+
]
|
| 111 |
+
},
|
| 112 |
+
{
|
| 113 |
+
"cell_type": "code",
|
| 114 |
+
"execution_count": null,
|
| 115 |
+
"metadata": {},
|
| 116 |
+
"outputs": [],
|
| 117 |
+
"source": []
|
| 118 |
+
},
|
| 119 |
+
{
|
| 120 |
+
"cell_type": "markdown",
|
| 121 |
+
"metadata": {},
|
| 122 |
+
"source": [
|
| 123 |
+
"<a id='start'></a>\n",
|
| 124 |
+
"# Decision Tree Esercizi\n",
|
| 125 |
+
"\n",
|
| 126 |
+
"In questo notebook vengono presentati degli esercizi sui Decision Tree. <br>\n",
|
| 127 |
+
"Provate a svolgere il seguente esercizio: <br>\n",
|
| 128 |
+
"1) [Sopravvivere all'iceberg](#section1)<br>\n",
|
| 129 |
+
"2) [Predire il diabete](#section2)<br>\n",
|
| 130 |
+
"3) [Crime in Chicago](#section3)"
|
| 131 |
+
]
|
| 132 |
+
},
|
| 133 |
+
{
|
| 134 |
+
"cell_type": "markdown",
|
| 135 |
+
"metadata": {},
|
| 136 |
+
"source": [
|
| 137 |
+
"<a id='section1'></a>\n",
|
| 138 |
+
"## Sopravvivere all'iceberg"
|
| 139 |
+
]
|
| 140 |
+
},
|
| 141 |
+
{
|
| 142 |
+
"cell_type": "markdown",
|
| 143 |
+
"metadata": {},
|
| 144 |
+
"source": [
|
| 145 |
+
"Torniamo ad analizzare i dati dei passeggeri del Titanic. Proviamo a vedere se è possibile realizzare un albero di decisione che ci permetta di discernere chi è morto da chi è sopravvissuto:"
|
| 146 |
+
]
|
| 147 |
+
},
|
| 148 |
+
{
|
| 149 |
+
"cell_type": "code",
|
| 150 |
+
"execution_count": 88,
|
| 151 |
+
"metadata": {},
|
| 152 |
+
"outputs": [
|
| 153 |
+
{
|
| 154 |
+
"data": {
|
| 155 |
+
"text/html": [
|
| 156 |
+
"<div>\n",
|
| 157 |
+
"<style scoped>\n",
|
| 158 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 159 |
+
" vertical-align: middle;\n",
|
| 160 |
+
" }\n",
|
| 161 |
+
"\n",
|
| 162 |
+
" .dataframe tbody tr th {\n",
|
| 163 |
+
" vertical-align: top;\n",
|
| 164 |
+
" }\n",
|
| 165 |
+
"\n",
|
| 166 |
+
" .dataframe thead th {\n",
|
| 167 |
+
" text-align: right;\n",
|
| 168 |
+
" }\n",
|
| 169 |
+
"</style>\n",
|
| 170 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 171 |
+
" <thead>\n",
|
| 172 |
+
" <tr style=\"text-align: right;\">\n",
|
| 173 |
+
" <th></th>\n",
|
| 174 |
+
" <th>PassengerId</th>\n",
|
| 175 |
+
" <th>Survived</th>\n",
|
| 176 |
+
" <th>Pclass</th>\n",
|
| 177 |
+
" <th>Name</th>\n",
|
| 178 |
+
" <th>Sex</th>\n",
|
| 179 |
+
" <th>Age</th>\n",
|
| 180 |
+
" <th>SibSp</th>\n",
|
| 181 |
+
" <th>Parch</th>\n",
|
| 182 |
+
" <th>Ticket</th>\n",
|
| 183 |
+
" <th>Fare</th>\n",
|
| 184 |
+
" <th>Cabin</th>\n",
|
| 185 |
+
" <th>Embarked</th>\n",
|
| 186 |
+
" </tr>\n",
|
| 187 |
+
" </thead>\n",
|
| 188 |
+
" <tbody>\n",
|
| 189 |
+
" <tr>\n",
|
| 190 |
+
" <th>0</th>\n",
|
| 191 |
+
" <td>1</td>\n",
|
| 192 |
+
" <td>0</td>\n",
|
| 193 |
+
" <td>3</td>\n",
|
| 194 |
+
" <td>Braund, Mr. Owen Harris</td>\n",
|
| 195 |
+
" <td>male</td>\n",
|
| 196 |
+
" <td>22.0</td>\n",
|
| 197 |
+
" <td>1</td>\n",
|
| 198 |
+
" <td>0</td>\n",
|
| 199 |
+
" <td>A/5 21171</td>\n",
|
| 200 |
+
" <td>7.2500</td>\n",
|
| 201 |
+
" <td>NaN</td>\n",
|
| 202 |
+
" <td>S</td>\n",
|
| 203 |
+
" </tr>\n",
|
| 204 |
+
" <tr>\n",
|
| 205 |
+
" <th>1</th>\n",
|
| 206 |
+
" <td>2</td>\n",
|
| 207 |
+
" <td>1</td>\n",
|
| 208 |
+
" <td>1</td>\n",
|
| 209 |
+
" <td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>\n",
|
| 210 |
+
" <td>female</td>\n",
|
| 211 |
+
" <td>38.0</td>\n",
|
| 212 |
+
" <td>1</td>\n",
|
| 213 |
+
" <td>0</td>\n",
|
| 214 |
+
" <td>PC 17599</td>\n",
|
| 215 |
+
" <td>71.2833</td>\n",
|
| 216 |
+
" <td>C85</td>\n",
|
| 217 |
+
" <td>C</td>\n",
|
| 218 |
+
" </tr>\n",
|
| 219 |
+
" <tr>\n",
|
| 220 |
+
" <th>2</th>\n",
|
| 221 |
+
" <td>3</td>\n",
|
| 222 |
+
" <td>1</td>\n",
|
| 223 |
+
" <td>3</td>\n",
|
| 224 |
+
" <td>Heikkinen, Miss. Laina</td>\n",
|
| 225 |
+
" <td>female</td>\n",
|
| 226 |
+
" <td>26.0</td>\n",
|
| 227 |
+
" <td>0</td>\n",
|
| 228 |
+
" <td>0</td>\n",
|
| 229 |
+
" <td>STON/O2. 3101282</td>\n",
|
| 230 |
+
" <td>7.9250</td>\n",
|
| 231 |
+
" <td>NaN</td>\n",
|
| 232 |
+
" <td>S</td>\n",
|
| 233 |
+
" </tr>\n",
|
| 234 |
+
" <tr>\n",
|
| 235 |
+
" <th>3</th>\n",
|
| 236 |
+
" <td>4</td>\n",
|
| 237 |
+
" <td>1</td>\n",
|
| 238 |
+
" <td>1</td>\n",
|
| 239 |
+
" <td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>\n",
|
| 240 |
+
" <td>female</td>\n",
|
| 241 |
+
" <td>35.0</td>\n",
|
| 242 |
+
" <td>1</td>\n",
|
| 243 |
+
" <td>0</td>\n",
|
| 244 |
+
" <td>113803</td>\n",
|
| 245 |
+
" <td>53.1000</td>\n",
|
| 246 |
+
" <td>C123</td>\n",
|
| 247 |
+
" <td>S</td>\n",
|
| 248 |
+
" </tr>\n",
|
| 249 |
+
" <tr>\n",
|
| 250 |
+
" <th>4</th>\n",
|
| 251 |
+
" <td>5</td>\n",
|
| 252 |
+
" <td>0</td>\n",
|
| 253 |
+
" <td>3</td>\n",
|
| 254 |
+
" <td>Allen, Mr. William Henry</td>\n",
|
| 255 |
+
" <td>male</td>\n",
|
| 256 |
+
" <td>35.0</td>\n",
|
| 257 |
+
" <td>0</td>\n",
|
| 258 |
+
" <td>0</td>\n",
|
| 259 |
+
" <td>373450</td>\n",
|
| 260 |
+
" <td>8.0500</td>\n",
|
| 261 |
+
" <td>NaN</td>\n",
|
| 262 |
+
" <td>S</td>\n",
|
| 263 |
+
" </tr>\n",
|
| 264 |
+
" </tbody>\n",
|
| 265 |
+
"</table>\n",
|
| 266 |
+
"</div>"
|
| 267 |
+
],
|
| 268 |
+
"text/plain": [
|
| 269 |
+
" PassengerId Survived Pclass \\\n",
|
| 270 |
+
"0 1 0 3 \n",
|
| 271 |
+
"1 2 1 1 \n",
|
| 272 |
+
"2 3 1 3 \n",
|
| 273 |
+
"3 4 1 1 \n",
|
| 274 |
+
"4 5 0 3 \n",
|
| 275 |
+
"\n",
|
| 276 |
+
" Name Sex Age SibSp \\\n",
|
| 277 |
+
"0 Braund, Mr. Owen Harris male 22.0 1 \n",
|
| 278 |
+
"1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n",
|
| 279 |
+
"2 Heikkinen, Miss. Laina female 26.0 0 \n",
|
| 280 |
+
"3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 \n",
|
| 281 |
+
"4 Allen, Mr. William Henry male 35.0 0 \n",
|
| 282 |
+
"\n",
|
| 283 |
+
" Parch Ticket Fare Cabin Embarked \n",
|
| 284 |
+
"0 0 A/5 21171 7.2500 NaN S \n",
|
| 285 |
+
"1 0 PC 17599 71.2833 C85 C \n",
|
| 286 |
+
"2 0 STON/O2. 3101282 7.9250 NaN S \n",
|
| 287 |
+
"3 0 113803 53.1000 C123 S \n",
|
| 288 |
+
"4 0 373450 8.0500 NaN S "
|
| 289 |
+
]
|
| 290 |
+
},
|
| 291 |
+
"execution_count": 88,
|
| 292 |
+
"metadata": {},
|
| 293 |
+
"output_type": "execute_result"
|
| 294 |
+
}
|
| 295 |
+
],
|
| 296 |
+
"source": [
|
| 297 |
+
"import pandas as pd\n",
|
| 298 |
+
"import numpy as np\n",
|
| 299 |
+
"import matplotlib.pyplot as plt\n",
|
| 300 |
+
"import xgboost as xgb\n",
|
| 301 |
+
"\n",
|
| 302 |
+
"train = pd.read_csv(\"data/train_dataset_titanic.csv\")\n",
|
| 303 |
+
"train.head()"
|
| 304 |
+
]
|
| 305 |
+
},
|
| 306 |
+
{
|
| 307 |
+
"cell_type": "code",
|
| 308 |
+
"execution_count": 89,
|
| 309 |
+
"metadata": {},
|
| 310 |
+
"outputs": [],
|
| 311 |
+
"source": []
|
| 312 |
+
},
|
| 313 |
+
{
|
| 314 |
+
"cell_type": "markdown",
|
| 315 |
+
"metadata": {},
|
| 316 |
+
"source": [
|
| 317 |
+
"Proviamo ora a usare XGBoost per predire all'imbarco la probabilità di morte al momento dell'incidente.\n",
|
| 318 |
+
"\n",
|
| 319 |
+
"Il dataset è così descritto:\n",
|
| 320 |
+
"- survival - Survival (0 = No; 1 = Yes)\n",
|
| 321 |
+
"- class - Passenger Class (1 = 1st; 2 = 2nd; 3 = 3rd)\n",
|
| 322 |
+
"- name - Name\n",
|
| 323 |
+
"- sex - Sex\n",
|
| 324 |
+
"- age - Age\n",
|
| 325 |
+
"- sibsp - Number of Siblings/Spouses Aboard\n",
|
| 326 |
+
"- parch - Number of Parents/Children Aboard\n",
|
| 327 |
+
"- ticket - Ticket Number\n",
|
| 328 |
+
"- fare - Passenger Fare\n",
|
| 329 |
+
"- cabin - Cabin\n",
|
| 330 |
+
"- embarked - Port of Embarkation (C = Cherbourg; Q = Queenstown; S = Southampton)\n",
|
| 331 |
+
"\n",
|
| 332 |
+
"Possiamo quindi cercare di prevedere 'survival' partendo da 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked'.\n",
|
| 333 |
+
"\n",
|
| 334 |
+
"Tutte le label sono già numeriche tranne Sex e Embarked, che trasformiamo."
|
| 335 |
+
]
|
| 336 |
+
},
|
| 337 |
+
{
|
| 338 |
+
"cell_type": "code",
|
| 339 |
+
"execution_count": 100,
|
| 340 |
+
"metadata": {},
|
| 341 |
+
"outputs": [],
|
| 342 |
+
"source": []
|
| 343 |
+
},
|
| 344 |
+
{
|
| 345 |
+
"cell_type": "markdown",
|
| 346 |
+
"metadata": {},
|
| 347 |
+
"source": [
|
| 348 |
+
"<a id='section2'></a>\n",
|
| 349 |
+
"## Predire il diabete"
|
| 350 |
+
]
|
| 351 |
+
},
|
| 352 |
+
{
|
| 353 |
+
"cell_type": "markdown",
|
| 354 |
+
"metadata": {},
|
| 355 |
+
"source": [
|
| 356 |
+
"Utilizziamo il dataset `pima-indians-diabetes.data.csv` per provare a predire l'insorgenza di diabete nei pazienti in esso decritti."
|
| 357 |
+
]
|
| 358 |
+
},
|
| 359 |
+
{
|
| 360 |
+
"cell_type": "code",
|
| 361 |
+
"execution_count": 127,
|
| 362 |
+
"metadata": {},
|
| 363 |
+
"outputs": [
|
| 364 |
+
{
|
| 365 |
+
"data": {
|
| 366 |
+
"text/html": [
|
| 367 |
+
"<div>\n",
|
| 368 |
+
"<style scoped>\n",
|
| 369 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 370 |
+
" vertical-align: middle;\n",
|
| 371 |
+
" }\n",
|
| 372 |
+
"\n",
|
| 373 |
+
" .dataframe tbody tr th {\n",
|
| 374 |
+
" vertical-align: top;\n",
|
| 375 |
+
" }\n",
|
| 376 |
+
"\n",
|
| 377 |
+
" .dataframe thead th {\n",
|
| 378 |
+
" text-align: right;\n",
|
| 379 |
+
" }\n",
|
| 380 |
+
"</style>\n",
|
| 381 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 382 |
+
" <thead>\n",
|
| 383 |
+
" <tr style=\"text-align: right;\">\n",
|
| 384 |
+
" <th></th>\n",
|
| 385 |
+
" <th>Pregnancies</th>\n",
|
| 386 |
+
" <th>Glucose</th>\n",
|
| 387 |
+
" <th>BloodPressure</th>\n",
|
| 388 |
+
" <th>SkinThickness</th>\n",
|
| 389 |
+
" <th>Insulin</th>\n",
|
| 390 |
+
" <th>BMI</th>\n",
|
| 391 |
+
" <th>DiabetePedigreeFunction</th>\n",
|
| 392 |
+
" <th>Age</th>\n",
|
| 393 |
+
" <th>Class</th>\n",
|
| 394 |
+
" </tr>\n",
|
| 395 |
+
" </thead>\n",
|
| 396 |
+
" <tbody>\n",
|
| 397 |
+
" <tr>\n",
|
| 398 |
+
" <th>0</th>\n",
|
| 399 |
+
" <td>6</td>\n",
|
| 400 |
+
" <td>148</td>\n",
|
| 401 |
+
" <td>72</td>\n",
|
| 402 |
+
" <td>35</td>\n",
|
| 403 |
+
" <td>0</td>\n",
|
| 404 |
+
" <td>33.6</td>\n",
|
| 405 |
+
" <td>0.627</td>\n",
|
| 406 |
+
" <td>50</td>\n",
|
| 407 |
+
" <td>1</td>\n",
|
| 408 |
+
" </tr>\n",
|
| 409 |
+
" <tr>\n",
|
| 410 |
+
" <th>1</th>\n",
|
| 411 |
+
" <td>1</td>\n",
|
| 412 |
+
" <td>85</td>\n",
|
| 413 |
+
" <td>66</td>\n",
|
| 414 |
+
" <td>29</td>\n",
|
| 415 |
+
" <td>0</td>\n",
|
| 416 |
+
" <td>26.6</td>\n",
|
| 417 |
+
" <td>0.351</td>\n",
|
| 418 |
+
" <td>31</td>\n",
|
| 419 |
+
" <td>0</td>\n",
|
| 420 |
+
" </tr>\n",
|
| 421 |
+
" <tr>\n",
|
| 422 |
+
" <th>2</th>\n",
|
| 423 |
+
" <td>8</td>\n",
|
| 424 |
+
" <td>183</td>\n",
|
| 425 |
+
" <td>64</td>\n",
|
| 426 |
+
" <td>0</td>\n",
|
| 427 |
+
" <td>0</td>\n",
|
| 428 |
+
" <td>23.3</td>\n",
|
| 429 |
+
" <td>0.672</td>\n",
|
| 430 |
+
" <td>32</td>\n",
|
| 431 |
+
" <td>1</td>\n",
|
| 432 |
+
" </tr>\n",
|
| 433 |
+
" <tr>\n",
|
| 434 |
+
" <th>3</th>\n",
|
| 435 |
+
" <td>1</td>\n",
|
| 436 |
+
" <td>89</td>\n",
|
| 437 |
+
" <td>66</td>\n",
|
| 438 |
+
" <td>23</td>\n",
|
| 439 |
+
" <td>94</td>\n",
|
| 440 |
+
" <td>28.1</td>\n",
|
| 441 |
+
" <td>0.167</td>\n",
|
| 442 |
+
" <td>21</td>\n",
|
| 443 |
+
" <td>0</td>\n",
|
| 444 |
+
" </tr>\n",
|
| 445 |
+
" <tr>\n",
|
| 446 |
+
" <th>4</th>\n",
|
| 447 |
+
" <td>0</td>\n",
|
| 448 |
+
" <td>137</td>\n",
|
| 449 |
+
" <td>40</td>\n",
|
| 450 |
+
" <td>35</td>\n",
|
| 451 |
+
" <td>168</td>\n",
|
| 452 |
+
" <td>43.1</td>\n",
|
| 453 |
+
" <td>2.288</td>\n",
|
| 454 |
+
" <td>33</td>\n",
|
| 455 |
+
" <td>1</td>\n",
|
| 456 |
+
" </tr>\n",
|
| 457 |
+
" <tr>\n",
|
| 458 |
+
" <th>...</th>\n",
|
| 459 |
+
" <td>...</td>\n",
|
| 460 |
+
" <td>...</td>\n",
|
| 461 |
+
" <td>...</td>\n",
|
| 462 |
+
" <td>...</td>\n",
|
| 463 |
+
" <td>...</td>\n",
|
| 464 |
+
" <td>...</td>\n",
|
| 465 |
+
" <td>...</td>\n",
|
| 466 |
+
" <td>...</td>\n",
|
| 467 |
+
" <td>...</td>\n",
|
| 468 |
+
" </tr>\n",
|
| 469 |
+
" <tr>\n",
|
| 470 |
+
" <th>763</th>\n",
|
| 471 |
+
" <td>10</td>\n",
|
| 472 |
+
" <td>101</td>\n",
|
| 473 |
+
" <td>76</td>\n",
|
| 474 |
+
" <td>48</td>\n",
|
| 475 |
+
" <td>180</td>\n",
|
| 476 |
+
" <td>32.9</td>\n",
|
| 477 |
+
" <td>0.171</td>\n",
|
| 478 |
+
" <td>63</td>\n",
|
| 479 |
+
" <td>0</td>\n",
|
| 480 |
+
" </tr>\n",
|
| 481 |
+
" <tr>\n",
|
| 482 |
+
" <th>764</th>\n",
|
| 483 |
+
" <td>2</td>\n",
|
| 484 |
+
" <td>122</td>\n",
|
| 485 |
+
" <td>70</td>\n",
|
| 486 |
+
" <td>27</td>\n",
|
| 487 |
+
" <td>0</td>\n",
|
| 488 |
+
" <td>36.8</td>\n",
|
| 489 |
+
" <td>0.340</td>\n",
|
| 490 |
+
" <td>27</td>\n",
|
| 491 |
+
" <td>0</td>\n",
|
| 492 |
+
" </tr>\n",
|
| 493 |
+
" <tr>\n",
|
| 494 |
+
" <th>765</th>\n",
|
| 495 |
+
" <td>5</td>\n",
|
| 496 |
+
" <td>121</td>\n",
|
| 497 |
+
" <td>72</td>\n",
|
| 498 |
+
" <td>23</td>\n",
|
| 499 |
+
" <td>112</td>\n",
|
| 500 |
+
" <td>26.2</td>\n",
|
| 501 |
+
" <td>0.245</td>\n",
|
| 502 |
+
" <td>30</td>\n",
|
| 503 |
+
" <td>0</td>\n",
|
| 504 |
+
" </tr>\n",
|
| 505 |
+
" <tr>\n",
|
| 506 |
+
" <th>766</th>\n",
|
| 507 |
+
" <td>1</td>\n",
|
| 508 |
+
" <td>126</td>\n",
|
| 509 |
+
" <td>60</td>\n",
|
| 510 |
+
" <td>0</td>\n",
|
| 511 |
+
" <td>0</td>\n",
|
| 512 |
+
" <td>30.1</td>\n",
|
| 513 |
+
" <td>0.349</td>\n",
|
| 514 |
+
" <td>47</td>\n",
|
| 515 |
+
" <td>1</td>\n",
|
| 516 |
+
" </tr>\n",
|
| 517 |
+
" <tr>\n",
|
| 518 |
+
" <th>767</th>\n",
|
| 519 |
+
" <td>1</td>\n",
|
| 520 |
+
" <td>93</td>\n",
|
| 521 |
+
" <td>70</td>\n",
|
| 522 |
+
" <td>31</td>\n",
|
| 523 |
+
" <td>0</td>\n",
|
| 524 |
+
" <td>30.4</td>\n",
|
| 525 |
+
" <td>0.315</td>\n",
|
| 526 |
+
" <td>23</td>\n",
|
| 527 |
+
" <td>0</td>\n",
|
| 528 |
+
" </tr>\n",
|
| 529 |
+
" </tbody>\n",
|
| 530 |
+
"</table>\n",
|
| 531 |
+
"<p>768 rows × 9 columns</p>\n",
|
| 532 |
+
"</div>"
|
| 533 |
+
],
|
| 534 |
+
"text/plain": [
|
| 535 |
+
" Pregnancies Glucose BloodPressure SkinThickness Insulin BMI \\\n",
|
| 536 |
+
"0 6 148 72 35 0 33.6 \n",
|
| 537 |
+
"1 1 85 66 29 0 26.6 \n",
|
| 538 |
+
"2 8 183 64 0 0 23.3 \n",
|
| 539 |
+
"3 1 89 66 23 94 28.1 \n",
|
| 540 |
+
"4 0 137 40 35 168 43.1 \n",
|
| 541 |
+
".. ... ... ... ... ... ... \n",
|
| 542 |
+
"763 10 101 76 48 180 32.9 \n",
|
| 543 |
+
"764 2 122 70 27 0 36.8 \n",
|
| 544 |
+
"765 5 121 72 23 112 26.2 \n",
|
| 545 |
+
"766 1 126 60 0 0 30.1 \n",
|
| 546 |
+
"767 1 93 70 31 0 30.4 \n",
|
| 547 |
+
"\n",
|
| 548 |
+
" DiabetePedigreeFunction Age Class \n",
|
| 549 |
+
"0 0.627 50 1 \n",
|
| 550 |
+
"1 0.351 31 0 \n",
|
| 551 |
+
"2 0.672 32 1 \n",
|
| 552 |
+
"3 0.167 21 0 \n",
|
| 553 |
+
"4 2.288 33 1 \n",
|
| 554 |
+
".. ... ... ... \n",
|
| 555 |
+
"763 0.171 63 0 \n",
|
| 556 |
+
"764 0.340 27 0 \n",
|
| 557 |
+
"765 0.245 30 0 \n",
|
| 558 |
+
"766 0.349 47 1 \n",
|
| 559 |
+
"767 0.315 23 0 \n",
|
| 560 |
+
"\n",
|
| 561 |
+
"[768 rows x 9 columns]"
|
| 562 |
+
]
|
| 563 |
+
},
|
| 564 |
+
"execution_count": 127,
|
| 565 |
+
"metadata": {},
|
| 566 |
+
"output_type": "execute_result"
|
| 567 |
+
}
|
| 568 |
+
],
|
| 569 |
+
"source": [
|
| 570 |
+
"import pandas as pd\n",
|
| 571 |
+
"from xgboost import XGBClassifier\n",
|
| 572 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 573 |
+
"from sklearn.metrics import accuracy_score\n",
|
| 574 |
+
"\n",
|
| 575 |
+
"dataset = pd.read_csv('data/pima-indians-diabetes.csv')\n",
|
| 576 |
+
"dataset"
|
| 577 |
+
]
|
| 578 |
+
},
|
| 579 |
+
{
|
| 580 |
+
"cell_type": "code",
|
| 581 |
+
"execution_count": 128,
|
| 582 |
+
"metadata": {},
|
| 583 |
+
"outputs": [],
|
| 584 |
+
"source": []
|
| 585 |
+
},
|
| 586 |
+
{
|
| 587 |
+
"cell_type": "markdown",
|
| 588 |
+
"metadata": {},
|
| 589 |
+
"source": [
|
| 590 |
+
"<a id='section3'></a>\n",
|
| 591 |
+
"## Crime in Chicago"
|
| 592 |
+
]
|
| 593 |
+
},
|
| 594 |
+
{
|
| 595 |
+
"cell_type": "markdown",
|
| 596 |
+
"metadata": {},
|
| 597 |
+
"source": [
|
| 598 |
+
"Leggiamo i dati dei crimini accaduti a Chicago con le coordinate di latitudine e longitudine e vediamo come è possibile raggrupparli in diverse categorie in base alla posizione dove i crimini sono avvenuti."
|
| 599 |
+
]
|
| 600 |
+
},
|
| 601 |
+
{
|
| 602 |
+
"cell_type": "code",
|
| 603 |
+
"execution_count": 1,
|
| 604 |
+
"metadata": {},
|
| 605 |
+
"outputs": [
|
| 606 |
+
{
|
| 607 |
+
"name": "stderr",
|
| 608 |
+
"output_type": "stream",
|
| 609 |
+
"text": [
|
| 610 |
+
"<frozen importlib._bootstrap>:228: RuntimeWarning: scipy._lib.messagestream.MessageStream size changed, may indicate binary incompatibility. Expected 56 from C header, got 64 from PyObject\n"
|
| 611 |
+
]
|
| 612 |
+
}
|
| 613 |
+
],
|
| 614 |
+
"source": [
|
| 615 |
+
"import math\n",
|
| 616 |
+
"import numpy as np\n",
|
| 617 |
+
"import pandas as pd\n",
|
| 618 |
+
"import matplotlib.pyplot as plt\n",
|
| 619 |
+
"import matplotlib\n",
|
| 620 |
+
"\n",
|
| 621 |
+
"from sklearn.cluster import KMeans"
|
| 622 |
+
]
|
| 623 |
+
},
|
| 624 |
+
{
|
| 625 |
+
"cell_type": "code",
|
| 626 |
+
"execution_count": 2,
|
| 627 |
+
"metadata": {},
|
| 628 |
+
"outputs": [
|
| 629 |
+
{
|
| 630 |
+
"data": {
|
| 631 |
+
"text/html": [
|
| 632 |
+
"<div>\n",
|
| 633 |
+
"<style scoped>\n",
|
| 634 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 635 |
+
" vertical-align: middle;\n",
|
| 636 |
+
" }\n",
|
| 637 |
+
"\n",
|
| 638 |
+
" .dataframe tbody tr th {\n",
|
| 639 |
+
" vertical-align: top;\n",
|
| 640 |
+
" }\n",
|
| 641 |
+
"\n",
|
| 642 |
+
" .dataframe thead th {\n",
|
| 643 |
+
" text-align: right;\n",
|
| 644 |
+
" }\n",
|
| 645 |
+
"</style>\n",
|
| 646 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 647 |
+
" <thead>\n",
|
| 648 |
+
" <tr style=\"text-align: right;\">\n",
|
| 649 |
+
" <th></th>\n",
|
| 650 |
+
" <th>ID</th>\n",
|
| 651 |
+
" <th>Case Number</th>\n",
|
| 652 |
+
" <th>Date</th>\n",
|
| 653 |
+
" <th>Block</th>\n",
|
| 654 |
+
" <th>IUCR</th>\n",
|
| 655 |
+
" <th>Primary Type</th>\n",
|
| 656 |
+
" <th>Description</th>\n",
|
| 657 |
+
" <th>Location Description</th>\n",
|
| 658 |
+
" <th>Arrest</th>\n",
|
| 659 |
+
" <th>Domestic</th>\n",
|
| 660 |
+
" <th>...</th>\n",
|
| 661 |
+
" <th>Ward</th>\n",
|
| 662 |
+
" <th>Community Area</th>\n",
|
| 663 |
+
" <th>FBI Code</th>\n",
|
| 664 |
+
" <th>X Coordinate</th>\n",
|
| 665 |
+
" <th>Y Coordinate</th>\n",
|
| 666 |
+
" <th>Year</th>\n",
|
| 667 |
+
" <th>Updated On</th>\n",
|
| 668 |
+
" <th>Latitude</th>\n",
|
| 669 |
+
" <th>Longitude</th>\n",
|
| 670 |
+
" <th>Location</th>\n",
|
| 671 |
+
" </tr>\n",
|
| 672 |
+
" </thead>\n",
|
| 673 |
+
" <tbody>\n",
|
| 674 |
+
" <tr>\n",
|
| 675 |
+
" <th>0</th>\n",
|
| 676 |
+
" <td>10002677</td>\n",
|
| 677 |
+
" <td>HY191890</td>\n",
|
| 678 |
+
" <td>2015-03-20 13:13:00</td>\n",
|
| 679 |
+
" <td>020XX W 70TH ST</td>\n",
|
| 680 |
+
" <td>1661</td>\n",
|
| 681 |
+
" <td>GAMBLING</td>\n",
|
| 682 |
+
" <td>GAME/DICE</td>\n",
|
| 683 |
+
" <td>STREET</td>\n",
|
| 684 |
+
" <td>True</td>\n",
|
| 685 |
+
" <td>False</td>\n",
|
| 686 |
+
" <td>...</td>\n",
|
| 687 |
+
" <td>17.0</td>\n",
|
| 688 |
+
" <td>67.0</td>\n",
|
| 689 |
+
" <td>19</td>\n",
|
| 690 |
+
" <td>1164027.0</td>\n",
|
| 691 |
+
" <td>1858234.0</td>\n",
|
| 692 |
+
" <td>2015</td>\n",
|
| 693 |
+
" <td>2018-02-10 15:50:01</td>\n",
|
| 694 |
+
" <td>41.766615</td>\n",
|
| 695 |
+
" <td>-87.674337</td>\n",
|
| 696 |
+
" <td>(41.766615006, -87.674337321)</td>\n",
|
| 697 |
+
" </tr>\n",
|
| 698 |
+
" <tr>\n",
|
| 699 |
+
" <th>1</th>\n",
|
| 700 |
+
" <td>10003802</td>\n",
|
| 701 |
+
" <td>HY193541</td>\n",
|
| 702 |
+
" <td>2015-03-21 18:45:00</td>\n",
|
| 703 |
+
" <td>042XX W MONROE ST</td>\n",
|
| 704 |
+
" <td>1661</td>\n",
|
| 705 |
+
" <td>GAMBLING</td>\n",
|
| 706 |
+
" <td>GAME/DICE</td>\n",
|
| 707 |
+
" <td>SIDEWALK</td>\n",
|
| 708 |
+
" <td>True</td>\n",
|
| 709 |
+
" <td>False</td>\n",
|
| 710 |
+
" <td>...</td>\n",
|
| 711 |
+
" <td>28.0</td>\n",
|
| 712 |
+
" <td>26.0</td>\n",
|
| 713 |
+
" <td>19</td>\n",
|
| 714 |
+
" <td>1148136.0</td>\n",
|
| 715 |
+
" <td>1899324.0</td>\n",
|
| 716 |
+
" <td>2015</td>\n",
|
| 717 |
+
" <td>2018-02-10 15:50:01</td>\n",
|
| 718 |
+
" <td>41.879691</td>\n",
|
| 719 |
+
" <td>-87.731530</td>\n",
|
| 720 |
+
" <td>(41.879691121, -87.731529667)</td>\n",
|
| 721 |
+
" </tr>\n",
|
| 722 |
+
" <tr>\n",
|
| 723 |
+
" <th>2</th>\n",
|
| 724 |
+
" <td>10003937</td>\n",
|
| 725 |
+
" <td>HY193663</td>\n",
|
| 726 |
+
" <td>2015-03-21 19:55:00</td>\n",
|
| 727 |
+
" <td>032XX W LAWRENCE AVE</td>\n",
|
| 728 |
+
" <td>1670</td>\n",
|
| 729 |
+
" <td>GAMBLING</td>\n",
|
| 730 |
+
" <td>GAME/AMUSEMENT DEVICE</td>\n",
|
| 731 |
+
" <td>SMALL RETAIL STORE</td>\n",
|
| 732 |
+
" <td>True</td>\n",
|
| 733 |
+
" <td>False</td>\n",
|
| 734 |
+
" <td>...</td>\n",
|
| 735 |
+
" <td>39.0</td>\n",
|
| 736 |
+
" <td>14.0</td>\n",
|
| 737 |
+
" <td>19</td>\n",
|
| 738 |
+
" <td>1154071.0</td>\n",
|
| 739 |
+
" <td>1931710.0</td>\n",
|
| 740 |
+
" <td>2015</td>\n",
|
| 741 |
+
" <td>2018-02-10 15:50:01</td>\n",
|
| 742 |
+
" <td>41.968445</td>\n",
|
| 743 |
+
" <td>-87.708871</td>\n",
|
| 744 |
+
" <td>(41.968444682, -87.708871059)</td>\n",
|
| 745 |
+
" </tr>\n",
|
| 746 |
+
" <tr>\n",
|
| 747 |
+
" <th>3</th>\n",
|
| 748 |
+
" <td>10004429</td>\n",
|
| 749 |
+
" <td>HY194332</td>\n",
|
| 750 |
+
" <td>2015-03-22 13:57:00</td>\n",
|
| 751 |
+
" <td>019XX S ST LOUIS AVE</td>\n",
|
| 752 |
+
" <td>1661</td>\n",
|
| 753 |
+
" <td>GAMBLING</td>\n",
|
| 754 |
+
" <td>GAME/DICE</td>\n",
|
| 755 |
+
" <td>SIDEWALK</td>\n",
|
| 756 |
+
" <td>True</td>\n",
|
| 757 |
+
" <td>False</td>\n",
|
| 758 |
+
" <td>...</td>\n",
|
| 759 |
+
" <td>24.0</td>\n",
|
| 760 |
+
" <td>29.0</td>\n",
|
| 761 |
+
" <td>19</td>\n",
|
| 762 |
+
" <td>1153407.0</td>\n",
|
| 763 |
+
" <td>1890195.0</td>\n",
|
| 764 |
+
" <td>2015</td>\n",
|
| 765 |
+
" <td>2018-02-10 15:50:01</td>\n",
|
| 766 |
+
" <td>41.854537</td>\n",
|
| 767 |
+
" <td>-87.712418</td>\n",
|
| 768 |
+
" <td>(41.854537081, -87.71241763)</td>\n",
|
| 769 |
+
" </tr>\n",
|
| 770 |
+
" <tr>\n",
|
| 771 |
+
" <th>4</th>\n",
|
| 772 |
+
" <td>10005837</td>\n",
|
| 773 |
+
" <td>HY195964</td>\n",
|
| 774 |
+
" <td>2015-03-23 20:40:00</td>\n",
|
| 775 |
+
" <td>039XX W CERMAK RD</td>\n",
|
| 776 |
+
" <td>1661</td>\n",
|
| 777 |
+
" <td>GAMBLING</td>\n",
|
| 778 |
+
" <td>GAME/DICE</td>\n",
|
| 779 |
+
" <td>PARKING LOT/GARAGE(NON.RESID.)</td>\n",
|
| 780 |
+
" <td>True</td>\n",
|
| 781 |
+
" <td>False</td>\n",
|
| 782 |
+
" <td>...</td>\n",
|
| 783 |
+
" <td>24.0</td>\n",
|
| 784 |
+
" <td>29.0</td>\n",
|
| 785 |
+
" <td>19</td>\n",
|
| 786 |
+
" <td>1150290.0</td>\n",
|
| 787 |
+
" <td>1889088.0</td>\n",
|
| 788 |
+
" <td>2015</td>\n",
|
| 789 |
+
" <td>2018-02-10 15:50:01</td>\n",
|
| 790 |
+
" <td>41.851561</td>\n",
|
| 791 |
+
" <td>-87.723887</td>\n",
|
| 792 |
+
" <td>(41.851560618, -87.723887245)</td>\n",
|
| 793 |
+
" </tr>\n",
|
| 794 |
+
" </tbody>\n",
|
| 795 |
+
"</table>\n",
|
| 796 |
+
"<p>5 rows × 22 columns</p>\n",
|
| 797 |
+
"</div>"
|
| 798 |
+
],
|
| 799 |
+
"text/plain": [
|
| 800 |
+
" ID Case Number Date Block IUCR \\\n",
|
| 801 |
+
"0 10002677 HY191890 2015-03-20 13:13:00 020XX W 70TH ST 1661 \n",
|
| 802 |
+
"1 10003802 HY193541 2015-03-21 18:45:00 042XX W MONROE ST 1661 \n",
|
| 803 |
+
"2 10003937 HY193663 2015-03-21 19:55:00 032XX W LAWRENCE AVE 1670 \n",
|
| 804 |
+
"3 10004429 HY194332 2015-03-22 13:57:00 019XX S ST LOUIS AVE 1661 \n",
|
| 805 |
+
"4 10005837 HY195964 2015-03-23 20:40:00 039XX W CERMAK RD 1661 \n",
|
| 806 |
+
"\n",
|
| 807 |
+
" Primary Type Description Location Description Arrest \\\n",
|
| 808 |
+
"0 GAMBLING GAME/DICE STREET True \n",
|
| 809 |
+
"1 GAMBLING GAME/DICE SIDEWALK True \n",
|
| 810 |
+
"2 GAMBLING GAME/AMUSEMENT DEVICE SMALL RETAIL STORE True \n",
|
| 811 |
+
"3 GAMBLING GAME/DICE SIDEWALK True \n",
|
| 812 |
+
"4 GAMBLING GAME/DICE PARKING LOT/GARAGE(NON.RESID.) True \n",
|
| 813 |
+
"\n",
|
| 814 |
+
" Domestic ... Ward Community Area FBI Code X Coordinate Y Coordinate \\\n",
|
| 815 |
+
"0 False ... 17.0 67.0 19 1164027.0 1858234.0 \n",
|
| 816 |
+
"1 False ... 28.0 26.0 19 1148136.0 1899324.0 \n",
|
| 817 |
+
"2 False ... 39.0 14.0 19 1154071.0 1931710.0 \n",
|
| 818 |
+
"3 False ... 24.0 29.0 19 1153407.0 1890195.0 \n",
|
| 819 |
+
"4 False ... 24.0 29.0 19 1150290.0 1889088.0 \n",
|
| 820 |
+
"\n",
|
| 821 |
+
" Year Updated On Latitude Longitude \\\n",
|
| 822 |
+
"0 2015 2018-02-10 15:50:01 41.766615 -87.674337 \n",
|
| 823 |
+
"1 2015 2018-02-10 15:50:01 41.879691 -87.731530 \n",
|
| 824 |
+
"2 2015 2018-02-10 15:50:01 41.968445 -87.708871 \n",
|
| 825 |
+
"3 2015 2018-02-10 15:50:01 41.854537 -87.712418 \n",
|
| 826 |
+
"4 2015 2018-02-10 15:50:01 41.851561 -87.723887 \n",
|
| 827 |
+
"\n",
|
| 828 |
+
" Location \n",
|
| 829 |
+
"0 (41.766615006, -87.674337321) \n",
|
| 830 |
+
"1 (41.879691121, -87.731529667) \n",
|
| 831 |
+
"2 (41.968444682, -87.708871059) \n",
|
| 832 |
+
"3 (41.854537081, -87.71241763) \n",
|
| 833 |
+
"4 (41.851560618, -87.723887245) \n",
|
| 834 |
+
"\n",
|
| 835 |
+
"[5 rows x 22 columns]"
|
| 836 |
+
]
|
| 837 |
+
},
|
| 838 |
+
"execution_count": 2,
|
| 839 |
+
"metadata": {},
|
| 840 |
+
"output_type": "execute_result"
|
| 841 |
+
}
|
| 842 |
+
],
|
| 843 |
+
"source": [
|
| 844 |
+
"df = pd.read_csv('data/Crimes_-_2001_to_present.csv', header = 0)\n",
|
| 845 |
+
"#Ripuliamo i dati eliminando i dati N/A e formattando i campi data\n",
|
| 846 |
+
"df = df.dropna(axis=0)\n",
|
| 847 |
+
"df.Date = pd.to_datetime(df.Date, errors='coerce')\n",
|
| 848 |
+
"df['Updated On'] = pd.to_datetime(df['Updated On'], errors='coerce')\n",
|
| 849 |
+
"df.head()"
|
| 850 |
+
]
|
| 851 |
+
},
|
| 852 |
+
{
|
| 853 |
+
"cell_type": "code",
|
| 854 |
+
"execution_count": null,
|
| 855 |
+
"metadata": {},
|
| 856 |
+
"outputs": [],
|
| 857 |
+
"source": []
|
| 858 |
+
}
|
| 859 |
+
],
|
| 860 |
+
"metadata": {
|
| 861 |
+
"kernelspec": {
|
| 862 |
+
"display_name": "Python 3 (ipykernel)",
|
| 863 |
+
"language": "python",
|
| 864 |
+
"name": "python3"
|
| 865 |
+
},
|
| 866 |
+
"language_info": {
|
| 867 |
+
"codemirror_mode": {
|
| 868 |
+
"name": "ipython",
|
| 869 |
+
"version": 3
|
| 870 |
+
},
|
| 871 |
+
"file_extension": ".py",
|
| 872 |
+
"mimetype": "text/x-python",
|
| 873 |
+
"name": "python",
|
| 874 |
+
"nbconvert_exporter": "python",
|
| 875 |
+
"pygments_lexer": "ipython3",
|
| 876 |
+
"version": "3.10.6"
|
| 877 |
+
},
|
| 878 |
+
"nbTranslate": {
|
| 879 |
+
"displayLangs": [
|
| 880 |
+
"*"
|
| 881 |
+
],
|
| 882 |
+
"hotkey": "alt-t",
|
| 883 |
+
"langInMainMenu": true,
|
| 884 |
+
"sourceLang": "en",
|
| 885 |
+
"targetLang": "fr",
|
| 886 |
+
"useGoogleTranslate": true
|
| 887 |
+
}
|
| 888 |
+
},
|
| 889 |
+
"nbformat": 4,
|
| 890 |
+
"nbformat_minor": 2
|
| 891 |
+
}
|
Lezione 2.S - Soluzioni.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Lezione 3.A - Clustering e anomaly detection.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Lezione 3.E - Esercizi.ipynb
ADDED
|
@@ -0,0 +1,480 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"<a id='start'></a>\n",
|
| 8 |
+
"# Esercizi Clustering e anomaly detection\n",
|
| 9 |
+
"\n",
|
| 10 |
+
"In questo notebook vengono presentati degli esercizi sui Decision Tree.\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"Provate a svolgere il seguente esercizio:<br>\n",
|
| 13 |
+
"1) [Cluster e elbow method](#section1)<br>\n",
|
| 14 |
+
"2) [Segmentazione delle specie](#section2)<br>\n",
|
| 15 |
+
"3) [Chicchi di frumento](#section3)<br>\n",
|
| 16 |
+
"4) [Vendite ti pesce gatto](#section4)<br>"
|
| 17 |
+
]
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "markdown",
|
| 21 |
+
"metadata": {},
|
| 22 |
+
"source": [
|
| 23 |
+
"<a id='section1'></a>\n",
|
| 24 |
+
"## Cluster e elbow method"
|
| 25 |
+
]
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"cell_type": "markdown",
|
| 29 |
+
"metadata": {},
|
| 30 |
+
"source": [
|
| 31 |
+
"Proviamo a predere un dataset con tutti i paesi e la loro latitudine e longitudine e a effettuarne una clusterizzazione:"
|
| 32 |
+
]
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"cell_type": "code",
|
| 36 |
+
"execution_count": 1,
|
| 37 |
+
"metadata": {},
|
| 38 |
+
"outputs": [
|
| 39 |
+
{
|
| 40 |
+
"data": {
|
| 41 |
+
"text/html": [
|
| 42 |
+
"<div>\n",
|
| 43 |
+
"<style scoped>\n",
|
| 44 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 45 |
+
" vertical-align: middle;\n",
|
| 46 |
+
" }\n",
|
| 47 |
+
"\n",
|
| 48 |
+
" .dataframe tbody tr th {\n",
|
| 49 |
+
" vertical-align: top;\n",
|
| 50 |
+
" }\n",
|
| 51 |
+
"\n",
|
| 52 |
+
" .dataframe thead th {\n",
|
| 53 |
+
" text-align: right;\n",
|
| 54 |
+
" }\n",
|
| 55 |
+
"</style>\n",
|
| 56 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 57 |
+
" <thead>\n",
|
| 58 |
+
" <tr style=\"text-align: right;\">\n",
|
| 59 |
+
" <th></th>\n",
|
| 60 |
+
" <th>name</th>\n",
|
| 61 |
+
" <th>Longitude</th>\n",
|
| 62 |
+
" <th>Latitude</th>\n",
|
| 63 |
+
" </tr>\n",
|
| 64 |
+
" </thead>\n",
|
| 65 |
+
" <tbody>\n",
|
| 66 |
+
" <tr>\n",
|
| 67 |
+
" <th>0</th>\n",
|
| 68 |
+
" <td>Aruba</td>\n",
|
| 69 |
+
" <td>-69.982677</td>\n",
|
| 70 |
+
" <td>12.520880</td>\n",
|
| 71 |
+
" </tr>\n",
|
| 72 |
+
" <tr>\n",
|
| 73 |
+
" <th>1</th>\n",
|
| 74 |
+
" <td>Afghanistan</td>\n",
|
| 75 |
+
" <td>66.004734</td>\n",
|
| 76 |
+
" <td>33.835231</td>\n",
|
| 77 |
+
" </tr>\n",
|
| 78 |
+
" <tr>\n",
|
| 79 |
+
" <th>2</th>\n",
|
| 80 |
+
" <td>Angola</td>\n",
|
| 81 |
+
" <td>17.537368</td>\n",
|
| 82 |
+
" <td>-12.293361</td>\n",
|
| 83 |
+
" </tr>\n",
|
| 84 |
+
" <tr>\n",
|
| 85 |
+
" <th>3</th>\n",
|
| 86 |
+
" <td>Anguilla</td>\n",
|
| 87 |
+
" <td>-63.064989</td>\n",
|
| 88 |
+
" <td>18.223959</td>\n",
|
| 89 |
+
" </tr>\n",
|
| 90 |
+
" <tr>\n",
|
| 91 |
+
" <th>4</th>\n",
|
| 92 |
+
" <td>Albania</td>\n",
|
| 93 |
+
" <td>20.049834</td>\n",
|
| 94 |
+
" <td>41.142450</td>\n",
|
| 95 |
+
" </tr>\n",
|
| 96 |
+
" </tbody>\n",
|
| 97 |
+
"</table>\n",
|
| 98 |
+
"</div>"
|
| 99 |
+
],
|
| 100 |
+
"text/plain": [
|
| 101 |
+
" name Longitude Latitude\n",
|
| 102 |
+
"0 Aruba -69.982677 12.520880\n",
|
| 103 |
+
"1 Afghanistan 66.004734 33.835231\n",
|
| 104 |
+
"2 Angola 17.537368 -12.293361\n",
|
| 105 |
+
"3 Anguilla -63.064989 18.223959\n",
|
| 106 |
+
"4 Albania 20.049834 41.142450"
|
| 107 |
+
]
|
| 108 |
+
},
|
| 109 |
+
"execution_count": 1,
|
| 110 |
+
"metadata": {},
|
| 111 |
+
"output_type": "execute_result"
|
| 112 |
+
}
|
| 113 |
+
],
|
| 114 |
+
"source": [
|
| 115 |
+
"import pandas as pd\n",
|
| 116 |
+
"data = pd.read_csv('data/Countries-exercise.csv')\n",
|
| 117 |
+
"data.head()"
|
| 118 |
+
]
|
| 119 |
+
},
|
| 120 |
+
{
|
| 121 |
+
"cell_type": "code",
|
| 122 |
+
"execution_count": null,
|
| 123 |
+
"metadata": {},
|
| 124 |
+
"outputs": [],
|
| 125 |
+
"source": []
|
| 126 |
+
},
|
| 127 |
+
{
|
| 128 |
+
"cell_type": "markdown",
|
| 129 |
+
"metadata": {},
|
| 130 |
+
"source": [
|
| 131 |
+
"<a id='section2'></a>\n",
|
| 132 |
+
"## Segmentazione delle specie"
|
| 133 |
+
]
|
| 134 |
+
},
|
| 135 |
+
{
|
| 136 |
+
"cell_type": "markdown",
|
| 137 |
+
"metadata": {},
|
| 138 |
+
"source": [
|
| 139 |
+
"Per questo esercizio utilizzeremo l'Iris flower dataset, che è uno dei dataset più popolari per gli esercizi di machine learning. Puoi leggere molte informazioni a riguardo a questo link: https://en.wikipedia.org/wiki/Iris_flower_data_set\n",
|
| 140 |
+
"\n",
|
| 141 |
+
"In questo dataset ci sono 4 feature: sepal length, sepal width, petal length, and petal width.\n",
|
| 142 |
+
"\n",
|
| 143 |
+
"Prova a disegnare i dati con 2, 3 e 5 cluster. Cosa osservi?\n",
|
| 144 |
+
"\n",
|
| 145 |
+
"Prova a identificare il numero ottimale di cluster e poi calcolalo col metodo elbow."
|
| 146 |
+
]
|
| 147 |
+
},
|
| 148 |
+
{
|
| 149 |
+
"cell_type": "code",
|
| 150 |
+
"execution_count": null,
|
| 151 |
+
"metadata": {},
|
| 152 |
+
"outputs": [],
|
| 153 |
+
"source": []
|
| 154 |
+
},
|
| 155 |
+
{
|
| 156 |
+
"cell_type": "markdown",
|
| 157 |
+
"metadata": {},
|
| 158 |
+
"source": [
|
| 159 |
+
"<a id='section3'></a>\n",
|
| 160 |
+
"## Chicchi di frumento"
|
| 161 |
+
]
|
| 162 |
+
},
|
| 163 |
+
{
|
| 164 |
+
"cell_type": "markdown",
|
| 165 |
+
"metadata": {},
|
| 166 |
+
"source": [
|
| 167 |
+
"Leggiamo il dataset che contiene misurazioni delle proprietà geometriche di chicchi appartenenti a tre diverse varietà di frumento. Una tecnica a raggi X morbidi e un pacchetto GRAINS sono stati utilizzati per costruire tutti e sette gli attributi a valore reale.\n",
|
| 168 |
+
"\n",
|
| 169 |
+
"Proviamo a clusterizzare questo dataset con i 2 metodi visti: KMeans e DBScan."
|
| 170 |
+
]
|
| 171 |
+
},
|
| 172 |
+
{
|
| 173 |
+
"cell_type": "code",
|
| 174 |
+
"execution_count": 1,
|
| 175 |
+
"metadata": {},
|
| 176 |
+
"outputs": [
|
| 177 |
+
{
|
| 178 |
+
"data": {
|
| 179 |
+
"text/html": [
|
| 180 |
+
"<div>\n",
|
| 181 |
+
"<style scoped>\n",
|
| 182 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 183 |
+
" vertical-align: middle;\n",
|
| 184 |
+
" }\n",
|
| 185 |
+
"\n",
|
| 186 |
+
" .dataframe tbody tr th {\n",
|
| 187 |
+
" vertical-align: top;\n",
|
| 188 |
+
" }\n",
|
| 189 |
+
"\n",
|
| 190 |
+
" .dataframe thead th {\n",
|
| 191 |
+
" text-align: right;\n",
|
| 192 |
+
" }\n",
|
| 193 |
+
"</style>\n",
|
| 194 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 195 |
+
" <thead>\n",
|
| 196 |
+
" <tr style=\"text-align: right;\">\n",
|
| 197 |
+
" <th></th>\n",
|
| 198 |
+
" <th>AREA</th>\n",
|
| 199 |
+
" <th>PERIMETER</th>\n",
|
| 200 |
+
" <th>COMPACTNESS</th>\n",
|
| 201 |
+
" <th>LENGTH</th>\n",
|
| 202 |
+
" <th>WIDTH</th>\n",
|
| 203 |
+
" <th>ASYMMETRY</th>\n",
|
| 204 |
+
" <th>GROOVE_LENGTH</th>\n",
|
| 205 |
+
" </tr>\n",
|
| 206 |
+
" </thead>\n",
|
| 207 |
+
" <tbody>\n",
|
| 208 |
+
" <tr>\n",
|
| 209 |
+
" <th>0</th>\n",
|
| 210 |
+
" <td>15.26</td>\n",
|
| 211 |
+
" <td>14.84</td>\n",
|
| 212 |
+
" <td>0.8710</td>\n",
|
| 213 |
+
" <td>5.763</td>\n",
|
| 214 |
+
" <td>3.312</td>\n",
|
| 215 |
+
" <td>2.221</td>\n",
|
| 216 |
+
" <td>5.220</td>\n",
|
| 217 |
+
" </tr>\n",
|
| 218 |
+
" <tr>\n",
|
| 219 |
+
" <th>1</th>\n",
|
| 220 |
+
" <td>14.88</td>\n",
|
| 221 |
+
" <td>14.57</td>\n",
|
| 222 |
+
" <td>0.8811</td>\n",
|
| 223 |
+
" <td>5.554</td>\n",
|
| 224 |
+
" <td>3.333</td>\n",
|
| 225 |
+
" <td>1.018</td>\n",
|
| 226 |
+
" <td>4.956</td>\n",
|
| 227 |
+
" </tr>\n",
|
| 228 |
+
" <tr>\n",
|
| 229 |
+
" <th>2</th>\n",
|
| 230 |
+
" <td>14.29</td>\n",
|
| 231 |
+
" <td>14.09</td>\n",
|
| 232 |
+
" <td>0.9050</td>\n",
|
| 233 |
+
" <td>5.291</td>\n",
|
| 234 |
+
" <td>3.337</td>\n",
|
| 235 |
+
" <td>2.699</td>\n",
|
| 236 |
+
" <td>4.825</td>\n",
|
| 237 |
+
" </tr>\n",
|
| 238 |
+
" <tr>\n",
|
| 239 |
+
" <th>3</th>\n",
|
| 240 |
+
" <td>13.84</td>\n",
|
| 241 |
+
" <td>13.94</td>\n",
|
| 242 |
+
" <td>0.8955</td>\n",
|
| 243 |
+
" <td>5.324</td>\n",
|
| 244 |
+
" <td>3.379</td>\n",
|
| 245 |
+
" <td>2.259</td>\n",
|
| 246 |
+
" <td>4.805</td>\n",
|
| 247 |
+
" </tr>\n",
|
| 248 |
+
" <tr>\n",
|
| 249 |
+
" <th>4</th>\n",
|
| 250 |
+
" <td>16.14</td>\n",
|
| 251 |
+
" <td>14.99</td>\n",
|
| 252 |
+
" <td>0.9034</td>\n",
|
| 253 |
+
" <td>5.658</td>\n",
|
| 254 |
+
" <td>3.562</td>\n",
|
| 255 |
+
" <td>1.355</td>\n",
|
| 256 |
+
" <td>5.175</td>\n",
|
| 257 |
+
" </tr>\n",
|
| 258 |
+
" </tbody>\n",
|
| 259 |
+
"</table>\n",
|
| 260 |
+
"</div>"
|
| 261 |
+
],
|
| 262 |
+
"text/plain": [
|
| 263 |
+
" AREA PERIMETER COMPACTNESS LENGTH WIDTH ASYMMETRY GROOVE_LENGTH\n",
|
| 264 |
+
"0 15.26 14.84 0.8710 5.763 3.312 2.221 5.220\n",
|
| 265 |
+
"1 14.88 14.57 0.8811 5.554 3.333 1.018 4.956\n",
|
| 266 |
+
"2 14.29 14.09 0.9050 5.291 3.337 2.699 4.825\n",
|
| 267 |
+
"3 13.84 13.94 0.8955 5.324 3.379 2.259 4.805\n",
|
| 268 |
+
"4 16.14 14.99 0.9034 5.658 3.562 1.355 5.175"
|
| 269 |
+
]
|
| 270 |
+
},
|
| 271 |
+
"execution_count": 1,
|
| 272 |
+
"metadata": {},
|
| 273 |
+
"output_type": "execute_result"
|
| 274 |
+
}
|
| 275 |
+
],
|
| 276 |
+
"source": [
|
| 277 |
+
"import pandas as pd\n",
|
| 278 |
+
"import numpy as np\n",
|
| 279 |
+
"\n",
|
| 280 |
+
"dataframe = pd.read_csv('data/seeds_dataset.txt',\n",
|
| 281 |
+
" header = None, \n",
|
| 282 |
+
" sep = \"\\t\",\n",
|
| 283 |
+
" on_bad_lines='skip', \n",
|
| 284 |
+
" usecols = range(7)\n",
|
| 285 |
+
" )\n",
|
| 286 |
+
"\n",
|
| 287 |
+
"dataframe.columns = [\"AREA\", \"PERIMETER\", \"COMPACTNESS\", \"LENGTH\", \"WIDTH\", \"ASYMMETRY\", \"GROOVE_LENGTH\"] \n",
|
| 288 |
+
"\n",
|
| 289 |
+
"dataframe.head()"
|
| 290 |
+
]
|
| 291 |
+
},
|
| 292 |
+
{
|
| 293 |
+
"cell_type": "code",
|
| 294 |
+
"execution_count": null,
|
| 295 |
+
"metadata": {},
|
| 296 |
+
"outputs": [],
|
| 297 |
+
"source": []
|
| 298 |
+
},
|
| 299 |
+
{
|
| 300 |
+
"cell_type": "markdown",
|
| 301 |
+
"metadata": {},
|
| 302 |
+
"source": [
|
| 303 |
+
"<a id='section4'></a>\n",
|
| 304 |
+
"## Vendite di pesce gatto"
|
| 305 |
+
]
|
| 306 |
+
},
|
| 307 |
+
{
|
| 308 |
+
"cell_type": "markdown",
|
| 309 |
+
"metadata": {},
|
| 310 |
+
"source": [
|
| 311 |
+
"Importiamo il set di dati contenente le vendite di pesce gatto dal 1986 al 2001.\n",
|
| 312 |
+
"Usiamo quindi degli algoritmi per identificare eventuali anomalie nelle vendite."
|
| 313 |
+
]
|
| 314 |
+
},
|
| 315 |
+
{
|
| 316 |
+
"cell_type": "code",
|
| 317 |
+
"execution_count": 1,
|
| 318 |
+
"metadata": {},
|
| 319 |
+
"outputs": [
|
| 320 |
+
{
|
| 321 |
+
"data": {
|
| 322 |
+
"text/html": [
|
| 323 |
+
"<div>\n",
|
| 324 |
+
"<style scoped>\n",
|
| 325 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 326 |
+
" vertical-align: middle;\n",
|
| 327 |
+
" }\n",
|
| 328 |
+
"\n",
|
| 329 |
+
" .dataframe tbody tr th {\n",
|
| 330 |
+
" vertical-align: top;\n",
|
| 331 |
+
" }\n",
|
| 332 |
+
"\n",
|
| 333 |
+
" .dataframe thead th {\n",
|
| 334 |
+
" text-align: right;\n",
|
| 335 |
+
" }\n",
|
| 336 |
+
"</style>\n",
|
| 337 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 338 |
+
" <thead>\n",
|
| 339 |
+
" <tr style=\"text-align: right;\">\n",
|
| 340 |
+
" <th></th>\n",
|
| 341 |
+
" <th>Date</th>\n",
|
| 342 |
+
" <th>Total</th>\n",
|
| 343 |
+
" </tr>\n",
|
| 344 |
+
" </thead>\n",
|
| 345 |
+
" <tbody>\n",
|
| 346 |
+
" <tr>\n",
|
| 347 |
+
" <th>0</th>\n",
|
| 348 |
+
" <td>1986-01-01</td>\n",
|
| 349 |
+
" <td>9034</td>\n",
|
| 350 |
+
" </tr>\n",
|
| 351 |
+
" <tr>\n",
|
| 352 |
+
" <th>1</th>\n",
|
| 353 |
+
" <td>1986-02-01</td>\n",
|
| 354 |
+
" <td>9596</td>\n",
|
| 355 |
+
" </tr>\n",
|
| 356 |
+
" <tr>\n",
|
| 357 |
+
" <th>2</th>\n",
|
| 358 |
+
" <td>1986-03-01</td>\n",
|
| 359 |
+
" <td>10558</td>\n",
|
| 360 |
+
" </tr>\n",
|
| 361 |
+
" <tr>\n",
|
| 362 |
+
" <th>3</th>\n",
|
| 363 |
+
" <td>1986-04-01</td>\n",
|
| 364 |
+
" <td>9002</td>\n",
|
| 365 |
+
" </tr>\n",
|
| 366 |
+
" <tr>\n",
|
| 367 |
+
" <th>4</th>\n",
|
| 368 |
+
" <td>1986-05-01</td>\n",
|
| 369 |
+
" <td>9239</td>\n",
|
| 370 |
+
" </tr>\n",
|
| 371 |
+
" <tr>\n",
|
| 372 |
+
" <th>...</th>\n",
|
| 373 |
+
" <td>...</td>\n",
|
| 374 |
+
" <td>...</td>\n",
|
| 375 |
+
" </tr>\n",
|
| 376 |
+
" <tr>\n",
|
| 377 |
+
" <th>319</th>\n",
|
| 378 |
+
" <td>2012-08-01</td>\n",
|
| 379 |
+
" <td>14442</td>\n",
|
| 380 |
+
" </tr>\n",
|
| 381 |
+
" <tr>\n",
|
| 382 |
+
" <th>320</th>\n",
|
| 383 |
+
" <td>2012-09-01</td>\n",
|
| 384 |
+
" <td>34722</td>\n",
|
| 385 |
+
" </tr>\n",
|
| 386 |
+
" <tr>\n",
|
| 387 |
+
" <th>321</th>\n",
|
| 388 |
+
" <td>2012-10-01</td>\n",
|
| 389 |
+
" <td>13795</td>\n",
|
| 390 |
+
" </tr>\n",
|
| 391 |
+
" <tr>\n",
|
| 392 |
+
" <th>322</th>\n",
|
| 393 |
+
" <td>2012-11-01</td>\n",
|
| 394 |
+
" <td>13352</td>\n",
|
| 395 |
+
" </tr>\n",
|
| 396 |
+
" <tr>\n",
|
| 397 |
+
" <th>323</th>\n",
|
| 398 |
+
" <td>2012-12-01</td>\n",
|
| 399 |
+
" <td>12716</td>\n",
|
| 400 |
+
" </tr>\n",
|
| 401 |
+
" </tbody>\n",
|
| 402 |
+
"</table>\n",
|
| 403 |
+
"<p>324 rows × 2 columns</p>\n",
|
| 404 |
+
"</div>"
|
| 405 |
+
],
|
| 406 |
+
"text/plain": [
|
| 407 |
+
" Date Total\n",
|
| 408 |
+
"0 1986-01-01 9034\n",
|
| 409 |
+
"1 1986-02-01 9596\n",
|
| 410 |
+
"2 1986-03-01 10558\n",
|
| 411 |
+
"3 1986-04-01 9002\n",
|
| 412 |
+
"4 1986-05-01 9239\n",
|
| 413 |
+
".. ... ...\n",
|
| 414 |
+
"319 2012-08-01 14442\n",
|
| 415 |
+
"320 2012-09-01 34722\n",
|
| 416 |
+
"321 2012-10-01 13795\n",
|
| 417 |
+
"322 2012-11-01 13352\n",
|
| 418 |
+
"323 2012-12-01 12716\n",
|
| 419 |
+
"\n",
|
| 420 |
+
"[324 rows x 2 columns]"
|
| 421 |
+
]
|
| 422 |
+
},
|
| 423 |
+
"execution_count": 1,
|
| 424 |
+
"metadata": {},
|
| 425 |
+
"output_type": "execute_result"
|
| 426 |
+
}
|
| 427 |
+
],
|
| 428 |
+
"source": [
|
| 429 |
+
"import pandas as pd\n",
|
| 430 |
+
"dataset = pd.read_csv('data/catfish_sales_1986_2001.csv', parse_dates=[0])\n",
|
| 431 |
+
"dataset"
|
| 432 |
+
]
|
| 433 |
+
},
|
| 434 |
+
{
|
| 435 |
+
"cell_type": "code",
|
| 436 |
+
"execution_count": null,
|
| 437 |
+
"metadata": {},
|
| 438 |
+
"outputs": [],
|
| 439 |
+
"source": []
|
| 440 |
+
},
|
| 441 |
+
{
|
| 442 |
+
"cell_type": "markdown",
|
| 443 |
+
"metadata": {},
|
| 444 |
+
"source": [
|
| 445 |
+
"[Clicca qui per tornare all'inizio della pagina](#start)<a id='start'></a>"
|
| 446 |
+
]
|
| 447 |
+
}
|
| 448 |
+
],
|
| 449 |
+
"metadata": {
|
| 450 |
+
"kernelspec": {
|
| 451 |
+
"display_name": "Python 3 (ipykernel)",
|
| 452 |
+
"language": "python",
|
| 453 |
+
"name": "python3"
|
| 454 |
+
},
|
| 455 |
+
"language_info": {
|
| 456 |
+
"codemirror_mode": {
|
| 457 |
+
"name": "ipython",
|
| 458 |
+
"version": 3
|
| 459 |
+
},
|
| 460 |
+
"file_extension": ".py",
|
| 461 |
+
"mimetype": "text/x-python",
|
| 462 |
+
"name": "python",
|
| 463 |
+
"nbconvert_exporter": "python",
|
| 464 |
+
"pygments_lexer": "ipython3",
|
| 465 |
+
"version": "3.10.6"
|
| 466 |
+
},
|
| 467 |
+
"nbTranslate": {
|
| 468 |
+
"displayLangs": [
|
| 469 |
+
"*"
|
| 470 |
+
],
|
| 471 |
+
"hotkey": "alt-t",
|
| 472 |
+
"langInMainMenu": true,
|
| 473 |
+
"sourceLang": "en",
|
| 474 |
+
"targetLang": "fr",
|
| 475 |
+
"useGoogleTranslate": true
|
| 476 |
+
}
|
| 477 |
+
},
|
| 478 |
+
"nbformat": 4,
|
| 479 |
+
"nbformat_minor": 2
|
| 480 |
+
}
|
Lezione 3.S - Soluzioni.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Lezione 4.A - Time Series.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Lezione 4.E - Esercizi.ipynb
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"<a id='start'></a>\n",
|
| 8 |
+
"# Esercizi Serie storiche\n",
|
| 9 |
+
"\n",
|
| 10 |
+
"In questo notebook vengono presentati degli esercizi sulle serie storiche (_time series_).\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"Provate a svolgere il seguente esercizio:<br>\n",
|
| 13 |
+
"1) [Analisi di una time series](#section1)<br>\n",
|
| 14 |
+
"2) [Scomposizione nelle componenti principali](#section2)<br>\n",
|
| 15 |
+
"3) [Eliminazione di trend e stagionalità](#section3)<br>\n",
|
| 16 |
+
"4) [Predizione sulla serie originale](#section4)<br>"
|
| 17 |
+
]
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "markdown",
|
| 21 |
+
"metadata": {},
|
| 22 |
+
"source": [
|
| 23 |
+
"<a id='section1'></a>\n",
|
| 24 |
+
"## Analisi di una time series"
|
| 25 |
+
]
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"cell_type": "markdown",
|
| 29 |
+
"metadata": {},
|
| 30 |
+
"source": [
|
| 31 |
+
"Analizziamo i dati dei passeggeri dei voli di linea negli Stati Uniti:"
|
| 32 |
+
]
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"cell_type": "code",
|
| 36 |
+
"execution_count": 24,
|
| 37 |
+
"metadata": {},
|
| 38 |
+
"outputs": [
|
| 39 |
+
{
|
| 40 |
+
"data": {
|
| 41 |
+
"text/html": [
|
| 42 |
+
"<div>\n",
|
| 43 |
+
"<style scoped>\n",
|
| 44 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 45 |
+
" vertical-align: middle;\n",
|
| 46 |
+
" }\n",
|
| 47 |
+
"\n",
|
| 48 |
+
" .dataframe tbody tr th {\n",
|
| 49 |
+
" vertical-align: top;\n",
|
| 50 |
+
" }\n",
|
| 51 |
+
"\n",
|
| 52 |
+
" .dataframe thead th {\n",
|
| 53 |
+
" text-align: right;\n",
|
| 54 |
+
" }\n",
|
| 55 |
+
"</style>\n",
|
| 56 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 57 |
+
" <thead>\n",
|
| 58 |
+
" <tr style=\"text-align: right;\">\n",
|
| 59 |
+
" <th></th>\n",
|
| 60 |
+
" <th>value</th>\n",
|
| 61 |
+
" </tr>\n",
|
| 62 |
+
" <tr>\n",
|
| 63 |
+
" <th>date</th>\n",
|
| 64 |
+
" <th></th>\n",
|
| 65 |
+
" </tr>\n",
|
| 66 |
+
" </thead>\n",
|
| 67 |
+
" <tbody>\n",
|
| 68 |
+
" <tr>\n",
|
| 69 |
+
" <th>1949-01-01</th>\n",
|
| 70 |
+
" <td>112</td>\n",
|
| 71 |
+
" </tr>\n",
|
| 72 |
+
" <tr>\n",
|
| 73 |
+
" <th>1949-02-01</th>\n",
|
| 74 |
+
" <td>118</td>\n",
|
| 75 |
+
" </tr>\n",
|
| 76 |
+
" <tr>\n",
|
| 77 |
+
" <th>1949-03-01</th>\n",
|
| 78 |
+
" <td>132</td>\n",
|
| 79 |
+
" </tr>\n",
|
| 80 |
+
" <tr>\n",
|
| 81 |
+
" <th>1949-04-01</th>\n",
|
| 82 |
+
" <td>129</td>\n",
|
| 83 |
+
" </tr>\n",
|
| 84 |
+
" <tr>\n",
|
| 85 |
+
" <th>1949-05-01</th>\n",
|
| 86 |
+
" <td>121</td>\n",
|
| 87 |
+
" </tr>\n",
|
| 88 |
+
" <tr>\n",
|
| 89 |
+
" <th>...</th>\n",
|
| 90 |
+
" <td>...</td>\n",
|
| 91 |
+
" </tr>\n",
|
| 92 |
+
" <tr>\n",
|
| 93 |
+
" <th>1960-08-01</th>\n",
|
| 94 |
+
" <td>606</td>\n",
|
| 95 |
+
" </tr>\n",
|
| 96 |
+
" <tr>\n",
|
| 97 |
+
" <th>1960-09-01</th>\n",
|
| 98 |
+
" <td>508</td>\n",
|
| 99 |
+
" </tr>\n",
|
| 100 |
+
" <tr>\n",
|
| 101 |
+
" <th>1960-10-01</th>\n",
|
| 102 |
+
" <td>461</td>\n",
|
| 103 |
+
" </tr>\n",
|
| 104 |
+
" <tr>\n",
|
| 105 |
+
" <th>1960-11-01</th>\n",
|
| 106 |
+
" <td>390</td>\n",
|
| 107 |
+
" </tr>\n",
|
| 108 |
+
" <tr>\n",
|
| 109 |
+
" <th>1960-12-01</th>\n",
|
| 110 |
+
" <td>432</td>\n",
|
| 111 |
+
" </tr>\n",
|
| 112 |
+
" </tbody>\n",
|
| 113 |
+
"</table>\n",
|
| 114 |
+
"<p>144 rows × 1 columns</p>\n",
|
| 115 |
+
"</div>"
|
| 116 |
+
],
|
| 117 |
+
"text/plain": [
|
| 118 |
+
" value\n",
|
| 119 |
+
"date \n",
|
| 120 |
+
"1949-01-01 112\n",
|
| 121 |
+
"1949-02-01 118\n",
|
| 122 |
+
"1949-03-01 132\n",
|
| 123 |
+
"1949-04-01 129\n",
|
| 124 |
+
"1949-05-01 121\n",
|
| 125 |
+
"... ...\n",
|
| 126 |
+
"1960-08-01 606\n",
|
| 127 |
+
"1960-09-01 508\n",
|
| 128 |
+
"1960-10-01 461\n",
|
| 129 |
+
"1960-11-01 390\n",
|
| 130 |
+
"1960-12-01 432\n",
|
| 131 |
+
"\n",
|
| 132 |
+
"[144 rows x 1 columns]"
|
| 133 |
+
]
|
| 134 |
+
},
|
| 135 |
+
"execution_count": 24,
|
| 136 |
+
"metadata": {},
|
| 137 |
+
"output_type": "execute_result"
|
| 138 |
+
}
|
| 139 |
+
],
|
| 140 |
+
"source": [
|
| 141 |
+
"import pandas as pd\n",
|
| 142 |
+
"\n",
|
| 143 |
+
"df = pd.read_csv('data/AirPassengers.csv', parse_dates=['date'], index_col='date')\n",
|
| 144 |
+
"df"
|
| 145 |
+
]
|
| 146 |
+
},
|
| 147 |
+
{
|
| 148 |
+
"cell_type": "markdown",
|
| 149 |
+
"metadata": {},
|
| 150 |
+
"source": [
|
| 151 |
+
"Effettuiamo la visualizzazione dei dati con un grafico a linee (sia normale che con two sided view):"
|
| 152 |
+
]
|
| 153 |
+
},
|
| 154 |
+
{
|
| 155 |
+
"cell_type": "code",
|
| 156 |
+
"execution_count": null,
|
| 157 |
+
"metadata": {},
|
| 158 |
+
"outputs": [],
|
| 159 |
+
"source": []
|
| 160 |
+
},
|
| 161 |
+
{
|
| 162 |
+
"cell_type": "markdown",
|
| 163 |
+
"metadata": {},
|
| 164 |
+
"source": [
|
| 165 |
+
"<a id='section2'></a>\n",
|
| 166 |
+
"## Scomposizione nelle componenti principali"
|
| 167 |
+
]
|
| 168 |
+
},
|
| 169 |
+
{
|
| 170 |
+
"cell_type": "markdown",
|
| 171 |
+
"metadata": {},
|
| 172 |
+
"source": [
|
| 173 |
+
"Eseguiamo le tecniche viste per realizzare una scomposizione nelle componenti principali dei dati di questa serie storica:"
|
| 174 |
+
]
|
| 175 |
+
},
|
| 176 |
+
{
|
| 177 |
+
"cell_type": "code",
|
| 178 |
+
"execution_count": null,
|
| 179 |
+
"metadata": {},
|
| 180 |
+
"outputs": [],
|
| 181 |
+
"source": []
|
| 182 |
+
},
|
| 183 |
+
{
|
| 184 |
+
"cell_type": "markdown",
|
| 185 |
+
"metadata": {},
|
| 186 |
+
"source": [
|
| 187 |
+
"<a id='section3'></a>\n",
|
| 188 |
+
"## Eliminazione di trend e stagionalità"
|
| 189 |
+
]
|
| 190 |
+
},
|
| 191 |
+
{
|
| 192 |
+
"cell_type": "markdown",
|
| 193 |
+
"metadata": {},
|
| 194 |
+
"source": [
|
| 195 |
+
"Eliminiamo il trend:"
|
| 196 |
+
]
|
| 197 |
+
},
|
| 198 |
+
{
|
| 199 |
+
"cell_type": "code",
|
| 200 |
+
"execution_count": null,
|
| 201 |
+
"metadata": {},
|
| 202 |
+
"outputs": [],
|
| 203 |
+
"source": []
|
| 204 |
+
},
|
| 205 |
+
{
|
| 206 |
+
"cell_type": "markdown",
|
| 207 |
+
"metadata": {},
|
| 208 |
+
"source": [
|
| 209 |
+
"Eliminiamo la stagionalità:"
|
| 210 |
+
]
|
| 211 |
+
},
|
| 212 |
+
{
|
| 213 |
+
"cell_type": "code",
|
| 214 |
+
"execution_count": null,
|
| 215 |
+
"metadata": {},
|
| 216 |
+
"outputs": [],
|
| 217 |
+
"source": []
|
| 218 |
+
},
|
| 219 |
+
{
|
| 220 |
+
"cell_type": "markdown",
|
| 221 |
+
"metadata": {},
|
| 222 |
+
"source": [
|
| 223 |
+
"Eliminiamo sia trand che stagionalità:"
|
| 224 |
+
]
|
| 225 |
+
},
|
| 226 |
+
{
|
| 227 |
+
"cell_type": "code",
|
| 228 |
+
"execution_count": null,
|
| 229 |
+
"metadata": {},
|
| 230 |
+
"outputs": [],
|
| 231 |
+
"source": []
|
| 232 |
+
},
|
| 233 |
+
{
|
| 234 |
+
"cell_type": "markdown",
|
| 235 |
+
"metadata": {},
|
| 236 |
+
"source": [
|
| 237 |
+
"<a id='section4'></a>\n",
|
| 238 |
+
"## Predizione della serie originale"
|
| 239 |
+
]
|
| 240 |
+
},
|
| 241 |
+
{
|
| 242 |
+
"cell_type": "markdown",
|
| 243 |
+
"metadata": {},
|
| 244 |
+
"source": [
|
| 245 |
+
"Proviamo a provedere i prossimi 3 anni (36 mesi) dei dati di questa serie:"
|
| 246 |
+
]
|
| 247 |
+
},
|
| 248 |
+
{
|
| 249 |
+
"cell_type": "code",
|
| 250 |
+
"execution_count": null,
|
| 251 |
+
"metadata": {},
|
| 252 |
+
"outputs": [],
|
| 253 |
+
"source": []
|
| 254 |
+
},
|
| 255 |
+
{
|
| 256 |
+
"cell_type": "markdown",
|
| 257 |
+
"metadata": {},
|
| 258 |
+
"source": [
|
| 259 |
+
"[Clicca qui per tornare all'inizio della pagina](#start)<a id='start'></a>"
|
| 260 |
+
]
|
| 261 |
+
}
|
| 262 |
+
],
|
| 263 |
+
"metadata": {
|
| 264 |
+
"kernelspec": {
|
| 265 |
+
"display_name": "Python 3 (ipykernel)",
|
| 266 |
+
"language": "python",
|
| 267 |
+
"name": "python3"
|
| 268 |
+
},
|
| 269 |
+
"language_info": {
|
| 270 |
+
"codemirror_mode": {
|
| 271 |
+
"name": "ipython",
|
| 272 |
+
"version": 3
|
| 273 |
+
},
|
| 274 |
+
"file_extension": ".py",
|
| 275 |
+
"mimetype": "text/x-python",
|
| 276 |
+
"name": "python",
|
| 277 |
+
"nbconvert_exporter": "python",
|
| 278 |
+
"pygments_lexer": "ipython3",
|
| 279 |
+
"version": "3.10.6"
|
| 280 |
+
},
|
| 281 |
+
"nbTranslate": {
|
| 282 |
+
"displayLangs": [
|
| 283 |
+
"*"
|
| 284 |
+
],
|
| 285 |
+
"hotkey": "alt-t",
|
| 286 |
+
"langInMainMenu": true,
|
| 287 |
+
"sourceLang": "en",
|
| 288 |
+
"targetLang": "fr",
|
| 289 |
+
"useGoogleTranslate": true
|
| 290 |
+
}
|
| 291 |
+
},
|
| 292 |
+
"nbformat": 4,
|
| 293 |
+
"nbformat_minor": 2
|
| 294 |
+
}
|
Lezione 4.S - Soluzioni.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Lezione 5.A - Machine Learning.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Lezione 5.E - Esercizi.ipynb
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {},
|
| 6 |
+
"source": [
|
| 7 |
+
"<a id='start'></a>\n",
|
| 8 |
+
"# Esercizi Reti neurali con Tensor Flow\n",
|
| 9 |
+
"\n",
|
| 10 |
+
"In questo notebook vengono presentati degli esercizi sulle reti neurali con Tensor Flow.\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"Provate a svolgere il seguente esercizio:<br>\n",
|
| 13 |
+
"1) [pp -> H -> ZZ -> 4lepton](#section1)<br>\n",
|
| 14 |
+
"2) [Classificare i getti di particelle](#section2)<br>"
|
| 15 |
+
]
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "markdown",
|
| 19 |
+
"metadata": {},
|
| 20 |
+
"source": [
|
| 21 |
+
"<a id='section1'></a>\n",
|
| 22 |
+
"## pp -> H -> ZZ -> 4lepton"
|
| 23 |
+
]
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"cell_type": "markdown",
|
| 27 |
+
"metadata": {},
|
| 28 |
+
"source": [
|
| 29 |
+
"Creare una rete neurale per analizzare i dati prodotti dagli studi di pp -> H -> ZZ -> 4lepton nel contesto delle ricerche mono-Higgs da parte della collaborazione CMS. I programmi usano le ntuple ridotte (con il nome dell'albero radice HZZ4LeptonsAnalysisReduced) create con Root."
|
| 30 |
+
]
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"cell_type": "code",
|
| 34 |
+
"execution_count": 27,
|
| 35 |
+
"metadata": {},
|
| 36 |
+
"outputs": [
|
| 37 |
+
{
|
| 38 |
+
"data": {
|
| 39 |
+
"text/html": [
|
| 40 |
+
"<div>\n",
|
| 41 |
+
"<style scoped>\n",
|
| 42 |
+
" .dataframe tbody tr th:only-of-type {\n",
|
| 43 |
+
" vertical-align: middle;\n",
|
| 44 |
+
" }\n",
|
| 45 |
+
"\n",
|
| 46 |
+
" .dataframe tbody tr th {\n",
|
| 47 |
+
" vertical-align: top;\n",
|
| 48 |
+
" }\n",
|
| 49 |
+
"\n",
|
| 50 |
+
" .dataframe thead th {\n",
|
| 51 |
+
" text-align: right;\n",
|
| 52 |
+
" }\n",
|
| 53 |
+
"</style>\n",
|
| 54 |
+
"<table border=\"1\" class=\"dataframe\">\n",
|
| 55 |
+
" <thead>\n",
|
| 56 |
+
" <tr style=\"text-align: right;\">\n",
|
| 57 |
+
" <th></th>\n",
|
| 58 |
+
" <th>f_mass4l</th>\n",
|
| 59 |
+
" <th>f_massjj</th>\n",
|
| 60 |
+
" <th>isSignal</th>\n",
|
| 61 |
+
" </tr>\n",
|
| 62 |
+
" </thead>\n",
|
| 63 |
+
" <tbody>\n",
|
| 64 |
+
" <tr>\n",
|
| 65 |
+
" <th>0</th>\n",
|
| 66 |
+
" <td>125.077103</td>\n",
|
| 67 |
+
" <td>1300.426880</td>\n",
|
| 68 |
+
" <td>1.0</td>\n",
|
| 69 |
+
" </tr>\n",
|
| 70 |
+
" <tr>\n",
|
| 71 |
+
" <th>1</th>\n",
|
| 72 |
+
" <td>124.238113</td>\n",
|
| 73 |
+
" <td>437.221863</td>\n",
|
| 74 |
+
" <td>1.0</td>\n",
|
| 75 |
+
" </tr>\n",
|
| 76 |
+
" <tr>\n",
|
| 77 |
+
" <th>3</th>\n",
|
| 78 |
+
" <td>124.480667</td>\n",
|
| 79 |
+
" <td>1021.744080</td>\n",
|
| 80 |
+
" <td>1.0</td>\n",
|
| 81 |
+
" </tr>\n",
|
| 82 |
+
" <tr>\n",
|
| 83 |
+
" <th>4</th>\n",
|
| 84 |
+
" <td>124.919464</td>\n",
|
| 85 |
+
" <td>1101.381958</td>\n",
|
| 86 |
+
" <td>1.0</td>\n",
|
| 87 |
+
" </tr>\n",
|
| 88 |
+
" <tr>\n",
|
| 89 |
+
" <th>7</th>\n",
|
| 90 |
+
" <td>125.049065</td>\n",
|
| 91 |
+
" <td>498.717194</td>\n",
|
| 92 |
+
" <td>1.0</td>\n",
|
| 93 |
+
" </tr>\n",
|
| 94 |
+
" <tr>\n",
|
| 95 |
+
" <th>...</th>\n",
|
| 96 |
+
" <td>...</td>\n",
|
| 97 |
+
" <td>...</td>\n",
|
| 98 |
+
" <td>...</td>\n",
|
| 99 |
+
" </tr>\n",
|
| 100 |
+
" <tr>\n",
|
| 101 |
+
" <th>58056</th>\n",
|
| 102 |
+
" <td>197.383133</td>\n",
|
| 103 |
+
" <td>114.257278</td>\n",
|
| 104 |
+
" <td>0.0</td>\n",
|
| 105 |
+
" </tr>\n",
|
| 106 |
+
" <tr>\n",
|
| 107 |
+
" <th>58057</th>\n",
|
| 108 |
+
" <td>218.065582</td>\n",
|
| 109 |
+
" <td>106.195534</td>\n",
|
| 110 |
+
" <td>0.0</td>\n",
|
| 111 |
+
" </tr>\n",
|
| 112 |
+
" <tr>\n",
|
| 113 |
+
" <th>58060</th>\n",
|
| 114 |
+
" <td>89.288086</td>\n",
|
| 115 |
+
" <td>53.661579</td>\n",
|
| 116 |
+
" <td>0.0</td>\n",
|
| 117 |
+
" </tr>\n",
|
| 118 |
+
" <tr>\n",
|
| 119 |
+
" <th>58062</th>\n",
|
| 120 |
+
" <td>146.756577</td>\n",
|
| 121 |
+
" <td>71.162025</td>\n",
|
| 122 |
+
" <td>0.0</td>\n",
|
| 123 |
+
" </tr>\n",
|
| 124 |
+
" <tr>\n",
|
| 125 |
+
" <th>58069</th>\n",
|
| 126 |
+
" <td>218.869415</td>\n",
|
| 127 |
+
" <td>98.914696</td>\n",
|
| 128 |
+
" <td>0.0</td>\n",
|
| 129 |
+
" </tr>\n",
|
| 130 |
+
" </tbody>\n",
|
| 131 |
+
"</table>\n",
|
| 132 |
+
"<p>20738 rows × 3 columns</p>\n",
|
| 133 |
+
"</div>"
|
| 134 |
+
],
|
| 135 |
+
"text/plain": [
|
| 136 |
+
" f_mass4l f_massjj isSignal\n",
|
| 137 |
+
"0 125.077103 1300.426880 1.0\n",
|
| 138 |
+
"1 124.238113 437.221863 1.0\n",
|
| 139 |
+
"3 124.480667 1021.744080 1.0\n",
|
| 140 |
+
"4 124.919464 1101.381958 1.0\n",
|
| 141 |
+
"7 125.049065 498.717194 1.0\n",
|
| 142 |
+
"... ... ... ...\n",
|
| 143 |
+
"58056 197.383133 114.257278 0.0\n",
|
| 144 |
+
"58057 218.065582 106.195534 0.0\n",
|
| 145 |
+
"58060 89.288086 53.661579 0.0\n",
|
| 146 |
+
"58062 146.756577 71.162025 0.0\n",
|
| 147 |
+
"58069 218.869415 98.914696 0.0\n",
|
| 148 |
+
"\n",
|
| 149 |
+
"[20738 rows x 3 columns]"
|
| 150 |
+
]
|
| 151 |
+
},
|
| 152 |
+
"execution_count": 27,
|
| 153 |
+
"metadata": {},
|
| 154 |
+
"output_type": "execute_result"
|
| 155 |
+
}
|
| 156 |
+
],
|
| 157 |
+
"source": [
|
| 158 |
+
"import numpy as np\n",
|
| 159 |
+
"\n",
|
| 160 |
+
"# fix random seed for reproducibility\n",
|
| 161 |
+
"seed = 7\n",
|
| 162 |
+
"np.random.seed(seed)\n",
|
| 163 |
+
"\n",
|
| 164 |
+
"treename = 'HZZ4LeptonsAnalysisReduced'\n",
|
| 165 |
+
"\n",
|
| 166 |
+
"VARS = ['f_mass4l', 'f_massjj']\n",
|
| 167 |
+
"upfile_VV = uproot.open('data/ntuple_4mu_VV.root')\n",
|
| 168 |
+
"params_VV = upfile_VV[treename].arrays(VARS, library=\"np\")\n",
|
| 169 |
+
"upfile_BKG = uproot.open('data/ntuple_4mu_bkg.root')\n",
|
| 170 |
+
"params_BKG = upfile_BKG[treename].arrays(VARS, library=\"np\")\n",
|
| 171 |
+
"\n",
|
| 172 |
+
"df_VV = pd.DataFrame(params_VV, columns=VARS)\n",
|
| 173 |
+
"df_BKG = pd.DataFrame(params_BKG, columns=VARS)\n",
|
| 174 |
+
" \n",
|
| 175 |
+
"# cut out undefined variables VARS[0] and VARS[1] > -999\n",
|
| 176 |
+
"df_VV = df_VV[(df_VV[VARS[0]] > -999) & (df_VV[VARS[1]] > -999)]\n",
|
| 177 |
+
"df_BKG = df_BKG[(df_BKG[VARS[0]] > -999) & (df_BKG[VARS[1]] > -999)] \n",
|
| 178 |
+
"\n",
|
| 179 |
+
"# add isSignal variable\n",
|
| 180 |
+
"df_VV['isSignal'] = np.ones(len(df_VV)) \n",
|
| 181 |
+
"df_BKG['isSignal'] = np.zeros(len(df_BKG))\n",
|
| 182 |
+
"\n",
|
| 183 |
+
"df_all = pd.concat([df_VV, df_BKG])\n",
|
| 184 |
+
"df_all"
|
| 185 |
+
]
|
| 186 |
+
},
|
| 187 |
+
{
|
| 188 |
+
"cell_type": "markdown",
|
| 189 |
+
"metadata": {},
|
| 190 |
+
"source": [
|
| 191 |
+
"Creiamo una rete densa, con un singolo livello nascosto completamente connesso con lo stesso numero di neuroni delle variabili di input.\n",
|
| 192 |
+
"\n",
|
| 193 |
+
"Possiamo quindi usare la funzione di attivazione `sigmoide` per produrre un'uscita di probabilità nell'intervallo da 0 a 1.\n",
|
| 194 |
+
"\n",
|
| 195 |
+
"Infine usiamo la funzione di loss `binary_crossentropy` durante l'allenamento, una funzione di perdita standard per problemi di classificazione binaria.\n",
|
| 196 |
+
"\n",
|
| 197 |
+
"Infine utimmizziamo il modello con l'algoritmo Adam per il grandient descend."
|
| 198 |
+
]
|
| 199 |
+
},
|
| 200 |
+
{
|
| 201 |
+
"cell_type": "code",
|
| 202 |
+
"execution_count": null,
|
| 203 |
+
"metadata": {},
|
| 204 |
+
"outputs": [],
|
| 205 |
+
"source": []
|
| 206 |
+
},
|
| 207 |
+
{
|
| 208 |
+
"cell_type": "markdown",
|
| 209 |
+
"metadata": {},
|
| 210 |
+
"source": [
|
| 211 |
+
"<a id='section2'></a>\n",
|
| 212 |
+
"## Classificare i getti di particelle"
|
| 213 |
+
]
|
| 214 |
+
},
|
| 215 |
+
{
|
| 216 |
+
"cell_type": "markdown",
|
| 217 |
+
"metadata": {},
|
| 218 |
+
"source": [
|
| 219 |
+
"Caricare i dati da `jetImage.h5` e quindi definizimo una DNN con tre livelli che, una volta allenata, aiuti a classificare i getti di particelle contenuti in jets.\n",
|
| 220 |
+
"\n",
|
| 221 |
+
"Le ultime 6 feature (**'j_g' 'j_q' 'j_w' 'j_z' 'j_t' 'j_undef'**) riportano l'informazione di quale jet stiamo osservando. Le informazioni sono fornite con una codifica one-hot: tutti i flag sono 0 tranne quello per la categoria corretta, che è impostato su 1.\n",
|
| 222 |
+
"\n",
|
| 223 |
+
"Ad esempio, un **W-jet** avrà indici **[0,0,1,0,0,0]**\n",
|
| 224 |
+
"\n",
|
| 225 |
+
"Si noti che l'ultimo flag (**undefined**) viene utilizzato per jet di categorie diverse dalle cinque specificate. Poiché il nostro set di dati ha solo getti di questi cinque tipi, questo flag è sempre impostato su 0 e può essere scartato."
|
| 226 |
+
]
|
| 227 |
+
},
|
| 228 |
+
{
|
| 229 |
+
"cell_type": "code",
|
| 230 |
+
"execution_count": 2,
|
| 231 |
+
"metadata": {},
|
| 232 |
+
"outputs": [],
|
| 233 |
+
"source": [
|
| 234 |
+
"import h5py\n",
|
| 235 |
+
"import numpy as np\n",
|
| 236 |
+
"\n",
|
| 237 |
+
"f = h5py.File(\"data/jetImage.h5\")\n",
|
| 238 |
+
"jets = f.get('jets')\n",
|
| 239 |
+
"y = np.array(jets[:,-6:-1])\n",
|
| 240 |
+
"X = np.array(jets[:,:-6])"
|
| 241 |
+
]
|
| 242 |
+
},
|
| 243 |
+
{
|
| 244 |
+
"cell_type": "code",
|
| 245 |
+
"execution_count": null,
|
| 246 |
+
"metadata": {},
|
| 247 |
+
"outputs": [],
|
| 248 |
+
"source": []
|
| 249 |
+
},
|
| 250 |
+
{
|
| 251 |
+
"cell_type": "markdown",
|
| 252 |
+
"metadata": {},
|
| 253 |
+
"source": [
|
| 254 |
+
"[Clicca qui per tornare all'inizio della pagina](#start)<a id='start'></a>"
|
| 255 |
+
]
|
| 256 |
+
}
|
| 257 |
+
],
|
| 258 |
+
"metadata": {
|
| 259 |
+
"kernelspec": {
|
| 260 |
+
"display_name": "Python 3 (ipykernel)",
|
| 261 |
+
"language": "python",
|
| 262 |
+
"name": "python3"
|
| 263 |
+
},
|
| 264 |
+
"language_info": {
|
| 265 |
+
"codemirror_mode": {
|
| 266 |
+
"name": "ipython",
|
| 267 |
+
"version": 3
|
| 268 |
+
},
|
| 269 |
+
"file_extension": ".py",
|
| 270 |
+
"mimetype": "text/x-python",
|
| 271 |
+
"name": "python",
|
| 272 |
+
"nbconvert_exporter": "python",
|
| 273 |
+
"pygments_lexer": "ipython3",
|
| 274 |
+
"version": "3.10.6"
|
| 275 |
+
},
|
| 276 |
+
"nbTranslate": {
|
| 277 |
+
"displayLangs": [
|
| 278 |
+
"*"
|
| 279 |
+
],
|
| 280 |
+
"hotkey": "alt-t",
|
| 281 |
+
"langInMainMenu": true,
|
| 282 |
+
"sourceLang": "en",
|
| 283 |
+
"targetLang": "fr",
|
| 284 |
+
"useGoogleTranslate": true
|
| 285 |
+
}
|
| 286 |
+
},
|
| 287 |
+
"nbformat": 4,
|
| 288 |
+
"nbformat_minor": 2
|
| 289 |
+
}
|
Lezione 5.S - Soluzioni.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Lezione 6.A - Deep Learning.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|