{"id":3122,"date":"2023-07-19T03:04:16","date_gmt":"2023-07-19T03:04:16","guid":{"rendered":"https:\/\/statorials.org\/it\/prova-del-treno-panda\/"},"modified":"2023-07-19T03:04:16","modified_gmt":"2023-07-19T03:04:16","slug":"prova-del-treno-panda","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/prova-del-treno-panda\/","title":{"rendered":"Come creare un treno e un set di test da un pandas dataframe"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Quando adattiamo i modelli di machine learning ai set di dati, spesso dividiamo il set di dati in due set:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>1. Set di training:<\/strong> utilizzato per addestrare il modello (70-80% del set di dati originale)<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>2. Set di test:<\/strong> utilizzato per ottenere una stima imparziale delle prestazioni del modello (20-30% del set di dati originale)<\/span><\/p>\n<p> <span style=\"color: #000000;\">In Python, ci sono due modi comuni per dividere un DataFrame panda in un set di addestramento e un set di test:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Metodo 1: utilizzare train_test_split() di sklearn<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">from<\/span> sklearn. <span style=\"color: #3366ff;\">model_selection<\/span> <span style=\"color: #008000;\">import<\/span> train_test_split\n\ntrain, test = train_test_split(df, test_size= <span style=\"color: #008000;\">0.2<\/span> , random_state= <span style=\"color: #008000;\">0<\/span> )<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Metodo 2: usa sample() dai panda<\/strong><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>train = df. <span style=\"color: #3366ff;\">sample<\/span> (frac= <span style=\"color: #008000;\">0.8<\/span> , random_state= <span style=\"color: #008000;\">0<\/span> )\ntest = df. <span style=\"color: #3366ff;\">drop<\/span> ( <span style=\"color: #3366ff;\">train.index<\/span> )<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Gli esempi seguenti mostrano come utilizzare ciascun metodo con i seguenti DataFrame panda:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> pandas <span style=\"color: #008000;\">as<\/span> pd\n<span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np\n\n<span style=\"color: #008080;\">#make this example reproducible\n<\/span>n.p. <span style=\"color: #3366ff;\">random<\/span> . <span style=\"color: #3366ff;\">seeds<\/span> (1)\n\n<span style=\"color: #008080;\">#create DataFrame with 1,000 rows and 3 columns\n<\/span>df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> <span style=\"color: #3366ff;\">(<\/span> {' <span style=\"color: #ff0000;\">x1<\/span> ': <span style=\"color: #3366ff;\">np.random.randint<\/span> (30,size=1000),\n                   ' <span style=\"color: #ff0000;\">x2<\/span> ': np. <span style=\"color: #3366ff;\">random<\/span> . <span style=\"color: #3366ff;\">randint<\/span> (12, size=1000),\n                   ' <span style=\"color: #ff0000;\">y<\/span> ': np. <span style=\"color: #3366ff;\">random<\/span> . <span style=\"color: #3366ff;\">randint<\/span> (2, size=1000)})\n\n<span style=\"color: #008080;\">#view first few rows of DataFrame<\/span>\ndf. <span style=\"color: #3366ff;\">head<\/span> ()\n\n        x1 x2 y\n0 5 1 1\n1 11 8 0\n2 12 4 1\n3 8 7 0\n4 9 0 0\n<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 1: usa train_test_split() da sklearn<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\"><span style=\"color: #000000;\">Il codice seguente mostra come utilizzare la funzione <strong>train_test_split()<\/strong> di <strong>sklearn<\/strong> per dividere il DataFrame panda in set di training e test:<\/span><\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">from<\/span> sklearn. <span style=\"color: #3366ff;\">model_selection<\/span> <span style=\"color: #008000;\">import<\/span> train_test_split\n\n<span style=\"color: #008080;\">#split original DataFrame into training and testing sets\n<\/span>train, test = train_test_split(df, test_size= <span style=\"color: #008000;\">0.2<\/span> , random_state= <span style=\"color: #008000;\">0<\/span> )\n\n<span style=\"color: #008080;\">#view first few rows of each set<\/span>\n<span style=\"color: #008000;\">print<\/span> ( <span style=\"color: #3366ff;\">train.head<\/span> ())\n\n     x1 x2 y\n687 16 2 0\n500 18 2 1\n332 4 10 1\n979 2 8 1\n817 11 1 0\n\n<span style=\"color: #008000;\">print<\/span> ( <span style=\"color: #3366ff;\">test.head<\/span> ())\n\n     x1 x2 y\n993 22 1 1\n859 27 6 0\n298 27 8 1\n553 20 6 0\n672 9 2 1\n\n<span style=\"color: #008080;\">#print size of each set<\/span>\n<span style=\"color: #008000;\">print<\/span> (train. <span style=\"color: #3366ff;\">shape<\/span> , test. <span style=\"color: #3366ff;\">shape<\/span> )\n\n(800, 3) (200, 3)\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Dal risultato possiamo vedere che sono stati creati due set:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Set di allenamento: 800 righe e 3 colonne<\/span><\/li>\n<li> <span style=\"color: #000000;\">Set di prova: 200 righe e 3 colonne<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Tieni presente che <strong>test_size<\/strong> controlla la percentuale di osservazioni del DataFrame originale che apparterranno al set di test e il valore <strong>random_state<\/strong> rende riproducibile la divisione.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Esempio 2: utilizzare sample() da panda<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Il codice seguente mostra come utilizzare la funzione <b>pandas<\/b> <strong>sample()<\/strong> per suddividere il DataFrame pandas in set di training e test:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#split original DataFrame into training and testing sets\n<\/span>train = df. <span style=\"color: #3366ff;\">sample<\/span> (frac= <span style=\"color: #008000;\">0.8<\/span> , random_state= <span style=\"color: #008000;\">0<\/span> )\ntest = df. <span style=\"color: #3366ff;\">drop<\/span> ( <span style=\"color: #3366ff;\">train.index<\/span> )\n\n<span style=\"color: #008080;\">#view first few rows of each set<\/span>\n<span style=\"color: #008000;\">print<\/span> ( <span style=\"color: #3366ff;\">train.head<\/span> ())\n\n     x1 x2 y\n993 22 1 1\n859 27 6 0\n298 27 8 1\n553 20 6 0\n672 9 2 1\n\n<span style=\"color: #008000;\">print<\/span> ( <span style=\"color: #3366ff;\">test.head<\/span> ())\n\n    x1 x2 y\n9 16 5 0\n11 12 10 0\n19 5 9 0\n23 28 1 1\n28 18 0 1\n\n<span style=\"color: #008080;\">#print size of each set<\/span>\n<span style=\"color: #008000;\">print<\/span> (train. <span style=\"color: #3366ff;\">shape<\/span> , test. <span style=\"color: #3366ff;\">shape<\/span> )\n\n(800, 3) (200, 3)\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Dal risultato possiamo vedere che sono stati creati due set:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Set di allenamento: 800 righe e 3 colonne<\/span><\/li>\n<li> <span style=\"color: #000000;\">Set di prova: 200 righe e 3 colonne<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Si noti che <b>frac<\/b> controlla la percentuale di osservazioni dal DataFrame originale che apparterranno al set di addestramento e il valore <strong>random_state<\/strong> rende riproducibile la divisione.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">I seguenti tutorial spiegano come eseguire altre attivit\u00e0 comuni in Python:<\/span><\/p>\n<p><a href=\"https:\/\/statorials.org\/it\/python-di-regressione-logistica\/\" target=\"_blank\" rel=\"noopener\">Come eseguire la regressione logistica in Python<\/a><br \/><a href=\"https:\/\/statorials.org\/it\/confusione-della-matrice-python\/\" target=\"_blank\" rel=\"noopener\">Come creare una matrice di confusione in Python<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/sklearn-pitone-di-precisione-bilanciato\/\">Come calcolare la precisione bilanciata in Python<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quando adattiamo i modelli di machine learning ai set di dati, spesso dividiamo il set di dati in due set: 1. Set di training: utilizzato per addestrare il modello (70-80% del set di dati originale) 2. Set di test: utilizzato per ottenere una stima imparziale delle prestazioni del modello (20-30% del set di dati originale) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Come creare un set di training e test da un Pandas DataFrame - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega diversi metodi che \u00e8 possibile utilizzare per creare un set di training e test da un singolo DataFrame Panda.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/it\/prova-del-treno-panda\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come creare un set di training e test da un Pandas DataFrame - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega diversi metodi che \u00e8 possibile utilizzare per creare un set di training e test da un singolo DataFrame Panda.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/prova-del-treno-panda\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-19T03:04:16+00:00\" \/>\n<meta name=\"author\" content=\"Benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/prova-del-treno-panda\/\",\"url\":\"https:\/\/statorials.org\/it\/prova-del-treno-panda\/\",\"name\":\"Come creare un set di training e test da un Pandas DataFrame - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-19T03:04:16+00:00\",\"dateModified\":\"2023-07-19T03:04:16+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega diversi metodi che \u00e8 possibile utilizzare per creare un set di training e test da un singolo DataFrame Panda.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/prova-del-treno-panda\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/prova-del-treno-panda\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/prova-del-treno-panda\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come creare un treno e un set di test da un pandas dataframe\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/it\/#website\",\"url\":\"https:\/\/statorials.org\/it\/\",\"name\":\"Statorials\",\"description\":\"La tua guida all&#039;alfabetizzazione statistica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/it\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\",\"name\":\"Benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Benjamin anderson\"},\"description\":\"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9\",\"sameAs\":[\"https:\/\/statorials.org\/it\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Come creare un set di training e test da un Pandas DataFrame - Statorials","description":"Questo tutorial spiega diversi metodi che \u00e8 possibile utilizzare per creare un set di training e test da un singolo DataFrame Panda.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/statorials.org\/it\/prova-del-treno-panda\/","og_locale":"it_IT","og_type":"article","og_title":"Come creare un set di training e test da un Pandas DataFrame - Statorials","og_description":"Questo tutorial spiega diversi metodi che \u00e8 possibile utilizzare per creare un set di training e test da un singolo DataFrame Panda.","og_url":"https:\/\/statorials.org\/it\/prova-del-treno-panda\/","og_site_name":"Statorials","article_published_time":"2023-07-19T03:04:16+00:00","author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"2 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/prova-del-treno-panda\/","url":"https:\/\/statorials.org\/it\/prova-del-treno-panda\/","name":"Come creare un set di training e test da un Pandas DataFrame - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-19T03:04:16+00:00","dateModified":"2023-07-19T03:04:16+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega diversi metodi che \u00e8 possibile utilizzare per creare un set di training e test da un singolo DataFrame Panda.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/prova-del-treno-panda\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/prova-del-treno-panda\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/prova-del-treno-panda\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come creare un treno e un set di test da un pandas dataframe"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/it\/#website","url":"https:\/\/statorials.org\/it\/","name":"Statorials","description":"La tua guida all&#039;alfabetizzazione statistica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/it\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"it-IT"},{"@type":"Person","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae","name":"Benjamin anderson","image":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/it\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Benjamin anderson"},"description":"Ciao, sono Benjamin, un professore di statistica in pensione diventato insegnante dedicato di Statorials. Con una vasta esperienza e competenza nel campo della statistica, sono ansioso di condividere le mie conoscenze per potenziare gli studenti attraverso Statorials. Scopri di pi\u00f9","sameAs":["https:\/\/statorials.org\/it"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3122"}],"collection":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/comments?post=3122"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/3122\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=3122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=3122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=3122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}