{"id":536,"date":"2018-06-03T14:11:19","date_gmt":"2018-06-03T05:11:19","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2018\/06\/03\/python_2_1\/"},"modified":"2018-06-03T14:11:19","modified_gmt":"2018-06-03T05:11:19","slug":"python_2_1","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2018\/06\/03\/python_2_1\/","title":{"rendered":"Python \u6570\u5b66\u3001\u7d71\u8a08\u3001\u6a5f\u68b0\u5b66\u7fd2\uff1a\u6563\u5e03\u56f3\u3068\u76f8\u95a2\u5206\u6790(2) \u5358\u56de\u5e30\u5206\u6790"},"content":{"rendered":"<p><a href=\"https:\/\/www.amazon.co.jp\/Excel%E3%81%A7%E5%AD%A6%E3%81%B6%E7%B5%B1%E8%A8%88%E8%A7%A3%E6%9E%90%E5%85%A5%E9%96%80-Excel2016-2013%E5%AF%BE%E5%BF%9C%E7%89%88-%E8%8F%85-%E6%B0%91%E9%83%8E\/dp\/4274218708\/ref=as_li_ss_il?s=books&amp;ie=UTF8&amp;qid=1528001277&amp;sr=1-2&amp;keywords=Excel+%E7%B5%B1%E8%A8%88&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=5bf055297464e74b3bdfbf689e8f794d\" target=\"_blank\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4274218708&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 !important;\" alt=\"\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=li2&amp;o=9&amp;a=4274218708\" border=\"0\"><\/p>\n<h2>1.\u5358\u56de\u5e30\u5206\u6790<\/h2>\n<ul>\n<li>\u5358\u56de\u5e30\u5206\u6790\u3068\u306f\u30012\u3064\u306e\u30c7\u30fc\u30bf\u7fa4\u3092\u300c\u539f\u56e0\u300d\u3068\u300c\u7d50\u679c\u300d\u3067\u3068\u3089\u3048\u305f\u3068\u304d\u306b\u3001\u305d\u306e\u95a2\u4fc2\u3092\u300c\u56de\u5e30\u76f4\u7dda\u300d\u3067\u3042\u3089\u308f\u3059\u3053\u3068\u306e\u3067\u304d\u308b\u5206\u6790\u624b\u6cd5<\/li>\n<li>\u5358\u56de\u5e30\u5f0f y = ax + b \u3092\u6c42\u3081\u308b\u3053\u3068\u3067\u3001\u5024\u3092\u4e88\u6e2c\u3067\u304d\u308b<\/li>\n<\/ul>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\">\n<tbody>\n<tr>\n<td>\u9805\u76ee<\/td>\n<td valign=\"top\">\u5185\u5bb9<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\u8aac\u660e\u5909\u6570(x)<\/td>\n<td valign=\"top\">\u539f\u56e0\u7cfb\u306e\u30c7\u30fc\u30bf<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\u76ee\u7684\u5909\u6570(y)<\/td>\n<td valign=\"top\">\u7d50\u679c\u7cfb\u306e\u30c7\u30fc\u30bf<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\u56de\u5e30\u4fc2\u6570(a)<\/td>\n<td valign=\"top\"><a href=\"https:\/\/www.albert2005.co.jp\/knowledge\/statistics_analysis\/multivariate_analysis\/single_regression\">\u6700\u5c0f\u4e8c\u4e57\u6cd5<\/a>\u306b\u3088\u308b\u50be\u304d<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">\u5207\u7247(b)<\/td>\n<td valign=\"top\"><a href=\"https:\/\/www.albert2005.co.jp\/knowledge\/statistics_analysis\/multivariate_analysis\/single_regression\">\u6700\u5c0f\u4e8c\u4e57\u6cd5<\/a>\u306b\u3088\u308by\u5207\u7247<\/td>\n<\/tr>\n<tr>\n<td>\u6c7a\u5b9a\u4fc2\u6570<\/td>\n<td>\u56de\u5e30\u5f0f\u306e\u7cbe\u5ea6\u5224\u65ad\u57fa\u6e96\u3068\u3057\u3066\u4e00\u822c\u7684\u306b\u300c\u5358\u76f8\u95a2\u4fc2\u6570\u300d\u3092\u4e8c\u4e57\u3057\u305f\u5024\u304c\u4f7f\u308f\u308c\u308b\u3002(\u6c7a\u5b9a\u4fc2\u6570\u3001\u5bc4\u4e0e\u7387\u306a\u3069\u3068\u547c\u3070\u308c\u308b\uff090 \u304b\u3089 1\u306e\u5024\u3092\u3068\u308a\u30011\u306b\u8fd1\u3044\u307b\u3069\u56de\u5e30\u5f0f\u306e\u7cbe\u5ea6\u304c\u3088\u3044(x\u3068y\u306e\u95a2\u4fc2\u304c\u5f37\u3044)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>2.\u8a66\u3057\u3066\u307f\u308b<\/h2>\n<p><a href=\"https:\/\/www.typea.info\/blog\/index.php\/2018\/06\/01\/python_1_1\">Python \u6570\u5b66\u3001\u7d71\u8a08\u3001\u6a5f\u68b0\u5b66\u7fd2\uff1a\u6563\u5e03\u56f3\u3068\u76f8\u95a2\u5206\u6790(1)<\/a> \u3067\u63cf\u3044\u305f\u6563\u5e03\u56f3\u306b\u56de\u5e30\u76f4\u7dda\u3092\u91cd\u306d\u308b<\/p>\n<p><a href=\"https:\/\/pythondatascience.plavox.info\/scikit-learn\/%E7%B7%9A%E5%BD%A2%E5%9B%9E%E5%B8%B0\">scikit-learn \u306b\u542b\u307e\u308c\u308b\u3001\u7dda\u5f62\u56de\u5e30\u30e2\u30c7\u30eb\u3092\u5229\u7528<\/a>\u3059\u308b\u3002<\/p>\n<p><\/p>\n<pre class=\"prettyprint linenums\">from sklearn import datasets\r\nfrom sklearn import linear_model\r\nimport pandas as pd\r\nimport matplotlib.pyplot as plt\r\nimport math\r\n\r\n\r\ndef liner_regression(*feature_index):\r\n    \"\"\"\r\n    https:\/\/pythondatascience.plavox.info\/scikit-learn\/%E7%B7%9A%E5%BD%A2%E5%9B%9E%E5%B8%B0\r\n    :return:\r\n    \"\"\"\r\n\r\n    # \u7dda\u5f62\u56de\u5e30\u4e88\u6e2c\u30af\u30e9\u30b9\r\n    lr = linear_model.LinearRegression()\r\n\r\n    boston = datasets.load_boston()\r\n\r\n    df_ex = pd.DataFrame(boston.data)\r\n    df_ex.columns = boston.feature_names\r\n    import pprint\r\n    print(pprint.pprint(df_ex))\r\n\r\n    df_res = pd.DataFrame(boston.target)\r\n    df_res.columns = ['Price']\r\n\r\n    y = df_res['Price']\r\n    fig = plt.figure()\r\n\r\n    cnt = len(feature_index)\r\n    cols = 1 if cnt == 1 else (2 if cnt &lt; 6 else 4)\r\n    rows = math.ceil(cnt \/ cols)\r\n    idx = 1\r\n    for feature in feature_index:\r\n\r\n        x = df_ex[feature]\r\n        ax = fig.add_subplot(rows, cols, idx)\r\n\r\n        ax.set_title(feature, fontdict={'fontsize': 10}, pad=2)\r\n        ax.scatter(x, y, s=0.5)\r\n\r\n        mat_x = x.reshape((-1, 1))\r\n        mat_y = y.reshape((-1, 1))\r\n\r\n        # \u4e88\u6e2c\u30e2\u30c7\u30eb\u3092\u4f5c\u6210\r\n        # y = ax + b\r\n        lr.fit(mat_x, mat_y)\r\n\r\n        # a:\u56de\u5e30\u4fc2\u6570\r\n        a = lr.coef_\r\n\r\n        # b:\u5207\u7247 (\u8aa4\u5dee)\r\n        b = lr.intercept_\r\n\r\n        # R^2:\u6c7a\u5b9a\u4fc2\u6570\r\n        r2 = lr.score(mat_x, mat_y)\r\n\r\n        # \u4e88\u6e2c\u3092\u5b9f\u884c\r\n        predict = lr.predict(mat_x)\r\n\r\n        # \u4e88\u6e2c\u3092\u30d7\u30ed\u30c3\u30c8\r\n        ax.plot(x, predict, 'k--', lw=0.5)\r\n\r\n        label = \"y = {0:.4f}x + {1:.4f}\\nR^2 = {2:.4f}\".format(a[0][0], b[0], r2)\r\n        ax.text(x.min(), y.min(), label, fontdict={'fontsize': 8})\r\n\r\n        idx = idx + 1\r\n\r\n    plt.tight_layout()\r\n    plt.show()\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    # liner_regression('CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT')\r\n    liner_regression('RM', 'AGE', 'TAX', 'B')<\/pre>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30a8\u30e9\u30fc\u304c\u3067\u308b\u306e\u3067\u3001\u6307\u793a\u306b\u5f93\u3063\u3066\u3001x.reshape(-1, 1) \u3068\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<blockquote>\n<p>Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.<\/p>\n<\/blockquote>\n<p><a title=\"https:\/\/stackoverflow.com\/questions\/35082140\/preprocessing-in-scikit-learn-single-sample-depreciation-warning\" href=\"https:\/\/stackoverflow.com\/questions\/35082140\/preprocessing-in-scikit-learn-single-sample-depreciation-warning\">https:\/\/stackoverflow.com\/questions\/35082140\/preprocessing-in-scikit-learn-single-sample-depreciation-warning<\/a><\/p>\n<p><pre class=\"prettyprint linenums\">&gt;&gt;&gt; import pandas as pd\r\n&gt;&gt;&gt; df = pd.DataFrame([1,2,3])\r\n&gt;&gt;&gt; df.columns = ['x']\r\n&gt;&gt;&gt; x = df['x']\r\nx\r\n0    1\r\n1    2\r\n2    3\r\nName: x, dtype: int64\r\n&gt;&gt;&gt; x.reshape((-1,1))\r\narray([[1],\r\n       [2],\r\n       [3]])<\/pre>\n<p>LinearRegression\u306eAPI\u306b\u3064\u3044\u3066\u4ee5\u4e0b\u30b5\u30a4\u30c8\u304b\u3089\u5f15\u7528\u3055\u305b\u3066\u3082\u3089\u3044\u307e\u3059\u3002<\/p>\n<p><a title=\"https:\/\/pythondatascience.plavox.info\/scikit-learn\/%E7%B7%9A%E5%BD%A2%E5%9B%9E%E5%B8%B0\" href=\"https:\/\/pythondatascience.plavox.info\/scikit-learn\/%E7%B7%9A%E5%BD%A2%E5%9B%9E%E5%B8%B0\">https:\/\/pythondatascience.plavox.info\/scikit-learn\/%E7%B7%9A%E5%BD%A2%E5%9B%9E%E5%B8%B0<\/a><\/p>\n<p><pre class=\"prettyprint linenums\">sklearn.linear_model.LinearRegression(fit_intercept=True, normalize=False,\r\n                                      copy_X=True, n_jobs=1)<\/pre>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\">\n<tbody>\n<tr>\n<td valign=\"top\">\u30d1\u30e9\u30e1\u30fc\u30bf<\/td>\n<td valign=\"top\">\u5185\u5bb9<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">fit_intercept<\/td>\n<td valign=\"top\">False \u306b\u8a2d\u5b9a\u3059\u308b\u3068\u5207\u7247\u3092\u6c42\u3081\u308b\u8a08\u7b97\u3092\u542b\u3081\u306a\u3044\u3002\u76ee\u7684\u5909\u6570\u304c\u539f\u70b9\u3092\u5fc5\u305a\u901a\u308b\u6027\u8cea\u306e\u30c7\u30fc\u30bf\u3092\u6271\u3046\u3068\u304d\u306b\u5229\u7528\u3002 (\u30c7\u30d5\u30a9\u30eb\u30c8\u5024: True)<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">normalize<\/td>\n<td valign=\"top\">True \u306b\u8a2d\u5b9a\u3059\u308b\u3068\u3001\u8aac\u660e\u5909\u6570\u3092\u4e8b\u524d\u306b\u6b63\u898f\u5316\u3057\u307e\u3059\u3002 (\u30c7\u30d5\u30a9\u30eb\u30c8\u5024: False)<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">copy_X<\/td>\n<td valign=\"top\">\u30e1\u30e2\u30ea\u5185\u3067\u30c7\u30fc\u30bf\u3092\u8907\u88fd\u3057\u3066\u304b\u3089\u5b9f\u884c\u3059\u308b\u304b\u3069\u3046\u304b\u3002 (\u30c7\u30d5\u30a9\u30eb\u30c8\u5024: True)<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">n_jobs<\/td>\n<td valign=\"top\">\u8a08\u7b97\u306b\u4f7f\u3046\u30b8\u30e7\u30d6\u306e\u6570\u3002-1 \u306b\u8a2d\u5b9a\u3059\u308b\u3068\u3001\u3059\u3079\u3066\u306e CPU \u3092\u4f7f\u3063\u3066\u8a08\u7b97\u3057\u307e\u3059\u3002 (\u30c7\u30d5\u30a9\u30eb\u30c8\u5024: 1)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\">\n<tbody>\n<tr>\n<td valign=\"top\">\u5c5e\u6027<\/td>\n<td valign=\"top\">\u5185\u5bb9<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">coef_<\/td>\n<td valign=\"top\">\u504f\u56de\u5e30\u4fc2\u6570<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">intercept_<\/td>\n<td valign=\"top\">\u5207\u7247<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\">\n<tbody>\n<tr>\n<td valign=\"top\">\u30e1\u30bd\u30c3\u30c9<\/td>\n<td valign=\"top\">\u5185\u5bb9<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">fit(X, y[, sample_weight])<\/td>\n<td valign=\"top\">\u7dda\u5f62\u56de\u5e30\u30e2\u30c7\u30eb\u306e\u3042\u3066\u306f\u3081\u3092\u5b9f\u884c<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">get_params([deep])<\/td>\n<td valign=\"top\">\u63a8\u5b9a\u306b\u7528\u3044\u305f\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u53d6\u5f97<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">predict(X)<\/td>\n<td valign=\"top\">\u4f5c\u6210\u3057\u305f\u30e2\u30c7\u30eb\u3092\u5229\u7528\u3057\u3066\u4e88\u6e2c\u3092\u5b9f\u884c<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">score(X, y[, sample_weight])<\/td>\n<td valign=\"top\">\u6c7a\u5b9a\u4fc2\u6570 R2\u3092\u51fa\u529b<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">set_params(**params)<\/td>\n<td valign=\"top\">\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u8a2d\u5b9a<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Python-1_B975\/line_reg.png\"><img loading=\"lazy\" decoding=\"async\" width=\"648\" height=\"481\" title=\"line_reg\" style=\"display: inline; background-image: none;\" alt=\"line_reg\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Python-1_B975\/line_reg_thumb.png\" border=\"0\"><\/a><\/p>\n<p><a href=\"https:\/\/www.amazon.co.jp\/Python%E3%83%87%E3%83%BC%E3%82%BF%E3%82%B5%E3%82%A4%E3%82%A8%E3%83%B3%E3%82%B9%E3%83%8F%E3%83%B3%E3%83%89%E3%83%96%E3%83%83%E3%82%AF-%E2%80%95Jupyter%E3%80%81NumPy%E3%80%81pandas%E3%80%81Matplotlib%E3%80%81scikit-learn%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%9F%E3%83%87%E3%83%BC%E3%82%BF%E5%88%86%E6%9E%90%E3%80%81%E6%A9%9F%E6%A2%B0%E5%AD%A6%E7%BF%92-Jake-VanderPlas\/dp\/4873118417\/ref=as_li_ss_il?_encoding=UTF8&amp;pd_rd_i=4873118417&amp;pd_rd_r=e134abb3-6357-11e8-b88d-43678a1b55c8&amp;pd_rd_w=VAMZh&amp;pd_rd_wg=rP3Xn&amp;pf_rd_i=desktop-dp-sims&amp;pf_rd_m=AN1VRQENFRJN5&amp;pf_rd_p=3472031948783866574&amp;pf_rd_r=Q9WTG9Y0NW1E6XB8HAVA&amp;pf_rd_s=desktop-dp-sims&amp;pf_rd_t=40701&amp;psc=1&amp;refRID=Q9WTG9Y0NW1E6XB8HAVA&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=ff59dfe2121b5d6ad9f4ab37f8a0a3ce\" target=\"_blank\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4873118417&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 !important;\" alt=\"\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=li2&amp;o=9&amp;a=4873118417\" border=\"0\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u5358\u56de\u5e30\u5206\u6790 \u5358\u56de\u5e30\u5206\u6790\u3068\u306f\u30012\u3064\u306e\u30c7\u30fc\u30bf\u7fa4\u3092\u300c\u539f\u56e0\u300d\u3068\u300c\u7d50\u679c\u300d\u3067\u3068\u3089\u3048\u305f\u3068\u304d\u306b\u3001\u305d\u306e\u95a2\u4fc2\u3092\u300c\u56de\u5e30\u76f4\u7dda\u300d\u3067\u3042\u3089\u308f\u3059\u3053\u3068\u306e\u3067\u304d\u308b\u5206\u6790\u624b\u6cd5 \u5358\u56de\u5e30\u5f0f y = ax + b \u3092\u6c42\u3081\u308b\u3053\u3068\u3067\u3001\u5024\u3092\u4e88\u6e2c\u3067\u304d\u308b \u9805\u76ee \u5185\u5bb9 \u8aac\u660e\u5909 [&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-536","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\/536","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=536"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/536\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}