{"id":2460,"date":"2023-07-22T04:29:06","date_gmt":"2023-07-22T04:29:06","guid":{"rendered":"https:\/\/statorials.org\/it\/analisi-bivariata-in-python\/"},"modified":"2023-07-22T04:29:06","modified_gmt":"2023-07-22T04:29:06","slug":"analisi-bivariata-in-python","status":"publish","type":"post","link":"https:\/\/statorials.org\/it\/analisi-bivariata-in-python\/","title":{"rendered":"Come eseguire l&#39;analisi bivariata in python: con esempi"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Il termine <strong>analisi bivariata<\/strong> si riferisce all\u2019analisi di due variabili. Puoi ricordarlo perch\u00e9 il prefisso \u201cbi\u201d significa \u201cdue\u201d.<\/span><\/p>\n<p> <span style=\"color: #000000;\">L\u2019obiettivo dell\u2019analisi bivariata \u00e8 comprendere la relazione tra due variabili<\/span><\/p>\n<p> <span style=\"color: #000000;\">Esistono tre modi comuni per eseguire l&#8217;analisi bivariata:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>1.<\/strong> Nuvole di punti<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>2.<\/strong> Coefficienti di correlazione<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>3.<\/strong> Regressione lineare semplice<\/span><\/p>\n<p> <span style=\"color: #000000;\">L&#8217;esempio seguente mostra come eseguire ciascuno di questi tipi di analisi bivariata in Python utilizzando il seguente DataFrame panda che contiene informazioni su due variabili: <strong>(1)<\/strong> Ore trascorse a studiare e <strong>(2)<\/strong> Punteggio dell&#8217;esame ottenuto da 20 studenti diversi:<\/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\n<span style=\"color: #008080;\">#createDataFrame<\/span>\ndf = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">hours<\/span> ': [1, 1, 1, 2, 2, 2, 3, 3, 3, 3,\n                             3, 4, 4, 5, 5, 6, 6, 6, 7, 8],\n                   ' <span style=\"color: #ff0000;\">score<\/span> ': [75, 66, 68, 74, 78, 72, 85, 82, 90, 82,\n                             80, 88, 85, 90, 92, 94, 94, 88, 91, 96]})\n\n<span style=\"color: #008080;\">#view first five rows of DataFrame\n<\/span>df. <span style=\"color: #3366ff;\">head<\/span> ()\n\n\thours score\n0 1 75\n1 1 66\n2 1 68\n3 2 74\n4 2 78<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>1. Nuvole di punti<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare la seguente sintassi per creare un grafico a dispersione delle ore studiate rispetto ai risultati degli esami:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> matplotlib. <span style=\"color: #008000;\"><span style=\"color: #3366ff;\">pyplot<\/span> as<\/span> plt\n\n<span style=\"color: #008080;\">#create scatterplot of hours vs. score<\/span>\nplt. <span style=\"color: #3366ff;\">scatter<\/span> (df. <span style=\"color: #3366ff;\">hours<\/span> , df. <span style=\"color: #3366ff;\">score<\/span> )\nplt. <span style=\"color: #3366ff;\">title<\/span> (' <span style=\"color: #ff0000;\">Hours Studied vs. Exam Score<\/span> ')\nplt. <span style=\"color: #3366ff;\">xlabel<\/span> (' <span style=\"color: #ff0000;\">Hours Studied<\/span> ')\nplt. <span style=\"color: #3366ff;\">ylabel<\/span> (' <span style=\"color: #ff0000;\">Exam Score<\/span> ')\n<\/strong><\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-22049 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/bivpython1.png\" alt=\"\" width=\"526\" height=\"365\" srcset=\"\" sizes=\"\"><\/p>\n<p> <span style=\"color: #000000;\">L&#8217;asse x mostra le ore studiate e l&#8217;asse y mostra il voto ottenuto all&#8217;esame.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Dal grafico emerge che esiste una relazione positiva tra le due variabili: all&#8217;aumentare del numero di ore di studio, anche i punteggi degli esami tendono ad aumentare.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>2. Coefficienti di correlazione<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Un coefficiente di correlazione di Pearson \u00e8 un modo per quantificare la relazione lineare tra due variabili.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Possiamo usare la funzione <strong>corr()<\/strong> nei panda per creare una matrice di correlazione:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#create correlation matrix\n<\/span>df. <span style=\"color: #3366ff;\">corr<\/span> ()\n\n\thours score\nhours 1.000000 0.891306\nscore 0.891306 1.000000<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Il coefficiente di correlazione risulta essere <strong>0,891<\/strong> . Ci\u00f2<\/span> <span style=\"color: #000000;\">indica una forte correlazione positiva tra le ore studiate e il voto dell&#8217;esame.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>3. Regressione lineare semplice<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">La regressione lineare semplice \u00e8 un metodo statistico che possiamo utilizzare per quantificare la relazione tra due variabili.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Possiamo utilizzare la funzione <strong>OLS()<\/strong> del pacchetto statsmodels per adattare rapidamente un <a href=\"https:\/\/statorials.org\/it\/regressione-lineare-semplice-in-python\/\" target=\"_blank\" rel=\"noopener\">semplice modello di regressione lineare<\/a> per le ore studiate e i risultati degli esami ricevuti:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import<\/span> statsmodels. <span style=\"color: #3366ff;\">api<\/span> <span style=\"color: #008000;\">as<\/span> sm\n\n<span style=\"color: #008080;\">#define response variable\n<\/span>y = df[' <span style=\"color: #ff0000;\">score<\/span> ']\n\n<span style=\"color: #008080;\">#define explanatory variable\n<\/span>x = df[[' <span style=\"color: #ff0000;\">hours<\/span> ']]\n\n<span style=\"color: #008080;\">#add constant to predictor variables\n<\/span>x = sm. <span style=\"color: #3366ff;\">add_constant<\/span> (x)\n\n<span style=\"color: #008080;\">#fit linear regression model\n<\/span>model = sm. <span style=\"color: #3366ff;\">OLS<\/span> (y,x). <span style=\"color: #3366ff;\">fit<\/span> ()\n\n<span style=\"color: #008080;\">#view model summary\n<\/span><span style=\"color: #008000;\">print<\/span> ( <span style=\"color: #3366ff;\">model.summary<\/span> ())\n\n                            OLS Regression Results                            \n==================================================== ============================\nDept. Variable: R-squared score: 0.794\nModel: OLS Adj. R-squared: 0.783\nMethod: Least Squares F-statistic: 69.56\nDate: Mon, 22 Nov 2021 Prob (F-statistic): 1.35e-07\nTime: 16:15:52 Log-Likelihood: -55,886\nNo. Observations: 20 AIC: 115.8\nDf Residuals: 18 BIC: 117.8\nModel: 1                                         \nCovariance Type: non-robust                                         \n==================================================== ============================\n                 coef std err t P&gt;|t| [0.025 0.975]\n-------------------------------------------------- ----------------------------\nconst 69.0734 1.965 35.149 0.000 64.945 73.202\nhours 3.8471 0.461 8.340 0.000 2.878 4.816\n==================================================== ============================\nOmnibus: 0.171 Durbin-Watson: 1.404\nProb(Omnibus): 0.918 Jarque-Bera (JB): 0.177\nSkew: 0.165 Prob(JB): 0.915\nKurtosis: 2.679 Cond. No. 9.37\n==================================================== ============================\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">L\u2019equazione di regressione adattata risulta essere:<\/span><\/p>\n<p> <span style=\"color: #000000;\">Punteggio esame = 69,0734 + 3,8471*(ore studiate)<\/span><\/p>\n<p> <span style=\"color: #000000;\">Questo ci dice che ogni ora aggiuntiva studiata \u00e8 associata a un aumento medio di <strong>3,8471<\/strong> nel punteggio dell&#8217;esame.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Possiamo anche utilizzare l&#8217;equazione di regressione adattata per prevedere il punteggio che uno studente ricever\u00e0 in base al numero totale di ore studiate.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Ad esempio, uno studente che studia per 3 ore dovrebbe ottenere un punteggio di <strong>81,6147<\/strong> :<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Punteggio esame = 69,0734 + 3,8471*(ore studiate)<\/span><\/li>\n<li> <span style=\"color: #000000;\">Punteggio dell&#8217;esame = 69,0734 + 3,8471*(3)<\/span><\/li>\n<li> <span style=\"color: #000000;\">Risultato dell&#8217;esame = 81.6147<\/span><\/li>\n<\/ul>\n<h3> <span style=\"color: #000000;\"><strong>Risorse addizionali<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Le seguenti esercitazioni forniscono informazioni aggiuntive sull&#8217;analisi bivariata:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/it\/analisi-bivariata\/\" target=\"_blank\" rel=\"noopener\">Un&#8217;introduzione all&#8217;analisi bivariata<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/esempi-reali-di-dati-bivariati\/\" target=\"_blank\" rel=\"noopener\">5 esempi di dati bivariati nella vita reale<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/regressione-lineare-1\/\" target=\"_blank\" rel=\"noopener\">Un&#8217;introduzione alla regressione lineare semplice<\/a><br \/> <a href=\"https:\/\/statorials.org\/it\/coefficiente-di-correlazione-di-pearson-1\/\" target=\"_blank\" rel=\"noopener\">Un&#8217;introduzione al coefficiente di correlazione di Pearson<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Il termine analisi bivariata si riferisce all\u2019analisi di due variabili. Puoi ricordarlo perch\u00e9 il prefisso \u201cbi\u201d significa \u201cdue\u201d. L\u2019obiettivo dell\u2019analisi bivariata \u00e8 comprendere la relazione tra due variabili Esistono tre modi comuni per eseguire l&#8217;analisi bivariata: 1. Nuvole di punti 2. Coefficienti di correlazione 3. Regressione lineare semplice L&#8217;esempio seguente mostra come eseguire ciascuno di [&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 eseguire l&#039;analisi bivariata in Python (con esempi) - Statorials<\/title>\n<meta name=\"description\" content=\"Questo tutorial spiega come eseguire l&#039;analisi bivariata in Python, con diversi esempi.\" \/>\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\/analisi-bivariata-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Come eseguire l&#039;analisi bivariata in Python (con esempi) - Statorials\" \/>\n<meta property=\"og:description\" content=\"Questo tutorial spiega come eseguire l&#039;analisi bivariata in Python, con diversi esempi.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/it\/analisi-bivariata-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-22T04:29:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/bivpython1.png\" \/>\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=\"3 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/it\/analisi-bivariata-in-python\/\",\"url\":\"https:\/\/statorials.org\/it\/analisi-bivariata-in-python\/\",\"name\":\"Come eseguire l&#39;analisi bivariata in Python (con esempi) - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/it\/#website\"},\"datePublished\":\"2023-07-22T04:29:06+00:00\",\"dateModified\":\"2023-07-22T04:29:06+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae\"},\"description\":\"Questo tutorial spiega come eseguire l&#39;analisi bivariata in Python, con diversi esempi.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/it\/analisi-bivariata-in-python\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/it\/analisi-bivariata-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/it\/analisi-bivariata-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Casa\",\"item\":\"https:\/\/statorials.org\/it\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Come eseguire l&#39;analisi bivariata in python: con esempi\"}]},{\"@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 eseguire l&#39;analisi bivariata in Python (con esempi) - Statorials","description":"Questo tutorial spiega come eseguire l&#39;analisi bivariata in Python, con diversi esempi.","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\/analisi-bivariata-in-python\/","og_locale":"it_IT","og_type":"article","og_title":"Come eseguire l&#39;analisi bivariata in Python (con esempi) - Statorials","og_description":"Questo tutorial spiega come eseguire l&#39;analisi bivariata in Python, con diversi esempi.","og_url":"https:\/\/statorials.org\/it\/analisi-bivariata-in-python\/","og_site_name":"Statorials","article_published_time":"2023-07-22T04:29:06+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/bivpython1.png"}],"author":"Benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Benjamin anderson","Est. reading time":"3 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/it\/analisi-bivariata-in-python\/","url":"https:\/\/statorials.org\/it\/analisi-bivariata-in-python\/","name":"Come eseguire l&#39;analisi bivariata in Python (con esempi) - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/it\/#website"},"datePublished":"2023-07-22T04:29:06+00:00","dateModified":"2023-07-22T04:29:06+00:00","author":{"@id":"https:\/\/statorials.org\/it\/#\/schema\/person\/0896f191fb9fb019f2cd8623112cb3ae"},"description":"Questo tutorial spiega come eseguire l&#39;analisi bivariata in Python, con diversi esempi.","breadcrumb":{"@id":"https:\/\/statorials.org\/it\/analisi-bivariata-in-python\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/it\/analisi-bivariata-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/it\/analisi-bivariata-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Casa","item":"https:\/\/statorials.org\/it\/"},{"@type":"ListItem","position":2,"name":"Come eseguire l&#39;analisi bivariata in python: con esempi"}]},{"@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\/2460"}],"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=2460"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/posts\/2460\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/media?parent=2460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/categories?post=2460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/it\/wp-json\/wp\/v2\/tags?post=2460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}