{"id":1583,"date":"2023-07-25T18:37:40","date_gmt":"2023-07-25T18:37:40","guid":{"rendered":"https:\/\/statorials.org\/de\/trace-roc-kurve-python\/"},"modified":"2023-07-25T18:37:40","modified_gmt":"2023-07-25T18:37:40","slug":"trace-roc-kurve-python","status":"publish","type":"post","link":"https:\/\/statorials.org\/de\/trace-roc-kurve-python\/","title":{"rendered":"So zeichnen sie eine roc-kurve in python (schritt f\u00fcr schritt)"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\"><a href=\"https:\/\/statorials.org\/de\/logistische-regression-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">Die logistische Regression<\/a> ist eine statistische Methode, die wir verwenden, um ein Regressionsmodell anzupassen, wenn die Antwortvariable bin\u00e4r ist. Um zu bewerten, wie gut ein logistisches Regressionsmodell zu einem Datensatz passt, k\u00f6nnen wir uns die folgenden zwei Metriken ansehen:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>Sensitivit\u00e4t:<\/strong> Wahrscheinlichkeit, dass das Modell ein positives Ergebnis f\u00fcr eine Beobachtung vorhersagt, obwohl das Ergebnis tats\u00e4chlich positiv ist. Dies wird auch als \u201eTrue-Positive-Rate\u201c bezeichnet.<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>Spezifit\u00e4t:<\/strong> Die Wahrscheinlichkeit, dass das Modell ein negatives Ergebnis f\u00fcr eine Beobachtung vorhersagt, obwohl das Ergebnis tats\u00e4chlich negativ ist. Dies wird auch als \u201eechte Negativrate\u201c bezeichnet.<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Eine M\u00f6glichkeit, diese beiden Messungen zu visualisieren, besteht darin, eine <strong>ROC-Kurve<\/strong> zu erstellen, die f\u00fcr \u201eReceiver Operating Characteristic\u201c-Kurve steht. Dies ist ein Diagramm, das die Sensitivit\u00e4t und Spezifit\u00e4t eines logistischen Regressionsmodells anzeigt.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Das folgende Schritt-f\u00fcr-Schritt-Beispiel zeigt, wie Sie eine ROC-Kurve in Python erstellen und interpretieren.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong><span style=\"color: #000000;\">Schritt 1: Importieren Sie die erforderlichen Pakete<\/span><\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Zuerst importieren wir die notwendigen Pakete, um eine logistische Regression in Python durchzuf\u00fchren:<\/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>Schritt 2: Passen Sie das logistische Regressionsmodell an<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Als N\u00e4chstes importieren wir einen Datensatz und passen ein logistisches Regressionsmodell daran an:<\/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>Schritt 3: Zeichnen Sie die ROC-Kurve<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Als n\u00e4chstes berechnen wir die True-Positive-Rate und die False-Positive-Rate und erstellen eine ROC-Kurve mit dem Matplotlib-Datenvisualisierungspaket:<\/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=\"\"><\/p>\n<p> <span style=\"color: #000000;\">Je n\u00e4her die Kurve an der oberen linken Ecke des Diagramms liegt, desto besser kann das Modell die Daten in Kategorien einteilen.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Wie wir aus der obigen Grafik ersehen k\u00f6nnen, gelingt es diesem logistischen Regressionsmodell ziemlich schlecht, die Daten in Kategorien zu sortieren.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Um dies zu quantifizieren, k\u00f6nnen wir die AUC \u2013 Fl\u00e4che unter der Kurve \u2013 berechnen, die uns sagt, wie viel von der Handlung unter der Kurve liegt.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Je n\u00e4her die AUC bei 1 liegt, desto besser ist das Modell. Ein Modell mit einer AUC von 0,5 ist nicht besser als ein Modell, das zuf\u00e4llige Klassifizierungen durchf\u00fchrt.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Schritt 4: Berechnen Sie die AUC<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Wir k\u00f6nnen den folgenden Code verwenden, um die AUC des Modells zu berechnen und sie in der unteren rechten Ecke des ROC-Diagramms anzuzeigen:<\/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=\"\"><\/p>\n<p> <span style=\"color: #000000;\">Die AUC dieses logistischen Regressionsmodells betr\u00e4gt <strong>0,5602<\/strong> . Da dieser Wert nahe bei 0,5 liegt, best\u00e4tigt dies, dass das Modell die Daten schlecht klassifiziert.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Verwandte Themen:<\/strong> <a href=\"https:\/\/statorials.org\/de\/zeichnen-sie-mehrere-kurven-in-roc-python\/\">So zeichnen Sie mehrere ROC-Kurven in Python<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Die logistische Regression ist eine statistische Methode, die wir verwenden, um ein Regressionsmodell anzupassen, wenn die Antwortvariable bin\u00e4r ist. Um zu bewerten, wie gut ein logistisches Regressionsmodell zu einem Datensatz passt, k\u00f6nnen wir uns die folgenden zwei Metriken ansehen: Sensitivit\u00e4t: Wahrscheinlichkeit, dass das Modell ein positives Ergebnis f\u00fcr eine Beobachtung vorhersagt, obwohl das Ergebnis tats\u00e4chlich [&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>So zeichnen Sie eine ROC-Kurve in Python (Schritt f\u00fcr Schritt) - Statorials<\/title>\n<meta name=\"description\" content=\"In diesem Tutorial wird anhand eines Schritt-f\u00fcr-Schritt-Beispiels erkl\u00e4rt, wie man eine ROC-Kurve in Python zeichnet.\" \/>\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\/de\/trace-roc-kurve-python\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"So zeichnen Sie eine ROC-Kurve in Python (Schritt f\u00fcr Schritt) - Statorials\" \/>\n<meta property=\"og:description\" content=\"In diesem Tutorial wird anhand eines Schritt-f\u00fcr-Schritt-Beispiels erkl\u00e4rt, wie man eine ROC-Kurve in Python zeichnet.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/de\/trace-roc-kurve-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 Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/de\/trace-roc-kurve-python\/\",\"url\":\"https:\/\/statorials.org\/de\/trace-roc-kurve-python\/\",\"name\":\"So zeichnen Sie eine ROC-Kurve in Python (Schritt f\u00fcr Schritt) - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/de\/#website\"},\"datePublished\":\"2023-07-25T18:37:40+00:00\",\"dateModified\":\"2023-07-25T18:37:40+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0\"},\"description\":\"In diesem Tutorial wird anhand eines Schritt-f\u00fcr-Schritt-Beispiels erkl\u00e4rt, wie man eine ROC-Kurve in Python zeichnet.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/de\/trace-roc-kurve-python\/#breadcrumb\"},\"inLanguage\":\"de-DE\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/de\/trace-roc-kurve-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/de\/trace-roc-kurve-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Heim\",\"item\":\"https:\/\/statorials.org\/de\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"So zeichnen sie eine roc-kurve in python (schritt f\u00fcr schritt)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/de\/#website\",\"url\":\"https:\/\/statorials.org\/de\/\",\"name\":\"Statorials\",\"description\":\"Ihr Leitfaden f\u00fcr statistische Kompetenz !\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/de\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"de-DE\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0\",\"name\":\"Dr. Benjamin Anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"de-DE\",\"@id\":\"https:\/\/statorials.org\/de\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/de\/wp-content\/uploads\/2023\/11\/Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/de\/wp-content\/uploads\/2023\/11\/Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr. Benjamin Anderson\"},\"description\":\"Hallo, ich bin Benjamin, ein pensionierter Statistikprofessor, der sich zum engagierten Statorials-Lehrer entwickelt hat. Mit umfassender Erfahrung und Fachwissen auf dem Gebiet der Statistik bin ich bestrebt, mein Wissen zu teilen, um Studenten durch Statorials zu bef\u00e4higen. Mehr wissen\",\"sameAs\":[\"https:\/\/statorials.org\/de\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"So zeichnen Sie eine ROC-Kurve in Python (Schritt f\u00fcr Schritt) - Statorials","description":"In diesem Tutorial wird anhand eines Schritt-f\u00fcr-Schritt-Beispiels erkl\u00e4rt, wie man eine ROC-Kurve in Python zeichnet.","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\/de\/trace-roc-kurve-python\/","og_locale":"de_DE","og_type":"article","og_title":"So zeichnen Sie eine ROC-Kurve in Python (Schritt f\u00fcr Schritt) - Statorials","og_description":"In diesem Tutorial wird anhand eines Schritt-f\u00fcr-Schritt-Beispiels erkl\u00e4rt, wie man eine ROC-Kurve in Python zeichnet.","og_url":"https:\/\/statorials.org\/de\/trace-roc-kurve-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 Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/de\/trace-roc-kurve-python\/","url":"https:\/\/statorials.org\/de\/trace-roc-kurve-python\/","name":"So zeichnen Sie eine ROC-Kurve in Python (Schritt f\u00fcr Schritt) - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/de\/#website"},"datePublished":"2023-07-25T18:37:40+00:00","dateModified":"2023-07-25T18:37:40+00:00","author":{"@id":"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0"},"description":"In diesem Tutorial wird anhand eines Schritt-f\u00fcr-Schritt-Beispiels erkl\u00e4rt, wie man eine ROC-Kurve in Python zeichnet.","breadcrumb":{"@id":"https:\/\/statorials.org\/de\/trace-roc-kurve-python\/#breadcrumb"},"inLanguage":"de-DE","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/de\/trace-roc-kurve-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/de\/trace-roc-kurve-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Heim","item":"https:\/\/statorials.org\/de\/"},{"@type":"ListItem","position":2,"name":"So zeichnen sie eine roc-kurve in python (schritt f\u00fcr schritt)"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/de\/#website","url":"https:\/\/statorials.org\/de\/","name":"Statorials","description":"Ihr Leitfaden f\u00fcr statistische Kompetenz !","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/de\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"de-DE"},{"@type":"Person","@id":"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0","name":"Dr. Benjamin Anderson","image":{"@type":"ImageObject","inLanguage":"de-DE","@id":"https:\/\/statorials.org\/de\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/de\/wp-content\/uploads\/2023\/11\/Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/de\/wp-content\/uploads\/2023\/11\/Benjamin-Anderson-96x96.jpg","caption":"Dr. Benjamin Anderson"},"description":"Hallo, ich bin Benjamin, ein pensionierter Statistikprofessor, der sich zum engagierten Statorials-Lehrer entwickelt hat. Mit umfassender Erfahrung und Fachwissen auf dem Gebiet der Statistik bin ich bestrebt, mein Wissen zu teilen, um Studenten durch Statorials zu bef\u00e4higen. Mehr wissen","sameAs":["https:\/\/statorials.org\/de"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/posts\/1583"}],"collection":[{"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/comments?post=1583"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/posts\/1583\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/media?parent=1583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/categories?post=1583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/tags?post=1583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}