{"id":537,"date":"2018-06-05T22:13:48","date_gmt":"2018-06-05T13:13:48","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2018\/06\/05\/python_8\/"},"modified":"2018-06-05T22:13:48","modified_gmt":"2018-06-05T13:13:48","slug":"python_8","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2018\/06\/05\/python_8\/","title":{"rendered":"Python \u6570\u5b66\u3001\u7d71\u8a08\u3001\u6a5f\u68b0\u5b66\u7fd2\uff1a\u91cd\u56de\u5e30\u5206\u6790"},"content":{"rendered":"<p><a href=\"https:\/\/www.amazon.co.jp\/Excel%E3%81%A7%E5%AD%A6%E3%81%B6%E5%A4%9A%E5%A4%89%E9%87%8F%E8%A7%A3%E6%9E%90%E5%85%A5%E9%96%80%E2%88%92Excel2013-2010%E5%AF%BE%E5%BF%9C%E7%89%88%E2%88%92-%E8%8F%85-%E6%B0%91%E9%83%8E\/dp\/4274069141\/ref=as_li_ss_il?_encoding=UTF8&amp;pd_rd_i=4274069141&amp;pd_rd_r=1907b681-68c2-11e8-9bf7-ef3983ec0980&amp;pd_rd_w=sULEZ&amp;pd_rd_wg=dJSte&amp;pf_rd_i=desktop-dp-sims&amp;pf_rd_m=AN1VRQENFRJN5&amp;pf_rd_p=7990452376513976631&amp;pf_rd_r=QE155F6J5JJM7NJZN29W&amp;pf_rd_s=desktop-dp-sims&amp;pf_rd_t=40701&amp;psc=1&amp;refRID=QE155F6J5JJM7NJZN29W&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=339535ece70d8a2d20581e159b708d4c\" target=\"_blank\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4274069141&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\" border=\"0\"><\/a><img loading=\"lazy\" decoding=\"async\" width=\"1\" height=\"1\" style=\"margin: 0px !important; border: currentcolor !important; border-image: none !important;\" alt=\"\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=li2&amp;o=9&amp;a=4274069141\" border=\"0\"><\/p>\n<h2>1.\u91cd\u56de\u5e30\u5206\u6790<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2018\/06\/03\/python_2_1\">\u5358\u56de\u5e30\u5206\u6790<\/a>\u3092\u8a66\u3057\u305f\u304c\u3001\u76ee\u7684\u5909\u6570\u306b\u5bfe\u3057\u3066\u3001\u8aac\u660e\u5909\u6570\u304c2\u3064\u4ee5\u4e0a\u3042\u308b\u56de\u5e30\u5206\u6790\u3092\u3001<a href=\"https:\/\/www.albert2005.co.jp\/knowledge\/statistics_analysis\/multivariate_analysis\/multiple_regression\">\u91cd\u56de\u5e30\u5206\u6790<\/a>\u3068\u3044\u3046<\/li>\n<\/ul>\n<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2018\/05\/18\/python_5\">Boston\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u5404\u9805\u76ee\u306e\u5185\u5bb9\u3092\u8aac\u660e\u5909\u6570\u3068\u3057\u3066<\/a>\u3001scikit-learn \u3092\u7528\u3044\u3066<a href=\"https:\/\/pythondatascience.plavox.info\/scikit-learn\/%E7%B7%9A%E5%BD%A2%E5%9B%9E%E5%B8%B0\">\u7dda\u5f62\u56de\u5e30\u30e2\u30c7\u30eb<\/a>\u3092\u4f5c\u6210\u3001\u91cd\u56de\u5e30\u5206\u6790\u3092\u884c\u3063\u3066\u307f\u308b\u3002<\/p>\n<pre class=\"prettyprint linenums\">from sklearn import datasets\r\nimport pandas as pd\r\nimport statsmodels.formula.api as smf\r\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\n\r\ndef multi_regression(*feature_index):\r\n    \"\"\"\r\n    \u91cd\u56de\u5e30\u5206\u6790\r\n    <iframe title=\"Python\u306eStatsModels\u306b\u3088\u308b\u7dda\u5f62\u56de\u5e30\u5206\u6790! \u4ea4\u5dee\u9805\u3082\u30e2\u30c7\u30eb\u306b\u5165\u308c\u3066\u307f\u305f! - Np-Ur\u306e\u30c7\u30fc\u30bf\u5206\u6790\u6559\u5ba4\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" src=\"https:\/\/hatenablog-parts.com\/embed?url=http%3A%2F%2Fwww.randpy.tokyo%2Fentry%2F2017%2F06%2F26%2F153800#?secret=bEw0ozceNx\" data-secret=\"bEw0ozceNx\" scrolling=\"no\" frameborder=\"0\"><\/iframe>\r\n    <iframe title=\"Python\u306eStatsmodels\u3092\u4f7f\u3063\u3066\u56de\u5e30\u5206\u6790\u3092\u884c\u3046 - wonderful cool something\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" src=\"https:\/\/hatenablog-parts.com\/embed?url=http%3A%2F%2Fwcs.hatenablog.com%2Fentry%2F2016%2F11%2F08%2F231703#?secret=S0LFteTvQq\" data-secret=\"S0LFteTvQq\" scrolling=\"no\" frameborder=\"0\"><\/iframe>\r\n    https:\/\/pythondatascience.plavox.info\/scikit-learn\/%E7%B7%9A%E5%BD%A2%E5%9B%9E%E5%B8%B0\r\n    https:\/\/pythondatascience.plavox.info\/matplotlib\/%E6%A3%92%E3%82%B0%E3%83%A9%E3%83%95\r\n    :return:\r\n    \"\"\"\r\n    boston = datasets.load_boston()\r\n    df = pd.DataFrame(boston.data)\r\n    df.columns = boston.feature_names\r\n    df['PRICE'] = pd.DataFrame(boston.target)\r\n\r\n    x = df.loc[:, ['{0}'.format(x) for x in feature_index]]\r\n    y = df['PRICE']\r\n\r\n    model = smf.OLS(y, x)\r\n    result = model.fit()\r\n    print(result.summary())\r\n\r\n    # numpy sort\r\n    # https:\/\/qiita.com\/supersaiakujin\/items\/c580f2aae90818150b35\r\n    desc_idx = np.argsort(result.tvalues.values)[::-1]\r\n    labels = []\r\n    values = []\r\n    for idx in desc_idx:\r\n        labels.append(feature_index[idx])\r\n        values.append(result.tvalues.values[idx])\r\n\r\n    plt.bar(labels, values)\r\n    plt.legend(title=\"r^2(adj) = {0:.6}\".format(result.rsquared_adj))\r\n\r\n    plt.show()\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    multi_regression('CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT')\r\n    # multi_regression('RM', 'AGE', 'TAX', 'B')\r\n<\/pre>\n<p>\u5206\u6790\u7d50\u679c\u306b\u3064\u3044\u3066\u3001summary() \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u3053\u3068\u3067\u3001\u4ee5\u4e0b\u306e\u7d50\u679c\u8868\u304c\u5f97\u3089\u308c\u308b\u3002<\/p>\n<p><pre class=\"prettyprint linenums\">                            OLS Regression Results                            \r\n==============================================================================\r\nDep. Variable:                  PRICE   R-squared:                       0.959\r\nModel:                            OLS   Adj. R-squared:                  0.958\r\nMethod:                 Least Squares   F-statistic:                     891.1\r\nDate:                Mon, 04 Jun 2018   Prob (F-statistic):               0.00\r\nTime:                        22:53:10   Log-Likelihood:                -1523.8\r\nNo. Observations:                 506   AIC:                             3074.\r\nDf Residuals:                     493   BIC:                             3129.\r\nDf Model:                          13                                         \r\nCovariance Type:            nonrobust                                         \r\n==============================================================================\r\n                 coef    std err          t      P&gt;|t|      [0.025      0.975]\r\n------------------------------------------------------------------------------\r\nCRIM          -0.0916      0.034     -2.675      0.008      -0.159      -0.024\r\nZN             0.0487      0.014      3.379      0.001       0.020       0.077\r\nINDUS         -0.0038      0.064     -0.059      0.953      -0.130       0.123\r\nCHAS           2.8564      0.904      3.160      0.002       1.080       4.633\r\nNOX           -2.8808      3.359     -0.858      0.392      -9.481       3.720\r\nRM             5.9252      0.309     19.168      0.000       5.318       6.533\r\nAGE           -0.0072      0.014     -0.523      0.601      -0.034       0.020\r\nDIS           -0.9680      0.196     -4.947      0.000      -1.352      -0.584\r\nRAD            0.1704      0.067      2.554      0.011       0.039       0.302\r\nTAX           -0.0094      0.004     -2.393      0.017      -0.017      -0.002\r\nPTRATIO       -0.3924      0.110     -3.571      0.000      -0.608      -0.177\r\nB              0.0150      0.003      5.561      0.000       0.010       0.020\r\nLSTAT         -0.4170      0.051     -8.214      0.000      -0.517      -0.317\r\n==============================================================================\r\nOmnibus:                      204.050   Durbin-Watson:                   0.999\r\nProb(Omnibus):                  0.000   Jarque-Bera (JB):             1372.527\r\nSkew:                           1.609   Prob(JB):                    9.11e-299\r\nKurtosis:                      10.399   Cond. No.                     8.50e+03\r\n==============================================================================\r\n\r\nWarnings:\r\n[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\r\n[2] The condition number is large, 8.5e+03. This might indicate that there are\r\nstrong multicollinearity or other numerical problems.\r\nNo handles with labels found to put in legend.\r\n<\/pre>\n<p>\u3044\u308d\u3044\u308d\u8b66\u544a\u304c\u51fa\u3066\u3044\u307e\u3059\u304c\u3001\u7d50\u679c\u306e\u898b\u65b9\u3092\u77e5\u308a\u305f\u3044\u3002<\/p>\n<p>\u8aac\u660e\u5909\u6570\u306e\u5404\u9805\u76ee\u306b\u3064\u3044\u3066\u306f\u3001<a href=\"https:\/\/www.typea.info\/blog\/index.php\/2018\/05\/18\/python_5\">Boston\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u5f15\u304d\u7d9a\u304d\u4f7f\u7528<\/a><\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2018\/05\/24\/python_6\">\u57fa\u672c\u7d71\u8a08\u91cf<\/a><\/p>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\">\n<tbody>\n<tr>\n<td valign=\"top\">\u9805\u76ee<\/td>\n<td valign=\"top\">\u5185\u5bb9<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\u6700\u5c0f\u4e8c\u4e57\u56de\u5e30(OLS : Ordinary Least Squares regression)<\/td>\n<td valign=\"top\">\u5206\u6790\u30e2\u30c7\u30eb\u3068\u3057\u3066<a href=\"https:\/\/ja.wikipedia.org\/wiki\/%E6%9C%80%E5%B0%8F%E4%BA%8C%E4%B9%97%E6%B3%95\">\u6700\u5c0f\u4e8c\u4e57\u6cd5<\/a>\u3092\u4f7f\u7528<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\u56de\u5e30\u4fc2\u6570(ceof)<\/td>\n<td valign=\"top\">\u56de\u5e30\u4fc2\u6570\u306f\u5358\u4f4d\u306e\u5f71\u97ff\u3092\u53d7\u3051\u308b\u305f\u3081\u3001\u5f71\u97ff\u5ea6\u306e\u6307\u6a19\u3068\u3057\u3066\u306f\u5229\u7528\u3067\u304d\u306a\u3044<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\u6a19\u6e96\u8aa4\u5dee(std err)<\/td>\n<td valign=\"top\">\u5e73\u5747\u304b\u3089\u306e\u30d0\u30e9\u30c4\u30ad\u3092\u793a\u3059\u6307\u6a19\u3001\u5206\u6563\u306e\u6b63\u306e\u5e73\u65b9\u6839<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">t\u5024(t)<\/td>\n<td valign=\"top\">\u76ee\u7684\u5909\u6570\u306b\u5bfe\u3059\u308b\u305d\u308c\u305e\u308c\u306e\u8aac\u660e\u5909\u6570\u306e\u5f71\u97ff\u5ea6\u3092\u8868\u3059\u3002\u56de\u5e30\u4fc2\u6570\u306f\u5358\u4f4d\u306e\u5f71\u97ff\u3092\u53d7\u3051\u3001\u5f71\u97ff\u5ea6\u306e\u6307\u6a19\u306b\u5229\u7528\u3067\u304d\u306a\u3044\u305f\u3081\u3001\u56de\u5e30\u4fc2\u6570\u3092\u6a19\u6e96\u8aa4\u5dee\u3067\u9664\u3057\u5358\u4f4d\u306e\u5f71\u97ff\u3092\u53d7\u3051\u306a\u3044t\u5024\u3092\u5229\u7528\u3059\u308b\u3002<u>t\u5024\u306e\u7d76\u5bfe\u5024\u304c\u5927\u304d\u3044\u307b\u3069\u3001\u5f71\u97ff\u5ea6\u306e\u5f37\u3044\u8aac\u660e\u5909\u6570\u3068\u3044\u3048\u308b<\/u><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">P-\u5024<\/td>\n<td valign=\"top\"><a href=\"http:\/\/xica.net\/magellan\/marketing-idea\/stats\/tvalue-and-pvalue\/\">p\u5024<\/a> \u306f\u3001\u5168\u304f\u52b9\u679c\u304c\u306a\u3044\u5834\u5408(\u4fc2\u6570=0)\u306b\u3001\u5206\u6790\u7d50\u679c\u3068\u3057\u3066\u51fa\u308b\u4fc2\u6570\u306e\u5024\u304c\u3001A\u4ee5\u4e0a\u306b\u306a\u308b\u78ba\u7387\u3092\u8868\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\u4e0b\u965097.5%<\/td>\n<td valign=\"top\">97.5%\u306e<a href=\"https:\/\/atarimae.biz\/archives\/18904\">\u4fe1\u983c\u533a\u9593<\/a>\u4e0b\u9650\u3002<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\u4e0a\u965097.5%<\/td>\n<td valign=\"top\">97.5%\u306e<a href=\"https:\/\/atarimae.biz\/archives\/18904\">\u4fe1\u983c\u533a\u9593<\/a>\u4e0a\u9650<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\u91cd\u6c7a\u5b9a R2(R-squared)<\/td>\n<td valign=\"top\">\u5bc4\u4e0e\u7387\u307e\u305f\u306f\u6c7a\u5b9a\u4fc2\u6570\u3068\u547c\u3070\u308c\u3001\u56de\u5e30\u5f0f\u5168\u4f53\u306e\u7cbe\u5ea6\u3092\u8868\u3059\u30021\u306b\u8fd1\u3044\u307b\u3069\u5236\u5ea6\u304c\u9ad8\u3044\u3068\u3044\u3048\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\u88dc\u6b63 R2(Adj R-squared)<\/td>\n<td valign=\"top\">\u81ea\u7531\u5ea6\u8abf\u6574\u6e08\u307f\u6c7a\u5b9a\u4fc2\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>t\u5024\u3092\u964d\u9806\u306b\u30bd\u30fc\u30c8\u3057\u3066\u68d2\u30b0\u30e9\u30d5\u306b\u30d7\u30ed\u30c3\u30c8\u3002<\/p>\n<p>\u4f4f\u5b85\u4fa1\u683c\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u3066\u3044\u308b\u5909\u6570\u3092\u5206\u6790\u3067\u304d\u308b\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Python-_14712\/multi_reg.png\"><img loading=\"lazy\" decoding=\"async\" width=\"546\" height=\"345\" title=\"multi_reg\" style=\"display: inline; background-image: none;\" alt=\"multi_reg\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Python-_14712\/multi_reg_thumb.png\" border=\"0\"><\/a><\/p>\n<p><a href=\"https:\/\/www.amazon.co.jp\/Python%E3%81%AB%E3%82%88%E3%82%8B%E3%83%99%E3%82%A4%E3%82%BA%E7%B5%B1%E8%A8%88%E3%83%A2%E3%83%87%E3%83%AA%E3%83%B3%E3%82%B0-PyMC%E3%81%A7%E3%81%AE%E3%83%87%E3%83%BC%E3%82%BF%E5%88%86%E6%9E%90%E5%AE%9F%E8%B7%B5%E3%82%AC%E3%82%A4%E3%83%89-Osvaldo-Martin\/dp\/4320113373\/ref=as_li_ss_il?s=books&amp;ie=UTF8&amp;qid=1528204375&amp;sr=1-3&amp;keywords=python+%E7%B5%B1%E8%A8%88&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=2a55b923b6e62a0d3f706568da711ecc\" target=\"_blank\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4320113373&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\" border=\"0\"><\/a><img loading=\"lazy\" decoding=\"async\" width=\"1\" height=\"1\" style=\"margin: 0px !important; border: currentcolor !important; border-image: none !important;\" alt=\"\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=li2&amp;o=9&amp;a=4320113373\" border=\"0\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u91cd\u56de\u5e30\u5206\u6790 \u5358\u56de\u5e30\u5206\u6790\u3092\u8a66\u3057\u305f\u304c\u3001\u76ee\u7684\u5909\u6570\u306b\u5bfe\u3057\u3066\u3001\u8aac\u660e\u5909\u6570\u304c2\u3064\u4ee5\u4e0a\u3042\u308b\u56de\u5e30\u5206\u6790\u3092\u3001\u91cd\u56de\u5e30\u5206\u6790\u3068\u3044\u3046 Boston\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u5404\u9805\u76ee\u306e\u5185\u5bb9\u3092\u8aac\u660e\u5909\u6570\u3068\u3057\u3066\u3001scikit-learn \u3092\u7528\u3044\u3066\u7dda\u5f62\u56de\u5e30\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\u3001 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[35,86],"tags":[],"class_list":["post-537","post","type-post","status-publish","format-standard","hentry","category-math","category-statistics"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=537"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/537\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}