{"id":1585,"date":"2023-07-25T18:37:40","date_gmt":"2023-07-25T18:37:40","guid":{"rendered":"https:\/\/statorials.org\/nl\/trace-roc-curve-python\/"},"modified":"2023-07-25T18:37:40","modified_gmt":"2023-07-25T18:37:40","slug":"trace-roc-curve-python","status":"publish","type":"post","link":"https:\/\/statorials.org\/nl\/trace-roc-curve-python\/","title":{"rendered":"Een roc-curve tekenen in python (stap voor stap)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\"><a href=\"https:\/\/statorials.org\/nl\/logistische-regressie-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">Logistische regressie<\/a> is een statistische methode die we gebruiken om een regressiemodel te fitten wanneer de responsvariabele binair is. Om te evalueren hoe goed een logistisch regressiemodel bij een dataset past, kunnen we naar de volgende twee statistieken kijken:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>Gevoeligheid:<\/strong> waarschijnlijkheid dat het model een positief resultaat voorspelt voor een waarneming terwijl het resultaat daadwerkelijk positief is. Dit wordt ook wel het \u201cechte positieve percentage\u201d genoemd.<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>Specificiteit:<\/strong> de kans dat het model een negatief resultaat voorspelt voor een waarneming terwijl het resultaat feitelijk negatief is. Dit wordt ook wel het \u2018echte negatieve tarief\u2019 genoemd.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">E\u00e9n manier om deze twee metingen te visualiseren is door een <strong>ROC-curve<\/strong> te maken, wat staat voor \u2018receiver operating karakteristieke\u2019-curve. Dit is een grafiek die de gevoeligheid en specificiteit van een logistisch regressiemodel weergeeft.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Het volgende stapsgewijze voorbeeld laat zien hoe u een ROC-curve in Python kunt maken en interpreteren.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong><span style=\"color: #000000;\">Stap 1: Importeer de benodigde pakketten<\/span><\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Eerst zullen we de benodigde pakketten importeren om logistieke regressie in Python uit te voeren:<\/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<span style=\"color: #008000;\">import<\/span> numpy <span style=\"color: #008000;\">as<\/span> np\n<span style=\"color: #008000;\">from<\/span> sklearn. <span style=\"color: #3366ff;\">model_selection<\/span> <span style=\"color: #008000;\">import<\/span> train_test_split\n<span style=\"color: #008000;\">from<\/span> sklearn. <span style=\"color: #3366ff;\">linear_model<\/span> <span style=\"color: #008000;\">import<\/span> LogisticRegression\n<span style=\"color: #008000;\">from<\/span> sklearn <span style=\"color: #008000;\">import<\/span> metrics\n<span style=\"color: #008000;\">import<\/span> matplotlib. <span style=\"color: #3366ff;\">pyplot<\/span> <span style=\"color: #008000;\">as<\/span> plt\n<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Stap 2: Pas het logistische regressiemodel aan<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Vervolgens importeren we een dataset en passen we er een logistisch regressiemodel aan toe:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#import dataset from CSV file on Github\n<\/span>url = \"https:\/\/raw.githubusercontent.com\/Statorials\/Python-Guides\/main\/default.csv\"\ndata = pd. <span style=\"color: #3366ff;\">read_csv<\/span> (url)\n\n<span style=\"color: #008080;\">#define the predictor variables and the response variable\n<\/span>X = data[[' <span style=\"color: #ff0000;\">student<\/span> ',' <span style=\"color: #ff0000;\">balance<\/span> ',' <span style=\"color: #ff0000;\">income<\/span> ']]\ny = data[' <span style=\"color: #ff0000;\">default<\/span> ']\n\n<span style=\"color: #008080;\">#split the dataset into training (70%) and testing (30%) sets\n<\/span>X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=0) \n\n<span style=\"color: #008080;\">#instantiate the model\n<\/span>log_regression = LogisticRegression()\n\n<span style=\"color: #008080;\">#fit the model using the training data\n<\/span>log_regression. <span style=\"color: #3366ff;\">fit<\/span> (X_train,y_train)<\/strong><\/span><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Stap 3: Teken de ROC-curve<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Vervolgens berekenen we het werkelijk positieve percentage en het fout-positieve percentage en cre\u00ebren we een ROC-curve met behulp van het Matplotlib-datavisualisatiepakket:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#define metrics\n<\/span>y_pred_proba = log_regression. <span style=\"color: #3366ff;\">predict_proba<\/span> (X_test)[::,1]\nfpr, tpr, _ = metrics. <span style=\"color: #3366ff;\">roc_curve<\/span> (y_test, y_pred_proba)\n\n<span style=\"color: #008080;\">#create ROC curve\n<\/span>plt. <span style=\"color: #3366ff;\">plot<\/span> (fpr,tpr)\nplt. <span style=\"color: #3366ff;\">ylabel<\/span> (' <span style=\"color: #ff0000;\">True Positive Rate<\/span> ')\nplt. <span style=\"color: #3366ff;\">xlabel<\/span> (' <span style=\"color: #ff0000;\">False Positive Rate<\/span> ')\nplt. <span style=\"color: #3366ff;\">show<\/span> ()<\/strong><\/span> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-15772 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/rocpython1.png\" alt=\"\" width=\"399\" height=\"267\" srcset=\"\" sizes=\"auto, \"><\/p>\n<p> <span style=\"color: #000000;\">Hoe dichter de curve bij de linkerbovenhoek van de grafiek past, hoe beter het model de gegevens in categorie\u00ebn kan indelen.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Zoals we uit de bovenstaande grafiek kunnen zien, slaagt dit logistische regressiemodel er behoorlijk slecht in om de gegevens in categorie\u00ebn te sorteren.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Om dit te kwantificeren kunnen we de AUC (gebied onder de curve) berekenen, die ons vertelt hoeveel van de grafiek zich onder de curve bevindt.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Hoe dichter de AUC bij 1 ligt, hoe beter het model. Een model met een AUC gelijk aan 0,5 is niet beter dan een model dat willekeurige classificaties uitvoert.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Stap 4: Bereken de AUC<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">We kunnen de volgende code gebruiken om de AUC van het model te berekenen en deze in de rechter benedenhoek van de ROC-plot weer te geven:<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#define metrics\n<\/span>y_pred_proba = log_regression. <span style=\"color: #3366ff;\">predict_proba<\/span> (X_test)[::,1]\nfpr, tpr, _ = metrics. <span style=\"color: #3366ff;\">roc_curve<\/span> (y_test, y_pred_proba)\nauc = metrics. <span style=\"color: #3366ff;\">roc_auc_score<\/span> (y_test, y_pred_proba)\n\n<span style=\"color: #008080;\">#create ROC curve\n<\/span>plt. <span style=\"color: #3366ff;\">plot<\/span> (fpr,tpr,label=\" <span style=\"color: #ff0000;\">AUC=<\/span> \"+str(auc))\nplt. <span style=\"color: #3366ff;\">ylabel<\/span> (' <span style=\"color: #ff0000;\">True Positive Rate<\/span> ')\nplt. <span style=\"color: #3366ff;\">xlabel<\/span> (' <span style=\"color: #ff0000;\">False Positive Rate<\/span> ')\nplt. <span style=\"color: #3366ff;\">legend<\/span> (loc=4)\nplt. <span style=\"color: #3366ff;\">show<\/span> ()<\/strong><\/span> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\" wp-image-15773 aligncenter\" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/rocpython2.png\" alt=\"\" width=\"404\" height=\"275\" srcset=\"\" sizes=\"auto, \"><\/p>\n<p> <span style=\"color: #000000;\">De AUC van dit logistieke regressiemodel blijkt <strong>0,5602<\/strong> te zijn. Aangezien dit cijfer gesloten is tot 0,5, bevestigt dit dat het model de gegevens slecht classificeert.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Gerelateerd:<\/strong> <a href=\"https:\/\/statorials.org\/nl\/teken-meerdere-curven-roc-python\/\">Hoe meerdere ROC-curven in Python te plotten<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Logistische regressie is een statistische methode die we gebruiken om een regressiemodel te fitten wanneer de responsvariabele binair is. Om te evalueren hoe goed een logistisch regressiemodel bij een dataset past, kunnen we naar de volgende twee statistieken kijken: Gevoeligheid: waarschijnlijkheid dat het model een positief resultaat voorspelt voor een waarneming terwijl het resultaat daadwerkelijk [&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-1585","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>Een ROC-curve tekenen in Python (stap voor stap) - Statorials<\/title>\n<meta name=\"description\" content=\"In deze tutorial wordt uitgelegd hoe u een ROC-curve in Python tekent, met een stapsgewijs voorbeeld.\" \/>\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\/trace-roc-curve-python\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Een ROC-curve tekenen in Python (stap voor stap) - Statorials\" \/>\n<meta property=\"og:description\" content=\"In deze tutorial wordt uitgelegd hoe u een ROC-curve in Python tekent, met een stapsgewijs voorbeeld.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/nl\/trace-roc-curve-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-25T18:37:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/rocpython1.png\" \/>\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\/trace-roc-curve-python\/\",\"url\":\"https:\/\/statorials.org\/nl\/trace-roc-curve-python\/\",\"name\":\"Een ROC-curve tekenen in Python (stap voor stap) - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/nl\/#website\"},\"datePublished\":\"2023-07-25T18:37:40+00:00\",\"dateModified\":\"2023-07-25T18:37:40+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\"},\"description\":\"In deze tutorial wordt uitgelegd hoe u een ROC-curve in Python tekent, met een stapsgewijs voorbeeld.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/nl\/trace-roc-curve-python\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/nl\/trace-roc-curve-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/nl\/trace-roc-curve-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Thuis\",\"item\":\"https:\/\/statorials.org\/nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Een roc-curve tekenen in python (stap voor stap)\"}]},{\"@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":"Een ROC-curve tekenen in Python (stap voor stap) - Statorials","description":"In deze tutorial wordt uitgelegd hoe u een ROC-curve in Python tekent, met een stapsgewijs voorbeeld.","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\/trace-roc-curve-python\/","og_locale":"de_DE","og_type":"article","og_title":"Een ROC-curve tekenen in Python (stap voor stap) - Statorials","og_description":"In deze tutorial wordt uitgelegd hoe u een ROC-curve in Python tekent, met een stapsgewijs voorbeeld.","og_url":"https:\/\/statorials.org\/nl\/trace-roc-curve-python\/","og_site_name":"Statorials","article_published_time":"2023-07-25T18:37:40+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/rocpython1.png"}],"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\/trace-roc-curve-python\/","url":"https:\/\/statorials.org\/nl\/trace-roc-curve-python\/","name":"Een ROC-curve tekenen in Python (stap voor stap) - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/nl\/#website"},"datePublished":"2023-07-25T18:37:40+00:00","dateModified":"2023-07-25T18:37:40+00:00","author":{"@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219"},"description":"In deze tutorial wordt uitgelegd hoe u een ROC-curve in Python tekent, met een stapsgewijs voorbeeld.","breadcrumb":{"@id":"https:\/\/statorials.org\/nl\/trace-roc-curve-python\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/nl\/trace-roc-curve-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/nl\/trace-roc-curve-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Thuis","item":"https:\/\/statorials.org\/nl\/"},{"@type":"ListItem","position":2,"name":"Een roc-curve tekenen in python (stap voor stap)"}]},{"@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\/1585","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=1585"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/1585\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/media?parent=1585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/categories?post=1585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/tags?post=1585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}