{"id":453,"date":"2019-06-27T00:03:08","date_gmt":"2019-06-26T15:03:08","guid":{"rendered":"https:\/\/obenkyolab.com\/?p=453"},"modified":"2020-05-19T22:56:04","modified_gmt":"2020-05-19T13:56:04","slug":"%e3%80%90python%e3%80%91%e6%b1%ba%e5%ae%9a%e4%bf%82%e6%95%b0%e3%81%a8%e7%9b%b8%e9%96%a2%e4%bf%82%e6%95%b0%e3%81%ae%e8%a8%88%e7%ae%97","status":"publish","type":"post","link":"https:\/\/obenkyolab.com\/?p=453","title":{"rendered":"\u3010Python\u3011\u6c7a\u5b9a\u4fc2\u6570\u3068\u76f8\u95a2\u4fc2\u6570\u306e\u8a08\u7b97"},"content":{"rendered":"\n\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/obenkyolab.com\/?p=453\/#%E7%9B%AE%E7%9A%84\" >\u76ee\u7684<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/obenkyolab.com\/?p=453\/#%E6%B1%BA%E5%AE%9A%E4%BF%82%E6%95%B0%E3%81%A8%E3%81%AF%EF%BC%9F\" >\u6c7a\u5b9a\u4fc2\u6570\u3068\u306f\uff1f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/obenkyolab.com\/?p=453\/#Python%E3%81%AB%E3%82%88%E3%82%8B%E6%B1%BA%E5%AE%9A%E4%BF%82%E6%95%B0%E3%81%AE%E8%A8%88%E7%AE%97\" >Python\u306b\u3088\u308b\u6c7a\u5b9a\u4fc2\u6570\u306e\u8a08\u7b97<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/obenkyolab.com\/?p=453\/#%E7%9B%B8%E9%96%A2%E4%BF%82%E6%95%B0\" >\u76f8\u95a2\u4fc2\u6570<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%9B%AE%E7%9A%84\"><\/span>\u76ee\u7684<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u524d\u56de\u3001\u3042\u308b\u30c7\u30fc\u30bfX\u304b\u3089\u30c7\u30fc\u30bfY\u306e\u95a2\u4fc2\u3092\u5358\u56de\u5e30\u306b\u3088\u308b\u7dda\u5f62\u65b9\u7a0b\u5f0f\u3067\u8868\u3059\u3053\u3068\u3092\u884c\u3044\u307e\u3057\u305f\u3002\u305f\u3060\u3057\u3001\u3053\u308c\u306f\u3042\u304f\u307e\u3067\u8fd1\u4f3c\u3067\u3059\u306e\u3067\u3001\u3069\u308c\u304f\u3089\u3044\u5b9f\u969b\u306e\u30c7\u30fc\u30bf\u3092\u3046\u307e\u304f\u8fd1\u4f3c\uff08\u8aac\u660e\uff09\u3067\u304d\u3066\u3044\u308b\u304b\u3092\u8a55\u4fa1\u3057\u305f\u3044\u3001\u3068\u601d\u3046\u306e\u304c\u81ea\u7136\u304b\u3068\u601d\u3044\u307e\u3059\u3002<br>\u4eca\u56de\u306f\u5358\u56de\u5e30\u3067\u30c7\u30fc\u30bf\u3092\u8868\u3057\u305f\u969b\u306e\u30d5\u30a3\u30c3\u30c8\u5177\u5408\u3092\u8a55\u4fa1\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u5b9f\u8df5\u3057\u306a\u304c\u3089\u52c9\u5f37\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%B1%BA%E5%AE%9A%E4%BF%82%E6%95%B0%E3%81%A8%E3%81%AF%EF%BC%9F\"><\/span>\u6c7a\u5b9a\u4fc2\u6570\u3068\u306f\uff1f<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u56de\u5e30\u3067\u6c42\u3081\u305f\u4e88\u6e2c\u5f0f\u304c\u3069\u308c\u304f\u3089\u3044\u5143\u306e\u30c7\u30fc\u30bf\u306b\u30d5\u30a3\u30c3\u30c8\u3057\u3066\u3044\u308b\u304b\u3092\u898b\u308b\u305f\u3081\u306e\u6307\u6a19\u3068\u3057\u3066<strong>\u6c7a\u5b9a\u4fc2\u6570<\/strong>\u3068\u3044\u3046\u3082\u306e\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u6c7a\u5b9a\u4fc2\u6570\u306e\u8003\u3048\u65b9\u3068\u3057\u3066\u306f\u3001\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<br>\u30c7\u30fc\u30bf\\(x_i\\)\u3068\\(y_i\\)\u306b\u3064\u3044\u3066\u5358\u56de\u5e30\u3092\u8003\u3048\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u4e00\u6b21\u306e\u7dda\u5f62\u56de\u5e30\u306e\u5f0f\u304c\u6c42\u307e\u3063\u305f\u3068\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>$$y=ax+b$$<\/p>\n\n\n\n<p>\u3053\u3053\u3067\u3042\u308b\u30c7\u30fc\u30bf\\(x_i\\)\u3092\u4e0a\u8a18\u306e\u5f0f\u306b\u4ee3\u5165\u3059\u308b\u3068\u3001\\(y_{x_i}=ax_i+b\\)\u3068\u306a\u308a\u307e\u3059\u3002<br>\u3053\u306e\\(y_{x_i}\\)\u306f\u7dda\u5f62\u56de\u5e30\u306e\u8fd1\u4f3c\u5f0f\u3067\u6c42\u307e\u308b\\(x=x_i\\)\u306e\u6642\u306e\u4e88\u6e2c\u5024\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u6b21\u306b\u3053\u306e\u4e88\u6e2c\u5024\u3068\u5b9f\u969b\u306ey_i\u3068\u306e\u8aa4\u5dee\u3092\u8a08\u7b97\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>$$y_i-y_{x_i}=y_i-(ax_i+b)$$<\/p>\n\n\n\n<p>\u3053\u308c\u306f\u3042\u308b\u4e00\u70b9\u306e\\(x_i\\)\u306b\u304a\u3051\u308b\u8aa4\u5dee\u306a\u306e\u3067\u3001\u3053\u308c\u3092\u4e8c\u4e57\u3057\u3066\u5168\u4f53\u306b\u308f\u305f\u3063\u3066\u8db3\u3057\u3001\u5e73\u5747\u3059\u308b\u3068\u3059\u3079\u3066\u306e\u30c7\u30fc\u30bf\u3092\u52a0\u5473\u3057\u305f\u5206\u6563\u306e\u3088\u3046\u306a\u30d0\u30e9\u3064\u304d\u3092\u8a55\u4fa1\u3067\u304d\u308b\u6307\u6a19\u306b\u306a\u308a\u307e\u3059\u3002\u305d\u308c\u304c\u6b21\u306e\u5f0f\u3067\u3059\u3002<\/p>\n\n\n\n<p>$$s_{yx}^2 =\\frac{1}{n}\\sum_{i=1}^n\\{ y_i-(ax_i+b) \\}^2 $$<\/p>\n\n\n\n<p>\u3053\u308c\u306b\u5bfe\u3057\u3001\u30c7\u30fc\u30bf\\(y\\)\u306e\u5206\u6563\\(s_y^2\\)\u306f\u5358\u306b\u4ee5\u4e0b\u306e\u5f0f\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>$$s_y^2 =  \\frac{1}{n}\\sum_{i=1}^n (y_i- \\overline{y})^2$$<\/p>\n\n\n\n<p>\u305d\u3057\u3066\u3053\u308c\u3089\u306e\u5dee\u5206\\(s_r^2\\)\u3092\u4ee5\u4e0b\u306e\u5f0f\u3067\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>$$s_r^2 = s_y^2-s_{yx}^2$$<\/p>\n\n\n\n<p>\u3053\u306e\u5f0f\u306e\u610f\u5473\u3068\u3057\u3066\u306f\u901a\u5e38\u306e\u30c7\u30fc\u30bf\\(y\\)\u306e\u5206\u6563\u304b\u3089\u3001\u5358\u56de\u5e30\u3067\u30c7\u30fc\u30bf\\(y\\)\u3092\u8fd1\u4f3c\u3057\u305f\u6642\u306b\u306a\u304a\u6b8b\u308b\u5206\u6563\u5024\u3092\u5f15\u3044\u3066\u3044\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u6b8b\u3063\u3066\u3044\u308b\u306e\u306f\u30c7\u30fc\u30bf\\(x\\)\u306e\u5f71\u97ff\u306b\u3088\u308b\u504f\u5dee\u5206\u3068\u3044\u3046\u89e3\u91c8\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u306e\\(s_r^2\\)\u3068\\(s_y^2\\)\u306e\u6bd4\u3092\u3068\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\\(y\\)\u306e\u5206\u6563\u306e\u3046\u3061\u306e\u3069\u308c\u3060\u3051\u304c\u30c7\u30fc\u30bf\\(x\\)\u306e\u5f71\u97ff\u3092\u53d7\u3051\u3066\u3044\u308b\u304b\u3092\u6e2c\u308c\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3053\u308c\u304c\u6c7a\u5b9a\u4fc2\u6570\\(r^2\\)\u3067\u3059\u3002<\/p>\n\n\n\n<p>$$r^2 = \\frac{s_r^2}{s_y^2}$$<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Python%E3%81%AB%E3%82%88%E3%82%8B%E6%B1%BA%E5%AE%9A%E4%BF%82%E6%95%B0%E3%81%AE%E8%A8%88%E7%AE%97\"><\/span>Python\u306b\u3088\u308b\u6c7a\u5b9a\u4fc2\u6570\u306e\u8a08\u7b97<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5b9f\u969b\u306e\u30c7\u30fc\u30bf\u306b\u5bfe\u3057python\u306e\u8a08\u7b97\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30c7\u30fc\u30bf\u306f\u524d\u56de\u3001\u5358\u56de\u5e30\u3092\u8a08\u7b97\u3057\u305f\u6642\u3068\u540c\u69d8\u306bkaggle\u306e\u4ee5\u4e0b\u3067\u516c\u958b\u3055\u308c\u3066\u3044\u308b\u30aa\u30fc\u30d7\u30f3\u30c7\u30fc\u30bf\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002 <a href=\"https:\/\/www.kaggle.com\/testpython\/linear-regression\">https:\/\/www.kaggle.com\/testpython\/linear-regression<\/a> <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n#\u30c7\u30fc\u30bf\u8aad\u307f\u8fbc\u307f\ndf = pd.read_csv(&#039;C:\\\\Users\\\\Yusan\\\\Desktop\\\\dataset\\\\test.csv&#039;,engine  =&#039;python&#039;)\n\n# scikit-learn\u306e linear_model\u3092\u8aad\u307f\u8fbc\u307f\nfrom sklearn import linear_model\n\n#\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u4f5c\u6210\nclf = linear_model.LinearRegression()\n\n# x\u8ef8\u30c7\u30fc\u30bf\nx = df&#x5B;&#x5B;&#039;x&#039;]].values\n \n# y\u8ef8\u30c7\u30fc\u30bf\ny = df&#x5B;&#039;y&#039;].values\n \n# \u5358\u56de\u5e30\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\nclf.fit(x,y)\n \n# y=ax+b\u306ea\na = clf.coef_\n\n# y=ax+b\u306eb\nb = clf.intercept_\n\n#y\u306e\u6a19\u672c\u5206\u6563\nsy2 = df&#x5B;&#039;y&#039;].var(ddof=False)\n\n#\u7dda\u5f62\u56de\u5e30\u5f0f\u3068\u306e\u8aa4\u5dee\u3092\u8a08\u7b97\nd = df&#x5B;&#039;y&#039;]-a*df&#x5B;&#039;x&#039;]-b\n\n#\u8aa4\u5dee\u306e2\u4e57\u5e73\u5747\u3092\u8a08\u7b97\nsyx2 = np.mean(d**2)\n\n#sr^2\u3092\u8a08\u7b97\nsr2 = sy2 - syx2\n\n#\u6c7a\u5b9a\u4fc2\u6570\u8a08\u7b97\nr2 = sr2\/sy2\n\nprint(r2) #0.9891203611402715\n<\/pre><\/div>\n\n\n<p>\u3053\u306e\\(r\\)\u306e\u5024\u304c1\u306b\u8fd1\u3044\u307b\u3069\u30c7\u30fc\u30bf\\(y\\)\u304c\u30c7\u30fc\u30bf\\(x\\)\u306e\u5f71\u97ff\u3092\u53d7\u3051\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%9B%B8%E9%96%A2%E4%BF%82%E6%95%B0\"><\/span>\u76f8\u95a2\u4fc2\u6570<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u76f8\u95a2\u4fc2\u6570\\(r\\)\u306f\u6c7a\u5b9a\u4fc2\u6570\\(r^2\\)\u306e\u5e73\u65b9\u6839\u306b\u5f53\u305f\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>$$      r = \\begin{cases}    \\sqrt{r^2}\\ \\ \\ (a&gt;0) \\\\    -\\sqrt{r^2}\\ \\ \\ (a&lt;0)   \\end{cases} $$<\/p>\n\n\n\n<p>python\u3067\u306e\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.sqrt(r2) #0.9945453037143513\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u76ee\u7684 \u524d\u56de\u3001\u3042\u308b\u30c7\u30fc\u30bfX\u304b\u3089\u30c7\u30fc\u30bfY\u306e\u95a2\u4fc2\u3092\u5358\u56de\u5e30\u306b\u3088\u308b\u7dda\u5f62\u65b9\u7a0b\u5f0f\u3067&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":853,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"ja","_original_post":"https:\/\/obenkyolab.com\/?p=453","footnotes":""},"categories":[7],"tags":[],"class_list":["post-453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","ja"],"_links":{"self":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=453"}],"version-history":[{"count":30,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/453\/revisions"}],"predecessor-version":[{"id":631,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/posts\/453\/revisions\/631"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=\/wp\/v2\/media\/853"}],"wp:attachment":[{"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/obenkyolab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}