{"id":829,"date":"2023-07-28T14:53:35","date_gmt":"2023-07-28T14:53:35","guid":{"rendered":"https:\/\/statorials.org\/de\/mahalanobis-remote-python\/"},"modified":"2023-07-28T14:53:35","modified_gmt":"2023-07-28T14:53:35","slug":"mahalanobis-remote-python","status":"publish","type":"post","link":"https:\/\/statorials.org\/de\/mahalanobis-remote-python\/","title":{"rendered":"So berechnen sie die mahalanobis-distanz in python"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">Der <strong>Mahalanobis-Abstand<\/strong> ist der Abstand zwischen zwei Punkten in einem multivariaten Raum. Es wird h\u00e4ufig verwendet, um Ausrei\u00dfer in statistischen Analysen mit mehreren Variablen zu erkennen.<\/span><\/p>\n<p> <span style=\"color: #000000;\">In diesem Tutorial wird erl\u00e4utert, wie Sie den Mahalanobis-Abstand in Python berechnen.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Beispiel: Mahalanobis-Distanz in Python<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Verwenden Sie die folgenden Schritte, um die Mahalanobis-Distanz f\u00fcr jede Beobachtung in einem Datensatz in Python zu berechnen.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Schritt 1: Erstellen Sie den Datensatz.<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Zun\u00e4chst erstellen wir einen Datensatz, der die Pr\u00fcfungsergebnisse von 20 Studenten zusammen mit der Anzahl der Lernstunden, der Anzahl der von ihnen abgelegten \u00dcbungspr\u00fcfungen und ihrer aktuellen Note im Kurs anzeigt:<\/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<span style=\"color: #107d3f;\">import<\/span> pandas <span style=\"color: #107d3f;\">as<\/span> pd<span style=\"color: #107d3f;\">\nimport<\/span> scipy <span style=\"color: #107d3f;\">as<\/span> stats\n\ndata = {'score': [91, 93, 72, 87, 86, 73, 68, 87, 78, 99, 95, 76, 84, 96, 76, 80, 83, 84, 73, 74],\n        'hours': [16, 6, 3, 1, 2, 3, 2, 5, 2, 5, 2, 3, 4, 3, 3, 3, 4, 3, 4, 4],\n        'prep': [3, 4, 0, 3, 4, 0, 1, 2, 1, 2, 3, 3, 3, 2, 2, 2, 3, 3, 2, 2],\n        'grade': [70, 88, 80, 83, 88, 84, 78, 94, 90, 93, 89, 82, 95, 94, 81, 93, 93, 90, 89, 89]\n        }\n\ndf = pd.DataFrame(data,columns=['score', 'hours', 'prep','grade'])\ndf.head()\n\n score hours prep grade\n0 91 16 3 70\n1 93 6 4 88\n2 72 3 0 80\n3 87 1 3 83\n4 86 2 4 88\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Schritt 2: Berechnen Sie die Mahalanobis-Distanz f\u00fcr jede Beobachtung.<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Als n\u00e4chstes schreiben wir eine kurze Funktion zur Berechnung der Mahalanobis-Distanz.<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #107d3f;\"><span style=\"color: #008080;\">#create function to calculate Mahalanobis distance<\/span>\ndef<\/span> mahalanobis(x= <span style=\"color: #107d3f;\">None<\/span> , data= <span style=\"color: #107d3f;\">None<\/span> , cov= <span style=\"color: #107d3f;\">None<\/span> ):\n\n    x_mu = x - np.mean(data)\n    if not cov:\n        cov = np.cov(data.values.T)\n    inv_covmat = np.linalg.inv(cov)\n    left = np.dot(x_mu, inv_covmat)\n    mahal = np.dot(left, x_mu.T)\n    return mahal.diagonal()\n\n<span style=\"color: #107d3f;\"><span style=\"color: #008080;\">#create new column in dataframe that contains Mahalanobis distance for each row<\/span><\/span>\ndf['mahalanobis'] = mahalanobis(x=df, data=df[['score', 'hours', 'prep', 'grade']])\n\n<span style=\"color: #008080;\">#display first five rows of dataframe\n<\/span>df.head()\n\n score hours prep grade mahalanobis\n0 91 16 3 70 16.501963\n1 93 6 4 88 2.639286\n2 72 3 0 80 4.850797\n3 87 1 3 83 5.201261\n4 86 2 4 88 3.828734\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\"><strong>Schritt 3: Berechnen Sie den p-Wert f\u00fcr jede Mahalanobis-Distanz.<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Wir k\u00f6nnen sehen, dass einige Mahalanobis-Abst\u00e4nde viel gr\u00f6\u00dfer sind als andere. Um festzustellen, ob eine der Abst\u00e4nde statistisch signifikant ist, m\u00fcssen wir ihre p-Werte berechnen.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Der p-Wert f\u00fcr jede Distanz wird als p-Wert berechnet, der der Chi-Quadrat-Statistik der Mahalanobis-Distanz mit k-1 Freiheitsgraden entspricht, wobei k = Anzahl der Variablen. In diesem Fall verwenden wir also Freiheitsgrade von 4-1 = 3.<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #107d3f;\">from<\/span> scipy.stats <span style=\"color: #107d3f;\">import<\/span> chi2\n\n<span style=\"color: #008080;\">#calculate p-value for each mahalanobis distance<\/span> \ndf['p'] = 1 - chi2.cdf(df['mahalanobis'], 3)\n\n<span style=\"color: #008080;\">#display p-values for first five rows in dataframe<\/span>\ndf.head()\n\n score hours prep grade mahalanobis p\n0 91 16 3 70 16.501963 0.000895\n1 93 6 4 88 2.639286 0.450644\n2 72 3 0 80 4.850797 0.183054\n3 87 1 3 83 5.201261 0.157639\n4 86 2 4 88 3.828734 0.280562\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Im Allgemeinen gilt ein p-Wert von <strong>weniger als 0,001<\/strong> als Ausrei\u00dfer.<\/span> <span style=\"color: #000000;\">Wir k\u00f6nnen sehen, dass die erste Beobachtung ein Ausrei\u00dfer im Datensatz ist, da sie einen p-Wert von weniger als 0,001 hat.<\/span><\/p>\n<p> <span style=\"color: #000000;\">Abh\u00e4ngig vom Kontext des Problems k\u00f6nnen Sie sich entscheiden, diese Beobachtung aus dem Datensatz zu entfernen, da es sich um einen Ausrei\u00dfer handelt und die Analyseergebnisse beeintr\u00e4chtigen k\u00f6nnte.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Der Mahalanobis-Abstand ist der Abstand zwischen zwei Punkten in einem multivariaten Raum. Es wird h\u00e4ufig verwendet, um Ausrei\u00dfer in statistischen Analysen mit mehreren Variablen zu erkennen. In diesem Tutorial wird erl\u00e4utert, wie Sie den Mahalanobis-Abstand in Python berechnen. Beispiel: Mahalanobis-Distanz in Python Verwenden Sie die folgenden Schritte, um die Mahalanobis-Distanz f\u00fcr jede Beobachtung in einem [&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 berechnen Sie die Mahalanobis-Distanz in Python \u2013 Statorials<\/title>\n<meta name=\"description\" content=\"Eine einfache Erkl\u00e4rung zur Berechnung der Mahalanobis-Distanz in Python.\" \/>\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\/mahalanobis-remote-python\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"So berechnen Sie die Mahalanobis-Distanz in Python \u2013 Statorials\" \/>\n<meta property=\"og:description\" content=\"Eine einfache Erkl\u00e4rung zur Berechnung der Mahalanobis-Distanz in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/de\/mahalanobis-remote-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-28T14:53:35+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 Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/de\/mahalanobis-remote-python\/\",\"url\":\"https:\/\/statorials.org\/de\/mahalanobis-remote-python\/\",\"name\":\"So berechnen Sie die Mahalanobis-Distanz in Python \u2013 Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/de\/#website\"},\"datePublished\":\"2023-07-28T14:53:35+00:00\",\"dateModified\":\"2023-07-28T14:53:35+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0\"},\"description\":\"Eine einfache Erkl\u00e4rung zur Berechnung der Mahalanobis-Distanz in Python.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/de\/mahalanobis-remote-python\/#breadcrumb\"},\"inLanguage\":\"de-DE\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/de\/mahalanobis-remote-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/de\/mahalanobis-remote-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Heim\",\"item\":\"https:\/\/statorials.org\/de\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"So berechnen sie die mahalanobis-distanz in python\"}]},{\"@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 berechnen Sie die Mahalanobis-Distanz in Python \u2013 Statorials","description":"Eine einfache Erkl\u00e4rung zur Berechnung der Mahalanobis-Distanz in Python.","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\/mahalanobis-remote-python\/","og_locale":"de_DE","og_type":"article","og_title":"So berechnen Sie die Mahalanobis-Distanz in Python \u2013 Statorials","og_description":"Eine einfache Erkl\u00e4rung zur Berechnung der Mahalanobis-Distanz in Python.","og_url":"https:\/\/statorials.org\/de\/mahalanobis-remote-python\/","og_site_name":"Statorials","article_published_time":"2023-07-28T14:53:35+00:00","author":"Dr. Benjamin Anderson","twitter_card":"summary_large_image","twitter_misc":{"Verfasst von":"Dr. Benjamin Anderson","Gesch\u00e4tzte Lesezeit":"2 Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/de\/mahalanobis-remote-python\/","url":"https:\/\/statorials.org\/de\/mahalanobis-remote-python\/","name":"So berechnen Sie die Mahalanobis-Distanz in Python \u2013 Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/de\/#website"},"datePublished":"2023-07-28T14:53:35+00:00","dateModified":"2023-07-28T14:53:35+00:00","author":{"@id":"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0"},"description":"Eine einfache Erkl\u00e4rung zur Berechnung der Mahalanobis-Distanz in Python.","breadcrumb":{"@id":"https:\/\/statorials.org\/de\/mahalanobis-remote-python\/#breadcrumb"},"inLanguage":"de-DE","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/de\/mahalanobis-remote-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/de\/mahalanobis-remote-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Heim","item":"https:\/\/statorials.org\/de\/"},{"@type":"ListItem","position":2,"name":"So berechnen sie die mahalanobis-distanz in python"}]},{"@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\/829"}],"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=829"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/posts\/829\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/media?parent=829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/categories?post=829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/tags?post=829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}