{"id":1113,"date":"2023-07-27T15:00:08","date_gmt":"2023-07-27T15:00:08","guid":{"rendered":"https:\/\/statorials.org\/de\/kosinusahnlichkeit-python\/"},"modified":"2023-07-27T15:00:08","modified_gmt":"2023-07-27T15:00:08","slug":"kosinusahnlichkeit-python","status":"publish","type":"post","link":"https:\/\/statorials.org\/de\/kosinusahnlichkeit-python\/","title":{"rendered":"So berechnen sie die kosinus\u00e4hnlichkeit in python"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\"><strong>Kosinus\u00e4hnlichkeit<\/strong> ist ein Ma\u00df f\u00fcr die \u00c4hnlichkeit zwischen zwei Vektoren eines inneren Produktraums.<\/span><\/p>\n<p> <span style=\"color: #000000;\">F\u00fcr zwei Vektoren A und B wird die Kosinus\u00e4hnlichkeit wie folgt berechnet:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>Kosinus\u00e4hnlichkeit<\/strong> = \u03a3A <sub>i<\/sub> B <sub>i<\/sub> \/ (\u221a\u03a3A <sub>i<\/sub> <sup>2<\/sup> \u221a\u03a3B <sub>i<\/sub> <sup>2<\/sup> )<\/span><\/p>\n<p> <span style=\"color: #000000;\">In diesem Tutorial wird erl\u00e4utert, wie Sie die Kosinus\u00e4hnlichkeit zwischen Vektoren in Python mithilfe von Funktionen aus der <a href=\"https:\/\/numpy.org\/doc\/stable\/index.html\" target=\"_blank\" rel=\"noopener noreferrer\">NumPy-<\/a> Bibliothek berechnen.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Kosinus\u00e4hnlichkeit zwischen zwei Vektoren in Python<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">Der folgende Code zeigt, wie die Kosinus\u00e4hnlichkeit zwischen zwei Arrays in Python berechnet wird:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">from<\/span> numpy <span style=\"color: #008000;\">import<\/span> dot\n<span style=\"color: #008000;\">from<\/span> numpy. <span style=\"color: #3366ff;\">linalg<\/span> <span style=\"color: #008000;\">import<\/span> norm\n\n<span style=\"color: #008080;\">#define arrays\n<\/span>a = [23, 34, 44, 45, 42, 27, 33, 34]\nb = [17, 18, 22, 26, 26, 29, 31, 30]\n\n<span style=\"color: #008080;\">#calculate Cosine Similarity\n<\/span>cos_sim = <span style=\"color: #3366ff;\">dot<\/span> (a, b)\/( <span style=\"color: #3366ff;\">norm<\/span> (a)* <span style=\"color: #3366ff;\">norm<\/span> (b))\n\ncos_sim\n\n0.965195008357566\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Die Kosinus\u00e4hnlichkeit zwischen den beiden Tabellen betr\u00e4gt <strong>0,965195<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Beachten Sie, dass diese Methode f\u00fcr zwei Arrays beliebiger L\u00e4nge funktioniert:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import <span style=\"color: #000000;\">numpy<\/span> as <span style=\"color: #000000;\">np<\/span>\nfrom<\/span> numpy <span style=\"color: #008000;\">import<\/span> dot\n<span style=\"color: #008000;\">from<\/span> numpy. <span style=\"color: #3366ff;\">linalg<\/span> <span style=\"color: #008000;\">import<\/span> norm\n\n<span style=\"color: #008080;\">#define arrays\n<\/span>a = np.random.randint(10, size= <span style=\"color: #008000;\">100<\/span> )\nb = np.random.randint(10, size= <span style=\"color: #008000;\">100<\/span> )\n\n<span style=\"color: #008080;\">#calculate Cosine Similarity\n<\/span>cos_sim = <span style=\"color: #3366ff;\">dot<\/span> (a, b)\/( <span style=\"color: #3366ff;\">norm<\/span> (a)* <span style=\"color: #3366ff;\">norm<\/span> (b))\n\ncos_sim\n\n0.7340201613960431<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Dies funktioniert jedoch nur, wenn die beiden Arrays gleich lang sind:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008000;\">import <span style=\"color: #000000;\">numpy<\/span> as <span style=\"color: #000000;\">np<\/span>\nfrom<\/span> numpy <span style=\"color: #008000;\">import<\/span> dot\n<span style=\"color: #008000;\">from<\/span> numpy. <span style=\"color: #3366ff;\">linalg<\/span> <span style=\"color: #008000;\">import<\/span> norm\n\n<span style=\"color: #008080;\">#define arrays\n<\/span>a = np.random.randint(10, size= <span style=\"color: #008000;\">90<\/span> ) <span style=\"color: #008080;\">#length=90<\/span>\nb = np.random.randint(10, size= <span style=\"color: #008000;\">100<\/span> ) <span style=\"color: #008080;\">#length=100<\/span>\n\n<span style=\"color: #008080;\">#calculate Cosine Similarity\n<\/span>cos_sim = <span style=\"color: #3366ff;\">dot<\/span> (a, b)\/( <span style=\"color: #3366ff;\">norm<\/span> (a)* <span style=\"color: #3366ff;\">norm<\/span> (b))\n\ncos_sim\n\n<span style=\"color: #993300;\">ValueError<\/span> : shapes (90,) and (100,) not aligned: 90 (dim 0) != 100 (dim 0)\n<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Kommentare<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\"><strong>1.<\/strong> Es gibt mehrere M\u00f6glichkeiten, die Kosinus\u00e4hnlichkeit mit Python zu berechnen, aber wie <a href=\"https:\/\/stackoverflow.com\/questions\/18424228\/cosine-similarity-between-2-number-lists\" target=\"_blank\" rel=\"noopener noreferrer\">dieser Stack Overflow-Thread<\/a> erkl\u00e4rt, erweist sich die in diesem Artikel erl\u00e4uterte Methode als die schnellste.<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>2.<\/strong> Weitere Informationen zur Kosinus\u00e4hnlichkeit finden Sie auf<a href=\"https:\/\/en.wikipedia.org\/wiki\/Cosine_similarity\" target=\"_blank\" rel=\"noopener noreferrer\">dieser Wikipedia-Seite<\/a> .<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Kosinus\u00e4hnlichkeit ist ein Ma\u00df f\u00fcr die \u00c4hnlichkeit zwischen zwei Vektoren eines inneren Produktraums. F\u00fcr zwei Vektoren A und B wird die Kosinus\u00e4hnlichkeit wie folgt berechnet: Kosinus\u00e4hnlichkeit = \u03a3A i B i \/ (\u221a\u03a3A i 2 \u221a\u03a3B i 2 ) In diesem Tutorial wird erl\u00e4utert, wie Sie die Kosinus\u00e4hnlichkeit zwischen Vektoren in Python mithilfe von Funktionen [&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 Kosinus\u00e4hnlichkeit in Python - Statorials<\/title>\n<meta name=\"description\" content=\"In diesem Tutorial wird anhand eines Beispiels erkl\u00e4rt, wie man den Kosinus in Python auf die gleiche Weise berechnet.\" \/>\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\/kosinusahnlichkeit-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 Kosinus\u00e4hnlichkeit in Python - Statorials\" \/>\n<meta property=\"og:description\" content=\"In diesem Tutorial wird anhand eines Beispiels erkl\u00e4rt, wie man den Kosinus in Python auf die gleiche Weise berechnet.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/de\/kosinusahnlichkeit-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-27T15:00:08+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=\"1 Minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/de\/kosinusahnlichkeit-python\/\",\"url\":\"https:\/\/statorials.org\/de\/kosinusahnlichkeit-python\/\",\"name\":\"So berechnen Sie die Kosinus\u00e4hnlichkeit in Python - Statorials\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/de\/#website\"},\"datePublished\":\"2023-07-27T15:00:08+00:00\",\"dateModified\":\"2023-07-27T15:00:08+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0\"},\"description\":\"In diesem Tutorial wird anhand eines Beispiels erkl\u00e4rt, wie man den Kosinus in Python auf die gleiche Weise berechnet.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/de\/kosinusahnlichkeit-python\/#breadcrumb\"},\"inLanguage\":\"de-DE\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/de\/kosinusahnlichkeit-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/de\/kosinusahnlichkeit-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Heim\",\"item\":\"https:\/\/statorials.org\/de\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"So berechnen sie die kosinus\u00e4hnlichkeit 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 Kosinus\u00e4hnlichkeit in Python - Statorials","description":"In diesem Tutorial wird anhand eines Beispiels erkl\u00e4rt, wie man den Kosinus in Python auf die gleiche Weise berechnet.","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\/kosinusahnlichkeit-python\/","og_locale":"de_DE","og_type":"article","og_title":"So berechnen Sie die Kosinus\u00e4hnlichkeit in Python - Statorials","og_description":"In diesem Tutorial wird anhand eines Beispiels erkl\u00e4rt, wie man den Kosinus in Python auf die gleiche Weise berechnet.","og_url":"https:\/\/statorials.org\/de\/kosinusahnlichkeit-python\/","og_site_name":"Statorials","article_published_time":"2023-07-27T15:00:08+00:00","author":"Dr. Benjamin Anderson","twitter_card":"summary_large_image","twitter_misc":{"Verfasst von":"Dr. Benjamin Anderson","Gesch\u00e4tzte Lesezeit":"1 Minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/de\/kosinusahnlichkeit-python\/","url":"https:\/\/statorials.org\/de\/kosinusahnlichkeit-python\/","name":"So berechnen Sie die Kosinus\u00e4hnlichkeit in Python - Statorials","isPartOf":{"@id":"https:\/\/statorials.org\/de\/#website"},"datePublished":"2023-07-27T15:00:08+00:00","dateModified":"2023-07-27T15:00:08+00:00","author":{"@id":"https:\/\/statorials.org\/de\/#\/schema\/person\/ec75c4d6365f2708f8a0ad3a42121aa0"},"description":"In diesem Tutorial wird anhand eines Beispiels erkl\u00e4rt, wie man den Kosinus in Python auf die gleiche Weise berechnet.","breadcrumb":{"@id":"https:\/\/statorials.org\/de\/kosinusahnlichkeit-python\/#breadcrumb"},"inLanguage":"de-DE","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/de\/kosinusahnlichkeit-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/de\/kosinusahnlichkeit-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Heim","item":"https:\/\/statorials.org\/de\/"},{"@type":"ListItem","position":2,"name":"So berechnen sie die kosinus\u00e4hnlichkeit 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\/1113"}],"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=1113"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/posts\/1113\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/media?parent=1113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/categories?post=1113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/de\/wp-json\/wp\/v2\/tags?post=1113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}