{"id":1985,"date":"2019-06-25T16:06:50","date_gmt":"2019-06-25T07:06:50","guid":{"rendered":"https:\/\/www.bi.appi.keio.ac.jp\/?p=1985"},"modified":"2024-08-20T18:14:06","modified_gmt":"2024-08-20T09:14:06","slug":"%e4%b8%bb%e6%88%90%e5%88%86%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.bi.appi.keio.ac.jp\/?p=1985","title":{"rendered":"\u4e3b\u6210\u5206\u5206\u6790"},"content":{"rendered":"\n<p>\u7269\u7406\u60c5\u5831\u6570\u5b66B\u3067\u8aac\u660e\u3057\u3066\u3044\u3066\u3044\u308b\u5bfe\u79f0\u884c\u5217\u304c\u51fa\u3066\u304f\u308b\u4f8b\u3067\u3059\uff0e\u56fa\u6709\u30d9\u30af\u30c8\u30eb\u3078\u306e\u5c04\u5f71\u3082\u95a2\u4fc2\u3057\u307e\u3059\uff0e<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#\n# \u4e3b\u6210\u5206\u5206\u6790\n#\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.decomposition import PCA\n\nN = 100\nd = 16\nx = 50 + np.random.randn(N) * d\ny = 0.8 * x + np.random.randn(N) * d * 0.5 + 10\nplt.figure(figsize=(5, 5))\nplt.xlim([0, 100])\nplt.ylim([0, 100])\nplt.xlabel('x')\nplt.ylabel('y')\nplt.scatter(x, y, color='blue', alpha=0.5)\n\nfeatures = np.vstack([x, y]).T\nfeatures2 = features.copy()\nave = np.average(features,0)\nprint(\"\u5e73\u5747\")\nprint(ave)\nfeatures = features - ave\ncv = np.dot(features.T, features) \/ (N - 1)\nprint(\"\u5171\u5206\u6563\u884c\u5217\")\nprint(cv)\nW, v = np.linalg.eig(cv)\nprint(\"\u5171\u5206\u6563\u884c\u5217\u306e\u56fa\u6709\u30d9\u30af\u30c8\u30eb\u3092\u5217\u306b\u3082\u3064\u884c\u5217\")\nprint(\"{0}\".format(v))\nprint(\"\u56fa\u6709\u30d9\u30af\u30c8\u30eb\")\nprint(\"{0}\".format(v[:,0].reshape(2,1)))\nprint(\"\u56fa\u6709\u30d9\u30af\u30c8\u30eb\")\nprint(\"{0}\".format(v[:,1].reshape(2,1)))\nprint(\"\u5171\u5206\u6563\u884c\u5217\u306e\u56fa\u6709\u5024\uff1a{0}\".format(W))\n# \u5bc4\u4e0e\u7387\u306f\u56fa\u6709\u5024\uff0f\uff08\u56fa\u6709\u5024\u306e\u7dcf\u548c\uff09\np = np.trace(cv)\nprint(\"\u5bc4\u4e0e\u73871: {0}, 2: {1}\".format(W[0]\/p, W[1]\/p))\n# \u56fa\u6709\u5024\u306e\u5927\u304d\u3044\u9806\u306b\u30bd\u30fc\u30c8\nind = np.argsort(W)[::-1]\nsortedW = np.sort(W)[::-1]\nsortedv = v[:,ind]\n# \u56fa\u6709\u30d9\u30af\u30c8\u30eb\u65b9\u5411\u306e\u76f4\u7dda\nplt.plot(np.array([0, 100]), np.array([sortedv[1,0]\/sortedv[0,0]*(0 - ave[0])+ave[1], sortedv[1,0]\/sortedv[0,0]*(100 - ave[0])+ave[1]]),  color='blue', alpha=0.5)\nplt.plot(np.array([0, 100]), np.array([sortedv[1,1]\/sortedv[0,1]*(0 - ave[0])+ave[1], sortedv[1,1]\/sortedv[0,1]*(100 - ave[0])+ave[1]]),  color='blue', alpha=0.5)\n#\nt1 = np.zeros([2, N])\n# \u56fa\u6709\u30d9\u30af\u30c8\u30eb\u65b9\u5411\u306b\u5c04\u5f71\u3059\u308b\n#p1 = v[:,0].reshape(2,1)\n#p2 = v[:,1].reshape(2,1)\n#for i in range(N):\n#  t1[0, i] = np.dot(p1.T, (features.T[:,i]).reshape(2,1)) # \u5c04\u5f71\u3059\u308b\u30d9\u30af\u30c8\u30eb\u306f\u5358\u4f4d\u30d9\u30af\u30c8\u30eb\u306a\u306e\u30672\u4e57\u30ce\u30eb\u30e0\u306e2\u4e57\u3067\u5272\u308b\u5fc5\u8981\u306a\u3057\n#  t1[1, i] = np.dot(p2.T, (features.T[:,i]).reshape(2,1))\nt1 = np.dot(sortedv.T, features.T)\n#\nplt.figure(figsize=(5, 5))\nplt.xlim([-70, 70])\nplt.ylim([-70, 70])\nplt.xlabel('p1')\nplt.ylabel('p2')\nplt.scatter(t1[0,:], t1[1,:], color='red', alpha=0.5)\n# 3\u03c3\u306e\u6955\u5186\ntheta = np.arange(0, 361, 3)\nX = np.cos(theta\/180*np.pi) * np.sqrt(sortedW[0]) * 3\nY = np.sin(theta\/180*np.pi) * np.sqrt(sortedW[1]) * 3\nplt.plot(X, Y, color='red', alpha=0.8)\nplt.show()\n#\n#\nprint(\"\u4e3b\u6210\u5206\u5206\u6790\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3046\")\npca = PCA(n_components=2)\npca.fit(features2)\nprint(\"components\")\nprint(\"{0}\".format(pca.components_))\nprint(\"mean={0}\".format(pca.mean_))\nprint(\"covariance\")\nprint(\"{0}\".format(pca.get_covariance()))\nt2 = pca.fit_transform(features)\nprint(\"contribution\")\nprint(pca.explained_variance_ratio_)\nplt.figure(figsize=(5, 5))\nplt.xlim([-70, 70])\nplt.ylim([-70, 70])\nplt.xlabel('p1')\nplt.ylabel('p2')\nplt.scatter(t2[:,0], t2[:,1], color='green', alpha=0.5)\nprint(\"\u56fa\u6709\u30d9\u30af\u30c8\u30eb\u306e\u7b26\u53f7\u304c\u53cd\u8ee2\u3057\u3066\u3044\u308b\u3068\u5de6\u53f3\u30fb\u4e0a\u4e0b\u306b\u53cd\u8ee2\u304c\u3042\u308b\")\nplt.show()<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u5e73\u5747\n[52.53235793 51.86342693]\n\u5171\u5206\u6563\u884c\u5217\n[[244.04297958 193.42743647]\n [193.42743647 216.02097448]]\n\u5171\u5206\u6563\u884c\u5217\u306e\u56fa\u6709\u30d9\u30af\u30c8\u30eb\u3092\u5217\u306b\u3082\u3064\u884c\u5217\n[[ 0.73220426 -0.6810851 ]\n [ 0.6810851   0.73220426]]\n\u56fa\u6709\u30d9\u30af\u30c8\u30eb\n[[0.73220426]\n [0.6810851 ]]\n\u56fa\u6709\u30d9\u30af\u30c8\u30eb\n[[-0.6810851 ]\n [ 0.73220426]]\n\u5171\u5206\u6563\u884c\u5217\u306e\u56fa\u6709\u5024\uff1a[423.96619622  36.09775785]\n\u5bc4\u4e0e\u73871: 0.9215375220555805, 2: 0.07846247794441952<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"342\" height=\"321\" src=\"https:\/\/www.bi.appi.keio.ac.jp\/wordpress\/wp-content\/uploads\/2019\/06\/pca1.png\" alt=\"\" class=\"wp-image-1987\" srcset=\"https:\/\/www.bi.appi.keio.ac.jp\/wordpress\/wp-content\/uploads\/2019\/06\/pca1.png 342w, https:\/\/www.bi.appi.keio.ac.jp\/wordpress\/wp-content\/uploads\/2019\/06\/pca1-300x282.png 300w\" sizes=\"auto, (max-width: 342px) 85vw, 342px\" \/><figcaption class=\"wp-element-caption\">\u76f4\u7dda\u306f\u56fa\u6709\u30d9\u30af\u30c8\u30eb\u306e\u65b9\u5411\u3092\u8868\u3059<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"338\" height=\"320\" src=\"https:\/\/www.bi.appi.keio.ac.jp\/wordpress\/wp-content\/uploads\/2019\/06\/pca2.png\" alt=\"\" class=\"wp-image-1988\" srcset=\"https:\/\/www.bi.appi.keio.ac.jp\/wordpress\/wp-content\/uploads\/2019\/06\/pca2.png 338w, https:\/\/www.bi.appi.keio.ac.jp\/wordpress\/wp-content\/uploads\/2019\/06\/pca2-300x284.png 300w\" sizes=\"auto, (max-width: 338px) 85vw, 338px\" \/><figcaption class=\"wp-element-caption\">\u7b2c1\u4e3b\u6210\u5206\u3068\u7b2c2\u4e3b\u6210\u5206\u3092\u8ef8\u306b\u3068\u3063\u305f\u3082\u306e\uff0e3\u03c3\u306e\u9577\u534a\u5f84\u3068\u77ed\u534a\u5f84\u3092\u6301\u3064\u6955\u5186\u3092\u91cd\u306d\u3066\u63cf\u3044\u305f<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">\u4e3b\u6210\u5206\u5206\u6790\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3046\ncomponents\n[[ 0.73220426  0.6810851 ]\n [-0.6810851   0.73220426]]\nmean=[52.53235793 51.86342693]\ncovariance\n[[244.04297958 193.42743647]\n [193.42743647 216.02097448]]\ncontribution\n[0.92153752 0.07846248]\n\u56fa\u6709\u30d9\u30af\u30c8\u30eb\u306e\u7b26\u53f7\u304c\u53cd\u8ee2\u3057\u3066\u3044\u308b\u3068\u5de6\u53f3\u30fb\u4e0a\u4e0b\u306b\u53cd\u8ee2\u304c\u3042\u308b<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"338\" height=\"320\" src=\"https:\/\/www.bi.appi.keio.ac.jp\/wordpress\/wp-content\/uploads\/2019\/06\/pca3.png\" alt=\"\" class=\"wp-image-1990\" srcset=\"https:\/\/www.bi.appi.keio.ac.jp\/wordpress\/wp-content\/uploads\/2019\/06\/pca3.png 338w, https:\/\/www.bi.appi.keio.ac.jp\/wordpress\/wp-content\/uploads\/2019\/06\/pca3-300x284.png 300w\" sizes=\"auto, (max-width: 338px) 85vw, 338px\" \/><figcaption class=\"wp-element-caption\">\u4e3b\u6210\u5206\u5206\u6790\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u8a08\u7b97\u3057\u305f\u3082\u306e<\/figcaption><\/figure>\n\n\n\n<p>\u7b2c1\u4e3b\u6210\u5206\u306f\uff0c\u56fa\u6709\u5024\u304c\u6700\u3082\u5927\u304d\u3044\u56fa\u6709\u30d9\u30af\u30c8\u30eb\u306e\u65b9\u5411\u3067\u3059\uff0e\u56fa\u6709\u5024\u304c\u6700\u3082\u5927\u304d\u3044\u65b9\u5411\u306f\uff0c\u30c7\u30fc\u30bf\u306e\u6563\u3089\u3070\u308a\u304c\u6700\u3082\u5927\u304d\u304f\u306a\u308b\u65b9\u5411\u3067\u3059\uff0e\u3053\u308c\u306f\u6b21\u306e\u3088\u3046\u306b\u5c0e\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\uff0e<\/p>\n\n\n\n<p><br>\u30d9\u30af\u30c8\u30eb$\\mathbf x_i$\u306e\u5e73\u5747\u306f$\\bar{\\mathbf x}=\\displaystyle\\frac{1}{N}\\sum_{i=1}^{N}\\mathbf x_i$\u3067\u3059\uff0e<br>\u4eca\uff0c\u30c7\u30fc\u30bf\u306e\u30d9\u30af\u30c8\u30eb$\\mathbf x_i$\u3092\u30c7\u30fc\u30bf\u306e\u6563\u3089\u3070\u308a\u304c\u6700\u3082\u5927\u304d\u304f\u306a\u308b\u65b9\u5411\u306e\u30d9\u30af\u30c8\u30eb$\\mathbf u_1$\u306b\u5c04\u5f71\u3059\u308b\u3053\u3068\u3092\u8003\u3048\u307e\u3059\uff0e\u3053\u306e\u3068\u304d\uff0c$\\mathbf u_1$\u3092\u5358\u4f4d\u30d9\u30af\u30c8\u30eb\u306b\u3068\u308a\u307e\u3059\uff0e$\\mathbf x_i$\u3092$\\mathbf u_1$\u306b\u5c04\u5f71\u3059\u308b\u3068$\\mathbf x_i^T\\mathbf u_1$\u306b\u306a\u308a\u307e\u3059\uff0e\u5c04\u5f71\u306e\u5206\u6563\u306f<br>$\\sigma^2 = \\displaystyle\\frac{1}{N}\\sum_{i=1}^{N}(\\mathbf x_i^T\\mathbf u_1 &#8211; \\bar{\\mathbf x}^T\\mathbf u_1)^2 = \\mathbf u_1^T S \\mathbf u_1$\u3067\u3059\uff0e<br>\u3053\u3053\u3067<br>$S= \\displaystyle\\frac{1}{N}(\\mathbf x_i &#8211; \\bar{\\mathbf x})(\\mathbf x_i &#8211; \\bar{\\mathbf x})^T$\u3067\u3059\uff0e<br>$\\mathbf u_1^T S \\mathbf u_1$\u3092$\\mathbf u_1^T\\mathbf u_1=1$\u306e\u6761\u4ef6\u306e\u4e0b\u3067\u6700\u5927\u5316\u3057\u307e\u3059\uff0e\u3053\u308c\u306f\u30e9\u30b0\u30e9\u30f3\u30b8\u30a7\u306e\u672a\u5b9a\u4e57\u6570\u6cd5\u3092\u7528\u3044\u3066<br>$\\mathbf u_1^T S \\mathbf u_1 &#8211; \\lambda(\\mathbf u_1^T\\mathbf u_1 &#8211; 1)$<br>\u3092\u6700\u5927\u5316\u3057\u3066\u89e3\u304d\u307e\u3059\uff0e$\\mathbf u_1$\u3067\u5fae\u5206\u3059\u308b\u3068<br>$S\\mathbf u_1 &#8211; \\lambda \\mathbf u_1=0$<br>\u306b\u306a\u308a\u307e\u3059\uff0e\u3064\u307e\u308a$S\\mathbf u_1 = \\lambda \\mathbf u_1$\u3067\u3059\u304b\u3089\uff0c$\\mathbf u_1$\u306f\u884c\u5217$S$\u306e\u56fa\u6709\u30d9\u30af\u30c8\u30eb\u3067\u3059\uff0e<br>\u3053\u306e\u5f0f\u306b\u5de6\u304b\u3089$\\mathbf u_1^T$\u3092\u304b\u3051\u308b\u3068$\\mathbf u_1^T S \\mathbf u_1 = \\lambda$\u3068\u306a\u308a\uff0c\u30c7\u30fc\u30bf$\\mathbf x_i$\u306e\u5c04\u5f71\u306e\u5206\u6563\u306f\u56fa\u6709\u5024\u3067\u3059\uff0e\u307e\u305f\uff0c\u305d\u308c\u3092\u6700\u5927\u306b\u3059\u308b\u3082\u306e\u306f\u6700\u5927\u306e\u56fa\u6709\u5024\u306b\u306a\u308a\u307e\u3059\uff0e<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7269\u7406\u60c5\u5831\u6570\u5b66B\u3067\u8aac\u660e\u3057\u3066\u3044\u3066\u3044\u308b\u5bfe\u79f0\u884c\u5217\u304c\u51fa\u3066\u304f\u308b\u4f8b\u3067\u3059\uff0e\u56fa\u6709\u30d9\u30af\u30c8\u30eb\u3078\u306e\u5c04\u5f71\u3082\u95a2\u4fc2\u3057\u307e\u3059\uff0e # # \u4e3b\u6210\u5206\u5206\u6790 # import numpy as np import matplotlib.pyplot as plt  &hellip; <a href=\"https:\/\/www.bi.appi.keio.ac.jp\/?p=1985\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u4e3b\u6210\u5206\u5206\u6790&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-1985","post","type-post","status-publish","format-standard","hentry","category-9"],"_links":{"self":[{"href":"https:\/\/www.bi.appi.keio.ac.jp\/index.php?rest_route=\/wp\/v2\/posts\/1985","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bi.appi.keio.ac.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bi.appi.keio.ac.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bi.appi.keio.ac.jp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bi.appi.keio.ac.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1985"}],"version-history":[{"count":14,"href":"https:\/\/www.bi.appi.keio.ac.jp\/index.php?rest_route=\/wp\/v2\/posts\/1985\/revisions"}],"predecessor-version":[{"id":2249,"href":"https:\/\/www.bi.appi.keio.ac.jp\/index.php?rest_route=\/wp\/v2\/posts\/1985\/revisions\/2249"}],"wp:attachment":[{"href":"https:\/\/www.bi.appi.keio.ac.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1985"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bi.appi.keio.ac.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1985"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bi.appi.keio.ac.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1985"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}