{"id":2170,"date":"2023-07-23T10:04:03","date_gmt":"2023-07-23T10:04:03","guid":{"rendered":"https:\/\/statorials.org\/nl\/auc-in-python\/"},"modified":"2023-07-23T10:04:03","modified_gmt":"2023-07-23T10:04:03","slug":"auc-in-python","status":"publish","type":"post","link":"https:\/\/statorials.org\/nl\/auc-in-python\/","title":{"rendered":"Hoe auc (gebied onder de curve) in python te berekenen"},"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.<\/span><\/p>\n<p> <span style=\"color: #000000;\">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.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Dit is een grafiek die de gevoeligheid langs de y-as en (1 \u2013 specificiteit) langs de x-as weergeeft.<\/span><\/p>\n<p> <span style=\"color: #000000;\">E\u00e9n manier om de effectiviteit van het logistische regressiemodel bij het classificeren van gegevens te kwantificeren is door <strong>de AUC<\/strong> te berekenen, wat staat voor &#8218;area under the curve&#8216;.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Hoe dichter de AUC bij 1 ligt, hoe beter het model.<\/span><\/p>\n<p> <span style=\"color: #000000;\">In het volgende stapsgewijze voorbeeld ziet u hoe u de AUC voor een logistisch regressiemodel in Python kunt berekenen.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Stap 1: Pakketten importeren<\/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<\/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= <span style=\"color: #008000;\">0.3<\/span> ,random_state= <span style=\"color: #008000;\">0<\/span> ) \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: Bereken de AUC<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">We kunnen de functie <strong>metrics.roc_auc_score()<\/strong> gebruiken om de AUC van het model te berekenen:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <span style=\"color: #000000;\"><strong><span style=\"color: #008080;\">#use model to predict probability that given y value is 1\n<\/span>y_pred_proba = log_regression. <span style=\"color: #3366ff;\">predict_proba<\/span> (X_test)[::, <span style=\"color: #008000;\">1<\/span> ]\n\n<span style=\"color: #008080;\">#calculate AUC of model\n<\/span>auc = metrics. <span style=\"color: #3366ff;\">roc_auc_score<\/span> (y_test, y_pred_proba)\n\n<span style=\"color: #008080;\">#print AUC score\n<\/span><span style=\"color: #008000;\">print<\/span> (auc)\n\n0.5602104030579559\n<\/strong><\/span><\/pre>\n<p> <span style=\"color: #000000;\">De AUC (area under curve) voor dit specifieke model is <strong>0,5602<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Bedenk dat een model met een AUC-score van <strong>0,5<\/strong> niet beter is dan een model dat willekeurige gissingen doet.<\/span><\/p>\n<p> <span style=\"color: #000000;\">In de meeste gevallen zou een model met een AUC-score van <strong>0,5602<\/strong> dus als slecht worden beschouwd in het classificeren van waarnemingen in de juiste klassen.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Aanvullende bronnen<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">De volgende tutorials bieden aanvullende informatie over ROC-curven en AUC-scores:<\/span><\/p>\n<p> <a href=\"https:\/\/statorials.org\/nl\/interpreteer-de-rotscurve\/\" target=\"_blank\" rel=\"noopener\">Een ROC-curve interpreteren (met voorbeelden)<\/a><br \/> <a href=\"https:\/\/statorials.org\/nl\" target=\"_blank\" rel=\"noopener\">Wat wordt beschouwd als een goede AUC-score?<\/a><\/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-2170","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 AUC (Area Under Curve) in Python te berekenen - Statorials<\/title>\n<meta name=\"description\" content=\"In deze tutorial wordt uitgelegd hoe u de AUC (area under curve) voor een logistisch regressiemodel in R kunt berekenen, inclusief 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\/auc-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hoe AUC (Area Under Curve) in Python te berekenen - Statorials\" \/>\n<meta property=\"og:description\" content=\"In deze tutorial wordt uitgelegd hoe u de AUC (area under curve) voor een logistisch regressiemodel in R kunt berekenen, inclusief een stapsgewijs voorbeeld.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/nl\/auc-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-23T10:04:03+00:00\" \/>\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=\"2\u00a0Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/nl\/auc-in-python\/\",\"url\":\"https:\/\/statorials.org\/nl\/auc-in-python\/\",\"name\":\"Hoe AUC (Area Under Curve) in Python te berekenen - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/nl\/#website\"},\"datePublished\":\"2023-07-23T10:04:03+00:00\",\"dateModified\":\"2023-07-23T10:04:03+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219\"},\"description\":\"In deze tutorial wordt uitgelegd hoe u de AUC (area under curve) voor een logistisch regressiemodel in R kunt berekenen, inclusief een stapsgewijs voorbeeld.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/nl\/auc-in-python\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/nl\/auc-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/nl\/auc-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Thuis\",\"item\":\"https:\/\/statorials.org\/nl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hoe auc (gebied onder de curve) in python te berekenen\"}]},{\"@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 AUC (Area Under Curve) in Python te berekenen - Statorials","description":"In deze tutorial wordt uitgelegd hoe u de AUC (area under curve) voor een logistisch regressiemodel in R kunt berekenen, inclusief 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\/auc-in-python\/","og_locale":"de_DE","og_type":"article","og_title":"Hoe AUC (Area Under Curve) in Python te berekenen - Statorials","og_description":"In deze tutorial wordt uitgelegd hoe u de AUC (area under curve) voor een logistisch regressiemodel in R kunt berekenen, inclusief een stapsgewijs voorbeeld.","og_url":"https:\/\/statorials.org\/nl\/auc-in-python\/","og_site_name":"Statorials","article_published_time":"2023-07-23T10:04:03+00:00","author":"Dr.benjamin anderson","twitter_card":"summary_large_image","twitter_misc":{"Verfasst von":"Dr.benjamin anderson","Gesch\u00e4tzte Lesezeit":"2\u00a0Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/nl\/auc-in-python\/","url":"https:\/\/statorials.org\/nl\/auc-in-python\/","name":"Hoe AUC (Area Under Curve) in Python te berekenen - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/nl\/#website"},"datePublished":"2023-07-23T10:04:03+00:00","dateModified":"2023-07-23T10:04:03+00:00","author":{"@id":"https:\/\/statorials.org\/nl\/#\/schema\/person\/d4b8842173cca1bb62cdec41860e4219"},"description":"In deze tutorial wordt uitgelegd hoe u de AUC (area under curve) voor een logistisch regressiemodel in R kunt berekenen, inclusief een stapsgewijs voorbeeld.","breadcrumb":{"@id":"https:\/\/statorials.org\/nl\/auc-in-python\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/nl\/auc-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/nl\/auc-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Thuis","item":"https:\/\/statorials.org\/nl\/"},{"@type":"ListItem","position":2,"name":"Hoe auc (gebied onder de curve) in python te berekenen"}]},{"@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\/2170","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=2170"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/posts\/2170\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/media?parent=2170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/categories?post=2170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/nl\/wp-json\/wp\/v2\/tags?post=2170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}