{"id":3921,"date":"2023-07-14T18:26:54","date_gmt":"2023-07-14T18:26:54","guid":{"rendered":"https:\/\/statorials.org\/nl\/kubieke-regressiepython\/"},"modified":"2023-07-14T18:26:54","modified_gmt":"2023-07-14T18:26:54","slug":"kubieke-regressiepython","status":"publish","type":"post","link":"https:\/\/statorials.org\/nl\/kubieke-regressiepython\/","title":{"rendered":"Hoe kubieke regressie uit te voeren in python"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\"><strong>Kubieke regressie<\/strong> is een type regressie dat we kunnen gebruiken om de relatie tussen een voorspellende variabele en een responsvariabele te kwantificeren wanneer de relatie tussen de variabelen niet-lineair is.<\/span><\/p>\n<p> <span style=\"color: #000000;\">In deze tutorial wordt uitgelegd hoe u kubieke regressie uitvoert in Python.<\/span><\/p>\n<h2> <strong><span style=\"color: #000000;\">Voorbeeld: kubieke regressie in Python<\/span><\/strong><\/h2>\n<p> <span style=\"color: #000000;\">Stel dat we het volgende panda&#8217;s DataFrame hebben dat twee variabelen bevat (x en y):<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #107d3f;\">import<\/span> pandas <span style=\"color: #107d3f;\">as<\/span> pd\n\n<span style=\"color: #008080;\">#createDataFrame\n<\/span>df = pd. <span style=\"color: #3366ff;\">DataFrame<\/span> ({' <span style=\"color: #ff0000;\">x<\/span> ': [6, 9, 12, 16, 22, 28, 33, 40, 47, 51, 55, 60],\n                   ' <span style=\"color: #ff0000;\">y<\/span> ': [14, 28, 50, 64, 67, 57, 55, 57, 68, 74, 88, 110]})\n\n<span style=\"color: #008080;\">#view DataFrame\n<\/span><span style=\"color: #008000;\">print<\/span> (df)\n\n     xy\n0 6 14\n1 9 28\n2 12 50\n3 16 64\n4 22 67\n5 28 57\n6 33 55\n7 40 57\n8 47 68\n9 51 74\n10 55 88\n11 60 110\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Als we een eenvoudig spreidingsdiagram van deze gegevens maken, kunnen we zien dat de relatie tussen de twee variabelen niet-lineair is:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #107d3f;\">import <span style=\"color: #000000;\">matplotlib. <span style=\"color: #3366ff;\">pyplot<\/span><\/span> as <span style=\"color: #000000;\">plt<\/span>\n\n<span style=\"color: #000000;\"><span style=\"color: #008080;\">#create scatterplot\n<\/span>plt. <span style=\"color: #3366ff;\">scatter<\/span> (df. <span style=\"color: #3366ff;\">x<\/span> , df. <span style=\"color: #3366ff;\">y<\/span> )<\/span><\/span><\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-31513 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/cubique1.jpg\" alt=\"\" width=\"463\" height=\"341\" srcset=\"\" sizes=\"auto, \"><\/p>\n<p> <span style=\"color: #000000;\">Naarmate de waarde van x toeneemt, neemt y toe tot een bepaald punt, neemt vervolgens af en neemt vervolgens weer toe.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Dit patroon met twee &#8222;curven&#8220; in de plot is een indicatie van een kubieke relatie tussen de twee variabelen.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Dit betekent dat een kubisch regressiemodel een goede kandidaat is om de relatie tussen de twee variabelen te kwantificeren.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Om kubieke regressie uit te voeren, kunnen we een polynomiaal regressiemodel met graad 3 fitten met behulp van de <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.polyfit.html\" target=\"_blank\" rel=\"noopener noreferrer\">numpy.polyfit()<\/a> <span style=\"color: #000000;\">functie<\/span> :<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #107d3f;\">import<\/span> numpy <span style=\"color: #107d3f;\">as<\/span> np\n\n<span style=\"color: #008080;\">#fit cubic regression model\n<\/span>model = np. <span style=\"color: #3366ff;\">poly1d<\/span> (np. <span style=\"color: #3366ff;\">polyfit<\/span> (df. <span style=\"color: #3366ff;\">x<\/span> , df. <span style=\"color: #3366ff;\">y<\/span> , <span style=\"color: #000000;\">3))<\/span>\n\n<span style=\"color: #008080;\">#add fitted cubic regression line to scatterplot\n<\/span>polyline = np. <span style=\"color: #3366ff;\">linspace<\/span> (1, 60, 50)\nplt. <span style=\"color: #3366ff;\">scatter<\/span> (df. <span style=\"color: #3366ff;\">x<\/span> , df. <span style=\"color: #3366ff;\">y<\/span> )\nplt. <span style=\"color: #3366ff;\">plot<\/span> (polyline, model(polyline))\n\n<span style=\"color: #008080;\">#add axis labels\n<\/span>plt. <span style=\"color: #3366ff;\">xlabel<\/span> (' <span style=\"color: #ff0000;\">x<\/span> ')\nplt. <span style=\"color: #3366ff;\">ylabel<\/span> (' <span style=\"color: #ff0000;\">y<\/span> ')\n\n<span style=\"color: #008080;\">#displayplot\n<\/span>plt. <span style=\"color: #3366ff;\">show<\/span> ()<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-31514\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/cubique2.jpg\" alt=\"kubieke regressie in Python\" width=\"547\" height=\"404\" srcset=\"\" sizes=\"auto, \"><\/p>\n<p> <span style=\"color: #000000;\">We kunnen de aangepaste kubieke regressievergelijking verkrijgen door de modelco\u00ebffici\u00ebnten af te drukken:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">print<\/span> (model)\n\n          3 2\n0.003302x - 0.3214x + 9.832x - 32.01\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">De aangepaste kubieke regressievergelijking is:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>y = 0,003302(x) <sup>3<\/sup> \u2013 0,3214(x) <sup>2<\/sup> + 9,832x \u2013 30,01<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">We kunnen deze vergelijking gebruiken om de verwachte waarde van y te berekenen op basis van de waarde van x.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Als x bijvoorbeeld 30 is, dan is de verwachte waarde voor y 64,844:<\/span><\/p>\n<p> <span style=\"color: #000000;\">y = 0,003302(30) <sup>3<\/sup> \u2013 0,3214(30) <sup>2<\/sup> + 9,832(30) \u2013 30,01 = 64,844<\/span><\/p>\n<p> <span style=\"color: #000000;\">We kunnen ook een korte functie schrijven om het R-kwadraat van het model te verkrijgen, wat het deel van de variantie in de responsvariabele is dat kan worden verklaard door de voorspellende variabelen.<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#define function to calculate r-squared<\/span>\n<span style=\"color: #008000;\">def<\/span> polyfit(x, y, degree):\n    results = {}\n    coeffs = np. <span style=\"color: #3366ff;\">polyfit<\/span> (x, y, degree)\n    p = np. <span style=\"color: #3366ff;\">poly1d<\/span> (coeffs)\n    <span style=\"color: #008080;\">#calculate r-squared<\/span>\n    yhat = p(x)\n    ybar = np. <span style=\"color: #3366ff;\">sum<\/span> (y)\/len(y)\n    ssreg = np. <span style=\"color: #3366ff;\">sum<\/span> ((yhat-ybar) <span style=\"color: #800080;\">**<\/span> 2)\n    sstot = np. <span style=\"color: #3366ff;\">sum<\/span> ((y - ybar) <span style=\"color: #800080;\">**<\/span> 2)\n    results[' <span style=\"color: #ff0000;\">r_squared<\/span> '] = ssreg \/ sstot\n\n    <span style=\"color: #008000;\">return<\/span> results\n\n<span style=\"color: #008080;\">#find r-squared of polynomial model with degree = 3\n<\/span>polyfit(df. <span style=\"color: #3366ff;\">x<\/span> , df. <span style=\"color: #3366ff;\">y<\/span> , 3)\n\n{'r_squared': 0.9632469890057967}\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">In dit voorbeeld is het R-kwadraat van het model <strong>0,9632<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Dit betekent dat 96,32% van de variatie in de responsvariabele kan worden verklaard door de voorspellende variabele.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Omdat deze waarde zo hoog is, vertelt dit ons dat het kubieke regressiemodel de relatie tussen de twee variabelen goed kwantificeert.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Gerelateerd:<\/strong> <a href=\"https:\/\/statorials.org\/nl\/goede-r-kwadraatwaarde\/\" target=\"_blank\" rel=\"noopener\">Wat is een goede R-kwadraatwaarde?<\/a><\/span><\/p>\n<h2> <span style=\"color: #000000;\"><strong>Aanvullende bronnen<\/strong><\/span><\/h2>\n<p> <span style=\"color: #000000;\">In de volgende tutorials wordt uitgelegd hoe u andere veelvoorkomende taken in Python kunt uitvoeren:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/nl\/eenvoudige-lineaire-regressie-in-python\/\" target=\"_blank\" rel=\"noopener\">Hoe eenvoudige lineaire regressie uit te voeren in Python<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/kwadratische-regressiepython\/\" target=\"_blank\" rel=\"noopener\">Hoe kwadratische regressie uit te voeren in Python<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\/polynomiale-regressiepython\/\" target=\"_blank\" rel=\"noopener noreferrer\">Hoe polynomiale regressie uit te voeren in Python<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kubieke regressie is een type regressie dat we kunnen gebruiken om de relatie tussen een voorspellende variabele en een responsvariabele te kwantificeren wanneer de relatie tussen de variabelen niet-lineair is. In deze tutorial wordt uitgelegd hoe u kubieke regressie uitvoert in Python. Voorbeeld: kubieke regressie in Python Stel dat we het volgende panda&#8217;s DataFrame hebben [&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":[],"class_list":["post-3921","post","type-post","status-publish","format-standard","hentry","category-gids"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Hoe kubieke regressie uit te voeren in Python - Statorials<\/title>\n<meta name=\"description\" content=\"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u kubieke regressie in Python uitvoert.\" \/>\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\/nl\/kubieke-regressiepython\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hoe kubieke regressie uit te voeren in Python - Statorials\" \/>\n<meta property=\"og:description\" content=\"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u kubieke regressie in Python uitvoert.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/nl\/kubieke-regressiepython\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-14T18:26:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/cubique1.jpg\" \/>\n<meta name=\"author\" content=\"Dr.benjamin anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Verfasst von\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dr.benjamin anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Gesch\u00e4tzte Lesezeit\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\u00a0Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/nl\/kubieke-regressiepython\/\",\"url\":\"https:\/\/statorials.org\/nl\/kubieke-regressiepython\/\",\"name\":\"Hoe kubieke regressie uit te voeren in Python - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/nl\/#website\"},\"datePublished\":\"2023-07-14T18:26:54+00:00\",\"dateModified\":\"2023-07-14T18:26:54+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\"},\"description\":\"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u kubieke regressie in Python uitvoert.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/nl\/kubieke-regressiepython\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/nl\/kubieke-regressiepython\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/nl\/kubieke-regressiepython\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Thuis\",\"item\":\"https:\/\/statorials.org\/nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hoe kubieke regressie uit te voeren in python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/nl\/#website\",\"url\":\"https:\/\/statorials.org\/nl\/\",\"name\":\"Statorials\",\"description\":\"Uw gids voor statistische competentie\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/nl\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"de\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\",\"name\":\"Dr.benjamin anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"de\",\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr.benjamin anderson\"},\"description\":\"Ik ben Benjamin, een gepensioneerde hoogleraar statistiek die nu een toegewijde Statorials-lesgever is. Ik heb uitgebreide ervaring en expertise op het gebied van statistiek en ik ben vastbesloten om mijn kennis te delen met studenten via Statorials. Lees verder\",\"sameAs\":[\"http:\/\/statorials.org\/nl\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hoe kubieke regressie uit te voeren in Python - Statorials","description":"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u kubieke regressie in Python uitvoert.","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\/nl\/kubieke-regressiepython\/","og_locale":"de_DE","og_type":"article","og_title":"Hoe kubieke regressie uit te voeren in Python - Statorials","og_description":"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u kubieke regressie in Python uitvoert.","og_url":"https:\/\/statorials.org\/nl\/kubieke-regressiepython\/","og_site_name":"Statorials","article_published_time":"2023-07-14T18:26:54+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/cubique1.jpg"}],"author":"Dr.benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Verfasst von":"Dr.benjamin anderson","Gesch\u00e4tzte Lesezeit":"3\u00a0Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/nl\/kubieke-regressiepython\/","url":"https:\/\/statorials.org\/nl\/kubieke-regressiepython\/","name":"Hoe kubieke regressie uit te voeren in Python - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/nl\/#website"},"datePublished":"2023-07-14T18:26:54+00:00","dateModified":"2023-07-14T18:26:54+00:00","author":{"@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219"},"description":"In deze tutorial wordt aan de hand van een voorbeeld uitgelegd hoe u kubieke regressie in Python uitvoert.","breadcrumb":{"@id":"https:\/\/statorials.org\/nl\/kubieke-regressiepython\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/nl\/kubieke-regressiepython\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/nl\/kubieke-regressiepython\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Thuis","item":"https:\/\/statorials.org\/nl\/"},{"@type":"ListItem","position":2,"name":"Hoe kubieke regressie uit te voeren in python"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/nl\/#website","url":"https:\/\/statorials.org\/nl\/","name":"Statorials","description":"Uw gids voor statistische competentie","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/nl\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"de"},{"@type":"Person","@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219","name":"Dr.benjamin anderson","image":{"@type":"ImageObject","inLanguage":"de","@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/image\/","url":"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"http:\/\/statorials.org\/nl\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Dr.benjamin anderson"},"description":"Ik ben Benjamin, een gepensioneerde hoogleraar statistiek die nu een toegewijde Statorials-lesgever is. Ik heb uitgebreide ervaring en expertise op het gebied van statistiek en ik ben vastbesloten om mijn kennis te delen met studenten via Statorials. Lees verder","sameAs":["http:\/\/statorials.org\/nl"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/3921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/comments?post=3921"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/3921\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/media?parent=3921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/categories?post=3921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/tags?post=3921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}