{"id":1247,"date":"2023-07-27T03:54:17","date_gmt":"2023-07-27T03:54:17","guid":{"rendered":"https:\/\/statorials.org\/cn\/r-%e4%b8%ad%e7%9a%84-k-%e4%b8%aa%e4%b8%ad%e5%bf%83%e7%82%b9\/"},"modified":"2023-07-27T03:54:17","modified_gmt":"2023-07-27T03:54:17","slug":"r-%e4%b8%ad%e7%9a%84-k-%e4%b8%aa%e4%b8%ad%e5%bf%83%e7%82%b9","status":"publish","type":"post","link":"https:\/\/statorials.org\/cn\/r-%e4%b8%ad%e7%9a%84-k-%e4%b8%aa%e4%b8%ad%e5%bf%83%e7%82%b9\/","title":{"rendered":"R \u4e2d\u7684 k-medoids\uff1a\u5206\u6b65\u793a\u4f8b"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">\u805a\u7c7b\u662f\u4e00\u79cd\u673a\u5668\u5b66\u4e60\u6280\u672f\uff0c\u5c1d\u8bd5\u5728\u6570\u636e\u96c6\u4e2d\u67e5\u627e<a href=\"https:\/\/statorials.org\/cn\/\u7edf\u8ba1\u89c2\u5bdf\/\" target=\"_blank\" rel=\"noopener noreferrer\">\u89c2\u5bdf<\/a>\u7ec4\u6216\u89c2\u5bdf<em>\u7c07<\/em>\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\u76ee\u6807\u662f\u627e\u5230\u805a\u7c7b\uff0c\u4f7f\u5f97\u6bcf\u4e2a\u805a\u7c7b\u5185\u7684\u89c2\u5bdf\u7ed3\u679c\u5f7c\u6b64\u975e\u5e38\u76f8\u4f3c\uff0c\u800c\u4e0d\u540c\u805a\u7c7b\u4e2d\u7684\u89c2\u5bdf\u7ed3\u679c\u5f7c\u6b64\u975e\u5e38\u4e0d\u540c\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\u805a\u7c7b\u662f<a href=\"https:\/\/statorials.org\/cn\/\u76d1\u7763\u5b66\u4e60\u4e0e\u65e0\u76d1\u7763\u5b66\u4e60\/\" target=\"_blank\" rel=\"noopener noreferrer\">\u65e0\u76d1\u7763\u5b66\u4e60<\/a>\u7684\u4e00\u79cd\u5f62\u5f0f\uff0c\u56e0\u4e3a\u6211\u4eec\u53ea\u662f\u8bd5\u56fe\u5728\u6570\u636e\u96c6\u4e2d\u627e\u5230\u7ed3\u6784\uff0c\u800c\u4e0d\u662f\u9884\u6d4b<a href=\"https:\/\/statorials.org\/cn\/\u53d8\u91cf\u89e3\u91ca\u6027\u53cd\u5e94\/\" target=\"_blank\" rel=\"noopener noreferrer\">\u54cd\u5e94\u53d8\u91cf<\/a>\u7684\u503c\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\u5f53\u4f01\u4e1a\u53ef\u4ee5\u8bbf\u95ee\u4ee5\u4e0b\u4fe1\u606f\u65f6\uff0c\u805a\u7c7b\u901a\u5e38\u7528\u4e8e\u8425\u9500\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">\u5bb6\u5ead\u6536\u5165<\/span><\/li>\n<li><span style=\"color: #000000;\">\u623f\u5b50\u5927\u5c0f<\/span><\/li>\n<li><span style=\"color: #000000;\">\u6237\u4e3b\u804c\u4e1a<\/span><\/li>\n<li><span style=\"color: #000000;\">\u5230\u6700\u8fd1\u5e02\u533a\u7684\u8ddd\u79bb<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000;\">\u5f53\u6b64\u4fe1\u606f\u53ef\u7528\u65f6\uff0c\u805a\u7c7b\u53ef\u7528\u4e8e\u8bc6\u522b\u76f8\u4f3c\u7684\u5bb6\u5ead\uff0c\u5e76\u4e14\u53ef\u80fd\u66f4\u6709\u53ef\u80fd\u8d2d\u4e70\u67d0\u4e9b\u4ea7\u54c1\u6216\u5bf9\u67d0\u79cd\u7c7b\u578b\u7684\u5e7f\u544a\u505a\u51fa\u66f4\u597d\u7684\u53cd\u5e94\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\u6700\u5e38\u89c1\u7684\u805a\u7c7b\u5f62\u5f0f\u4e4b\u4e00\u79f0\u4e3a<a href=\"https:\/\/statorials.org\/cn\/k-\u8868\u793a\u5728-r-\u4e2d\u5206\u7ec4\/\" target=\"_blank\" rel=\"noopener noreferrer\">k \u5747\u503c\u805a\u7c7b<\/a>\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\u4e0d\u5e78\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u4f1a\u53d7\u5230\u5f02\u5e38\u503c\u7684\u5f71\u54cd\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u7ecf\u5e38\u4f7f\u7528\u7684\u66ff\u4ee3\u65b9\u6cd5\u662f<strong>k-medoids \u805a\u7c7b<\/strong>\u3002<\/span><\/p>\n<h3><span style=\"color: #000000;\"><strong>\u4ec0\u4e48\u662f K-Medoids \u805a\u7c7b\uff1f<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">K-medoids \u805a\u7c7b\u662f\u4e00\u79cd\u5c06\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u4e2a\u89c2\u6d4b\u503c\u653e\u5165<em>K<\/em>\u4e2a\u805a\u7c7b\u4e2d\u7684\u6280\u672f\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\u6700\u7ec8\u76ee\u6807\u662f\u62e5\u6709<em>K \u4e2a<\/em>\u7c07\uff0c\u5176\u4e2d\u6bcf\u4e2a\u7c07\u5185\u7684\u89c2\u5bdf\u7ed3\u679c\u5f7c\u6b64\u975e\u5e38\u76f8\u4f3c\uff0c\u800c\u4e0d\u540c\u7c07\u4e2d\u7684\u89c2\u5bdf\u7ed3\u679c\u5f7c\u6b64\u975e\u5e38\u4e0d\u540c\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\u5728\u5b9e\u8df5\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u6765\u8fdb\u884cK-means\u805a\u7c7b\uff1a<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>1. \u9009\u62e9<em>K<\/em>\u503c\u3002<\/strong><\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u51b3\u5b9a\u8981\u5728\u6570\u636e\u4e2d\u8bc6\u522b\u591a\u5c11\u4e2a\u7c07\u3002\u901a\u5e38\u6211\u4eec\u53ea\u9700\u8981\u6d4b\u8bd5\u51e0\u4e2a\u4e0d\u540c\u7684<em>K<\/em>\u503c\u5e76\u5206\u6790\u7ed3\u679c\uff0c\u770b\u770b\u5bf9\u4e8e\u7ed9\u5b9a\u95ee\u9898\uff0c\u54ea\u4e2a\u7c07\u6570\u4f3c\u4e4e\u6700\u6709\u610f\u4e49\u3002<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\"><strong>2. \u5c06\u6bcf\u4e2a\u89c2\u6d4b\u503c\u968f\u673a\u5206\u914d\u5230\u4e00\u4e2a\u521d\u59cb\u7c07\uff08\u4ece 1 \u5230<em>K\uff09<\/em> \u3002<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>3. \u6267\u884c\u4ee5\u4e0b\u8fc7\u7a0b\uff0c\u76f4\u5230\u96c6\u7fa4\u5206\u914d\u505c\u6b62\u66f4\u6539\u3002<\/strong><\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">\u5bf9\u4e8e\u6bcf\u4e2a<em>K<\/em>\u4e2a\u7c07\uff0c\u8ba1\u7b97<em>\u8be5\u7c07\u7684\u91cd\u5fc3\u3002<\/em>\u8fd9\u662f<em>\u7b2c k<\/em>\u4e2a\u7c07\u7684\u89c2\u6d4b\u503c\u7684\u7279\u5f81\u7684<em>p \u4e2a<\/em><b>\u4e2d\u4f4d\u6570<\/b>\u7684\u5411\u91cf\u3002<\/span><\/li>\n<li><span style=\"color: #000000;\">\u5c06\u6bcf\u4e2a\u89c2\u6d4b\u503c\u5206\u914d\u7ed9\u5177\u6709\u6700\u8fd1\u8d28\u5fc3\u7684\u7c07\u3002\u8fd9\u91cc\uff0c<em>\u6700\u63a5\u8fd1<\/em>\u662f\u4f7f\u7528<a href=\"https:\/\/en.wikipedia.org\/wiki\/Euclidean_distance#Squared_Euclidean_distance\" target=\"_blank\" rel=\"noopener noreferrer\">\u6b27\u51e0\u91cc\u5fb7\u8ddd\u79bb<\/a>\u5b9a\u4e49\u7684\u3002<\/span><\/li>\n<\/ul>\n<blockquote>\n<p><span style=\"color: #000000;\"><strong>\u6280\u672f\u8bf4\u660e\uff1a<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">\u7531\u4e8e k-medoids \u4f7f\u7528\u4e2d\u4f4d\u6570\u800c\u4e0d\u662f\u5747\u503c\u6765\u8ba1\u7b97\u805a\u7c7b\u8d28\u5fc3\uff0c\u56e0\u6b64\u5b83\u5bf9\u4e8e\u5f02\u5e38\u503c\u5f80\u5f80\u6bd4 k-means \u66f4\u7a33\u5065\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\u5b9e\u9645\u4e0a\uff0c\u5982\u679c\u6570\u636e\u96c6\u4e2d\u6ca1\u6709\u6781\u7aef\u5f02\u5e38\u503c\uff0ck-means \u548c k-medoids \u5c06\u4ea7\u751f\u7c7b\u4f3c\u7684\u7ed3\u679c\u3002<\/span><\/p>\n<\/blockquote>\n<h3> <span style=\"color: #000000;\"><strong>R \u4e2d\u7684 K-Medoids \u805a\u7c7b<\/strong><\/span><\/h3>\n<p><span style=\"color: #000000;\">\u4ee5\u4e0b\u6559\u7a0b\u63d0\u4f9b\u4e86\u5982\u4f55\u5728 R \u4e2d\u6267\u884c k-medoids \u805a\u7c7b\u7684\u5206\u6b65\u793a\u4f8b\u3002<\/span><\/p>\n<h3><span style=\"color: #000000;\"><strong>\u7b2c1\u6b65\uff1a\u52a0\u8f7d\u5fc5\u8981\u7684\u5305<\/strong><\/span><\/h3>\n<p><span style=\"color: #000000;\">\u9996\u5148\uff0c\u6211\u4eec\u5c06\u52a0\u8f7d\u4e24\u4e2a\u5305\uff0c\u5176\u4e2d\u5305\u542b R \u4e2d k-medoids \u805a\u7c7b\u7684\u51e0\u4e2a\u6709\u7528\u51fd\u6570\u3002<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #993300;\">library<\/span> (factoextra)\n<span style=\"color: #993300;\">library<\/span> (cluster)<\/strong><\/pre>\n<h3><span style=\"color: #000000;\"><strong>\u7b2c 2 \u6b65\uff1a\u52a0\u8f7d\u548c\u51c6\u5907\u6570\u636e<\/strong><\/span><\/h3>\n<p><span style=\"color: #000000;\">\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528 R \u4e2d\u5185\u7f6e\u7684<em>USArrests<\/em>\u6570\u636e\u96c6\uff0c\u5176\u4e2d\u5305\u542b 1973 \u5e74\u7f8e\u56fd\u5404\u5dde\u6bcf 10 \u4e07\u4eba\u56e0<em>\u8c0b\u6740<\/em>\u3001<em>\u88ad\u51fb<\/em>\u548c<em>\u5f3a\u5978<\/em>\u800c\u88ab\u6355\u7684\u4eba\u6570\uff0c\u4ee5\u53ca\u6bcf\u4e2a\u5dde\u5c45\u4f4f\u5728\u57ce\u5e02\u7684\u4eba\u53e3\u767e\u5206\u6bd4\u5730\u533a\u3002 \u3001<em>\u90fd\u5e02\u6d41\u884c<\/em>\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\u4ee5\u4e0b\u4ee3\u7801\u663e\u793a\u4e86\u5982\u4f55\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">\u52a0\u8f7d<em>USArrests<\/em>\u6570\u636e\u96c6<\/span><\/li>\n<li><span style=\"color: #000000;\">\u5220\u9664\u6240\u6709\u6709\u7f3a\u5931\u503c\u7684\u884c<\/span><\/li>\n<li><span style=\"color: #000000;\">\u5c06\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u4e2a\u53d8\u91cf\u7f29\u653e\u4e3a\u5747\u503c 0 \u548c\u6807\u51c6\u5dee 1<\/span><\/li>\n<\/ul>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#load data<\/span>\ndf &lt;-USArrests\n\n<span style=\"color: #008080;\">#remove rows with missing values<\/span><\/strong>\n<strong>df &lt;- na. <span style=\"color: #3366ff;\">omitted<\/span> (df)\n\n<span style=\"color: #008080;\">#scale each variable to have a mean of 0 and sd of 1<\/span><\/strong>\n<strong>df &lt;- scale(df)\n\n<span style=\"color: #008080;\">#view first six rows of dataset<\/span>\nhead(df)\n\n               Murder Assault UrbanPop Rape\nAlabama 1.24256408 0.7828393 -0.5209066 -0.003416473\nAlaska 0.50786248 1.1068225 -1.2117642 2.484202941\nArizona 0.07163341 1.4788032 0.9989801 1.042878388\nArkansas 0.23234938 0.2308680 -1.0735927 -0.184916602\nCalifornia 0.27826823 1.2628144 1.7589234 2.067820292\nColorado 0.02571456 0.3988593 0.8608085 1.864967207\n<\/strong><\/pre>\n<h3><span style=\"color: #000000;\"><strong>\u7b2c 3 \u6b65\uff1a\u627e\u5230\u6700\u4f73\u7c07\u6570<\/strong><\/span><\/h3>\n<p><span style=\"color: #000000;\">\u8981\u5728 R \u4e2d\u6267\u884c k-medoid \u805a\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<strong>pam()<\/strong>\u51fd\u6570\uff0c\u5b83\u4ee3\u8868\u201c\u56f4\u7ed5\u4e2d\u4f4d\u6570\u8fdb\u884c\u5206\u533a\u201d\u5e76\u4f7f\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>pam\uff08\u6570\u636e\uff0ck\uff0c\u516c\u5236=\u201c\u6b27\u51e0\u91cc\u5fb7\u201d\uff0c\u6807\u51c6= FALSE\uff09<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">\u91d1\u5b50\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\"><strong>\u6570\u636e\uff1a<\/strong>\u6570\u636e\u96c6\u7684\u540d\u79f0\u3002<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>k\uff1a<\/strong>\u7c07\u7684\u6570\u91cf\u3002<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>metric\uff1a<\/strong>\u7528\u4e8e\u8ba1\u7b97\u8ddd\u79bb\u7684\u5ea6\u91cf\u3002\u9ed8\u8ba4\u503c\u4e3a<em>Euclidean<\/em> \uff0c\u4f46\u60a8\u4e5f\u53ef\u4ee5\u6307\u5b9a<em>manhattan<\/em> \u3002<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>Stand\uff1a<\/strong>\u662f\u5426\u5bf9\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u4e2a\u53d8\u91cf\u8fdb\u884c\u6807\u51c6\u5316\u3002\u9ed8\u8ba4\u503c\u4e3a false\u3002<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000;\">\u7531\u4e8e\u6211\u4eec\u4e8b\u5148\u4e0d\u77e5\u9053\u6700\u4f73\u96c6\u7fa4\u6570\u91cf\uff0c\u56e0\u6b64\u6211\u4eec\u5c06\u521b\u5efa\u4e24\u4e2a\u4e0d\u540c\u7684\u56fe\u8868\u6765\u5e2e\u52a9\u6211\u4eec\u505a\u51fa\u51b3\u5b9a\uff1a<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>1. \u7c07\u6570\u76f8\u5bf9\u4e8e\u603b\u6570\u7684\u5e73\u65b9\u548c<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">\u9996\u5148\uff0c\u6211\u4eec\u5c06\u4f7f\u7528<strong>fviz_nbclust()<\/strong>\u51fd\u6570\u521b\u5efa\u7c07\u6570\u4e0e\u5e73\u65b9\u548c\u603b\u6570\u7684\u5173\u7cfb\u56fe\uff1a<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong>fviz_nbclust(df, pam, method = \u201c <span style=\"color: #008000;\">wss<\/span> \u201d)<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-12327 \" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/kmedoide1.png\" alt=\"k \u4e2d\u5fc3\u70b9\u7684\u6700\u4f18\u805a\u7c7b\" width=\"462\" height=\"449\" srcset=\"\" sizes=\"auto, \"><\/p>\n<p><span style=\"color: #000000;\">\u968f\u7740\u7c07\u6570\u91cf\u7684\u589e\u52a0\uff0c\u5e73\u65b9\u548c\u4e2d\u7684\u603b\u548c\u901a\u5e38\u603b\u662f\u4f1a\u589e\u52a0\u3002\u56e0\u6b64\uff0c\u5f53\u6211\u4eec\u521b\u5efa\u6b64\u7c7b\u56fe\u65f6\uff0c\u6211\u4eec\u6b63\u5728\u5bfb\u627e\u5e73\u65b9\u548c\u5f00\u59cb\u201c\u5f2f\u66f2\u201d\u6216\u8d8b\u4e8e\u5e73\u5766\u7684\u201c\u819d\u76d6\u201d\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\u7ed8\u56fe\u7684\u66f2\u7387\u70b9\u901a\u5e38\u5bf9\u5e94\u4e8e\u6700\u4f73\u7c07\u6570\u3002\u8d85\u8fc7\u8fd9\u4e2a\u6570\u5b57\uff0c\u5f88\u53ef\u80fd\u4f1a\u53d1\u751f<a href=\"https:\/\/statorials.org\/cn\/\u673a\u5668\u5b66\u4e60\u8fc7\u5ea6\u62df\u5408\/\" target=\"_blank\" rel=\"noopener noreferrer\">\u8fc7\u5ea6\u62df\u5408<\/a>\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\u5bf9\u4e8e\u8be5\u56fe\uff0c\u5728 k = 4 \u4e2a\u7c07\u5904\u4f3c\u4e4e\u5b58\u5728\u5c0f\u626d\u7ed3\u6216\u201c\u5f2f\u66f2\u201d\u3002<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>2. \u805a\u7c7b\u6570\u91cf\u4e0e\u95f4\u9699\u7edf\u8ba1<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">\u786e\u5b9a\u6700\u4f73\u7c07\u6570\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u79f0\u4e3a<a style=\"color: #000000;\" href=\"https:\/\/web.stanford.edu\/~hastie\/Papers\/gap.pdf\" target=\"_blank\" rel=\"noopener noreferrer\">\u504f\u5dee\u7edf\u8ba1\u91cf<\/a>\u7684\u5ea6\u91cf\uff0c\u5b83\u5c06\u4e0d\u540c k \u503c\u7684\u7c07\u5185\u603b\u53d8\u5f02\u4e0e\u672a\u8fdb\u884c\u805a\u7c7b\u7684\u5206\u5e03\u7684\u9884\u671f\u503c\u8fdb\u884c\u6bd4\u8f83\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<em>cluster<\/em>\u5305\u4e2d\u7684<strong>clusGap()<\/strong>\u51fd\u6570\u8ba1\u7b97\u6bcf\u4e2a\u7c07\u6570\u91cf\u7684\u95f4\u9699\u7edf\u8ba1\u91cf\uff0c\u5e76\u4f7f\u7528<strong>fviz_gap_stat()<\/strong>\u51fd\u6570\u7ed8\u5236\u7c07\u4e0e\u95f4\u9699\u7edf\u8ba1\u91cf\u7684\u5173\u7cfb\u56fe\uff1a<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#calculate gap statistic based on number of clusters\n<\/span>gap_stat &lt;- clusGap(df,\n                    FUN = pam,\n                    K.max = 10, <span style=\"color: #008080;\">#max clusters to consider<\/span>\n                    B = 50) <span style=\"color: #008080;\">#total bootstrapped iterations<\/span>\n\n<span style=\"color: #008080;\">#plot number of clusters vs. gap statistic\n<\/span>fviz_gap_stat(gap_stat)<\/strong> <\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-12328 \" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/kmedoide2.png\" alt=\"R \u4e2d\u7684 K-medoids \u6700\u4f73\u7c07\u6570\" width=\"443\" height=\"441\" srcset=\"\" sizes=\"auto, \"><\/p>\n<p><span style=\"color: #000000;\">\u4ece\u56fe\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u95f4\u9699\u7edf\u8ba1\u91cf\u5728 k = 4 \u4e2a\u7c07\u65f6\u6700\u9ad8\uff0c\u8fd9\u5bf9\u5e94\u4e8e\u6211\u4eec\u4e4b\u524d\u4f7f\u7528\u7684\u8098\u6cd5\u3002<\/span><\/p>\n<h3><span style=\"color: #000000;\"><strong>\u6b65\u9aa4 4\uff1a\u4f7f\u7528 Optimal <em>K<\/em>\u6267\u884c K-Medoids \u805a\u7c7b<\/strong><\/span><\/h3>\n<p><span style=\"color: #000000;\">\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<em>k<\/em>\u7684\u6700\u4f73\u503c 4 \u5bf9\u6570\u636e\u96c6\u6267\u884c k-medoids \u805a\u7c7b\uff1a<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#make this example reproducible\n<span style=\"color: #000000;\">set.seed(1)<\/span>\n\n#perform k-medoids clustering with k = 4 clusters\n<\/span>kmed &lt;- pam(df, k = 4)\n\n<span style=\"color: #008080;\">#view results\n<\/span>kmed\n\n              ID Murder Assault UrbanPop Rape\nAlabama 1 1.2425641 0.7828393 -0.5209066 -0.003416473\nMichigan 22 0.9900104 1.0108275 0.5844655 1.480613993\nOklahoma 36 -0.2727580 -0.2371077 0.1699510 -0.131534211\nNew Hampshire 29 -1.3059321 -1.3650491 -0.6590781 -1.252564419\nVector clustering:\n       Alabama Alaska Arizona Arkansas California \n             1 2 2 1 2 \n      Colorado Connecticut Delaware Florida Georgia \n             2 3 3 2 1 \n        Hawaii Idaho Illinois Indiana Iowa \n             3 4 2 3 4 \n        Kansas Kentucky Louisiana Maine Maryland \n             3 3 1 4 2 \n Massachusetts Michigan Minnesota Mississippi Missouri \n             3 2 4 1 3 \n       Montana Nebraska Nevada New Hampshire New Jersey \n             3 3 2 4 3 \n    New Mexico New York North Carolina North Dakota Ohio \n             2 2 1 4 3 \n      Oklahoma Oregon Pennsylvania Rhode Island South Carolina \n             3 3 3 3 1 \n  South Dakota Tennessee Texas Utah Vermont \n             4 1 2 3 4 \n      Virginia Washington West Virginia Wisconsin Wyoming \n             3 3 4 4 3 \nObjective function:\n   build swap \n1.035116 1.027102 \n\nAvailable components:\n [1] \"medoids\" \"id.med\" \"clustering\" \"objective\" \"isolation\" \n [6] \"clusinfo\" \"silinfo\" \"diss\" \"call\" \"data\"          \n<\/strong><\/pre>\n<p><span style=\"color: #000000;\">\u8bf7\u6ce8\u610f\uff0c\u6240\u6709\u56db\u4e2a\u805a\u7c7b\u8d28\u5fc3\u90fd\u662f\u6570\u636e\u96c6\u4e2d\u7684\u5b9e\u9645\u89c2\u6d4b\u503c\u3002\u5728\u8f93\u51fa\u7684\u9876\u90e8\u9644\u8fd1\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u56db\u4e2a\u8d28\u5fc3\u5177\u6709\u4ee5\u4e0b\u72b6\u6001\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"color: #000000;\">\u963f\u62c9\u5df4\u9a6c\u5dde<\/span><\/li>\n<li><span style=\"color: #000000;\">\u5bc6\u6b47\u6839\u5dde<\/span><\/li>\n<li><span style=\"color: #000000;\">\u4fc4\u514b\u62c9\u8377\u9a6c\u5dde<\/span><\/li>\n<li><span style=\"color: #000000;\">\u65b0\u7f55\u5e03\u4ec0\u5c14<\/span><\/li>\n<\/ul>\n<p><span style=\"color: #000000;\">\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<strong>fivz_cluster()<\/strong>\u51fd\u6570\u5728\u6563\u70b9\u56fe\u4e0a\u53ef\u89c6\u5316\u7c07\uff0c\u8be5\u6563\u70b9\u56fe\u5728\u8f74\u4e0a\u663e\u793a\u524d\u4e24\u4e2a\u4e3b\u6210\u5206\uff1a<\/span> <\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#plot results of final k-medoids model\n<span style=\"color: #000000;\">fviz_cluster(kmed, data = df)\n<\/span><\/span><\/strong><\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-12329 \" src=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/kmedoide3.png\" alt=\"\u5728 R \u4e2d\u7ed8\u5236 k-medoid \u7c07\" width=\"497\" height=\"500\" srcset=\"\" sizes=\"auto, \"><\/p>\n<p><span style=\"color: #000000;\">\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u72b6\u6001\u7684\u805a\u7c7b\u5206\u914d\u6dfb\u52a0\u5230\u539f\u59cb\u6570\u636e\u96c6\u4e2d\uff1a<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#add cluster assignment to original data\n<span style=\"color: #000000;\">final_data &lt;- cbind(USArrests, cluster = kmed$cluster)\n<\/span>\n#view final data\n<span style=\"color: #000000;\">head(final_data)\n\n           Murder Assault UrbanPop Rape cluster\nAlabama 13.2 236 58 21.2 1\nAlaska 10.0 263 48 44.5 2\nArizona 8.1 294 80 31.0 2\nArkansas 8.8 190 50 19.5 1\nCalifornia 9.0 276 91 40.6 2\nColorado 7.9 204 78 38.7 2\n<\/span><\/span><\/strong><\/pre>\n<hr>\n<p><span style=\"color: #000000;\">\u60a8\u53ef\u4ee5<a href=\"https:\/\/github.com\/Statorials\/R-Guides\/blob\/main\/k_medoids.R\" target=\"_blank\" rel=\"noopener noreferrer\">\u5728\u6b64\u5904<\/a>\u627e\u5230\u672c\u793a\u4f8b\u4e2d\u4f7f\u7528\u7684\u5b8c\u6574 R \u4ee3\u7801\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u805a\u7c7b\u662f\u4e00\u79cd\u673a\u5668\u5b66\u4e60\u6280\u672f\uff0c\u5c1d\u8bd5\u5728\u6570\u636e\u96c6\u4e2d\u67e5\u627e\u89c2\u5bdf\u7ec4\u6216\u89c2\u5bdf\u7c07\u3002 \u76ee\u6807\u662f\u627e\u5230\u805a\u7c7b\uff0c\u4f7f\u5f97\u6bcf\u4e2a\u805a\u7c7b\u5185\u7684\u89c2\u5bdf\u7ed3\u679c\u5f7c\u6b64\u975e\u5e38\u76f8 [&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":[],"class_list":["post-1247","post","type-post","status-publish","format-standard","hentry","category-11"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>R \u4e2d\u7684 K-Medoids\uff1a\u5206\u6b65\u793a\u4f8b<\/title>\n<meta name=\"description\" content=\"\u672c\u6559\u7a0b\u63d0\u4f9b\u4e86\u5982\u4f55\u5728 R \u4e2d\u6267\u884c k-medoids \u805a\u7c7b\u7684\u5206\u6b65\u793a\u4f8b\u3002\" \/>\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\/cn\/r-\u4e2d\u7684-k-\u4e2a\u4e2d\u5fc3\u70b9\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"R \u4e2d\u7684 K-Medoids\uff1a\u5206\u6b65\u793a\u4f8b\" \/>\n<meta property=\"og:description\" content=\"\u672c\u6559\u7a0b\u63d0\u4f9b\u4e86\u5982\u4f55\u5728 R \u4e2d\u6267\u884c k-medoids \u805a\u7c7b\u7684\u5206\u6b65\u793a\u4f8b\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/cn\/r-\u4e2d\u7684-k-\u4e2a\u4e2d\u5fc3\u70b9\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-27T03:54:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/kmedoide1.png\" \/>\n<meta name=\"author\" content=\"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/cn\/r-%e4%b8%ad%e7%9a%84-k-%e4%b8%aa%e4%b8%ad%e5%bf%83%e7%82%b9\/\",\"url\":\"https:\/\/statorials.org\/cn\/r-%e4%b8%ad%e7%9a%84-k-%e4%b8%aa%e4%b8%ad%e5%bf%83%e7%82%b9\/\",\"name\":\"R \u4e2d\u7684 K-Medoids\uff1a\u5206\u6b65\u793a\u4f8b\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/cn\/#website\"},\"datePublished\":\"2023-07-27T03:54:17+00:00\",\"dateModified\":\"2023-07-27T03:54:17+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/cn\/#\/schema\/person\/124e4e5b7c9f8dc0f1f95cc8c1db3261\"},\"description\":\"\u672c\u6559\u7a0b\u63d0\u4f9b\u4e86\u5982\u4f55\u5728 R \u4e2d\u6267\u884c k-medoids \u805a\u7c7b\u7684\u5206\u6b65\u793a\u4f8b\u3002\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/cn\/r-%e4%b8%ad%e7%9a%84-k-%e4%b8%aa%e4%b8%ad%e5%bf%83%e7%82%b9\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/cn\/r-%e4%b8%ad%e7%9a%84-k-%e4%b8%aa%e4%b8%ad%e5%bf%83%e7%82%b9\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/cn\/r-%e4%b8%ad%e7%9a%84-k-%e4%b8%aa%e4%b8%ad%e5%bf%83%e7%82%b9\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u5bb6\",\"item\":\"https:\/\/statorials.org\/cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"R \u4e2d\u7684 k-medoids\uff1a\u5206\u6b65\u793a\u4f8b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/cn\/#website\",\"url\":\"https:\/\/statorials.org\/cn\/\",\"name\":\"Statorials\",\"description\":\"\u60a8\u7684\u7edf\u8ba1\u7d20\u517b\u6307\u5357\uff01\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/cn\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/cn\/#\/schema\/person\/124e4e5b7c9f8dc0f1f95cc8c1db3261\",\"name\":\"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/statorials.org\/cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/cn\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/cn\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a\"},\"description\":\"\u5927\u5bb6\u597d\uff0c\u6211\u662f\u672c\u6770\u660e\uff0c\u4e00\u4f4d\u9000\u4f11\u7684\u7edf\u8ba1\u5b66\u6559\u6388\uff0c\u540e\u6765\u6210\u4e3a Statorials \u7684\u70ed\u5fc3\u6559\u5e08\u3002 \u51ed\u501f\u5728\u7edf\u8ba1\u9886\u57df\u7684\u4e30\u5bcc\u7ecf\u9a8c\u548c\u4e13\u4e1a\u77e5\u8bc6\uff0c\u6211\u6e34\u671b\u5206\u4eab\u6211\u7684\u77e5\u8bc6\uff0c\u901a\u8fc7 Statorials \u589e\u5f3a\u5b66\u751f\u7684\u80fd\u529b\u3002\u4e86\u89e3\u66f4\u591a\",\"sameAs\":[\"https:\/\/statorials.org\/cn\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"R \u4e2d\u7684 K-Medoids\uff1a\u5206\u6b65\u793a\u4f8b","description":"\u672c\u6559\u7a0b\u63d0\u4f9b\u4e86\u5982\u4f55\u5728 R \u4e2d\u6267\u884c k-medoids \u805a\u7c7b\u7684\u5206\u6b65\u793a\u4f8b\u3002","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\/cn\/r-\u4e2d\u7684-k-\u4e2a\u4e2d\u5fc3\u70b9\/","og_locale":"zh_CN","og_type":"article","og_title":"R \u4e2d\u7684 K-Medoids\uff1a\u5206\u6b65\u793a\u4f8b","og_description":"\u672c\u6559\u7a0b\u63d0\u4f9b\u4e86\u5982\u4f55\u5728 R \u4e2d\u6267\u884c k-medoids \u805a\u7c7b\u7684\u5206\u6b65\u793a\u4f8b\u3002","og_url":"https:\/\/statorials.org\/cn\/r-\u4e2d\u7684-k-\u4e2a\u4e2d\u5fc3\u70b9\/","og_site_name":"Statorials","article_published_time":"2023-07-27T03:54:17+00:00","og_image":[{"url":"https:\/\/statorials.org\/wp-content\/uploads\/2023\/08\/kmedoide1.png"}],"author":"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/cn\/r-%e4%b8%ad%e7%9a%84-k-%e4%b8%aa%e4%b8%ad%e5%bf%83%e7%82%b9\/","url":"https:\/\/statorials.org\/cn\/r-%e4%b8%ad%e7%9a%84-k-%e4%b8%aa%e4%b8%ad%e5%bf%83%e7%82%b9\/","name":"R \u4e2d\u7684 K-Medoids\uff1a\u5206\u6b65\u793a\u4f8b","isPartOf":{"@id":"https:\/\/statorials.org\/cn\/#website"},"datePublished":"2023-07-27T03:54:17+00:00","dateModified":"2023-07-27T03:54:17+00:00","author":{"@id":"https:\/\/statorials.org\/cn\/#\/schema\/person\/124e4e5b7c9f8dc0f1f95cc8c1db3261"},"description":"\u672c\u6559\u7a0b\u63d0\u4f9b\u4e86\u5982\u4f55\u5728 R \u4e2d\u6267\u884c k-medoids \u805a\u7c7b\u7684\u5206\u6b65\u793a\u4f8b\u3002","breadcrumb":{"@id":"https:\/\/statorials.org\/cn\/r-%e4%b8%ad%e7%9a%84-k-%e4%b8%aa%e4%b8%ad%e5%bf%83%e7%82%b9\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/cn\/r-%e4%b8%ad%e7%9a%84-k-%e4%b8%aa%e4%b8%ad%e5%bf%83%e7%82%b9\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/cn\/r-%e4%b8%ad%e7%9a%84-k-%e4%b8%aa%e4%b8%ad%e5%bf%83%e7%82%b9\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u5bb6","item":"https:\/\/statorials.org\/cn\/"},{"@type":"ListItem","position":2,"name":"R \u4e2d\u7684 k-medoids\uff1a\u5206\u6b65\u793a\u4f8b"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/cn\/#website","url":"https:\/\/statorials.org\/cn\/","name":"Statorials","description":"\u60a8\u7684\u7edf\u8ba1\u7d20\u517b\u6307\u5357\uff01","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/cn\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/statorials.org\/cn\/#\/schema\/person\/124e4e5b7c9f8dc0f1f95cc8c1db3261","name":"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/statorials.org\/cn\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/cn\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/cn\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"\u672c\u6770\u660e\u00b7\u5b89\u5fb7\u68ee\u535a"},"description":"\u5927\u5bb6\u597d\uff0c\u6211\u662f\u672c\u6770\u660e\uff0c\u4e00\u4f4d\u9000\u4f11\u7684\u7edf\u8ba1\u5b66\u6559\u6388\uff0c\u540e\u6765\u6210\u4e3a Statorials \u7684\u70ed\u5fc3\u6559\u5e08\u3002 \u51ed\u501f\u5728\u7edf\u8ba1\u9886\u57df\u7684\u4e30\u5bcc\u7ecf\u9a8c\u548c\u4e13\u4e1a\u77e5\u8bc6\uff0c\u6211\u6e34\u671b\u5206\u4eab\u6211\u7684\u77e5\u8bc6\uff0c\u901a\u8fc7 Statorials \u589e\u5f3a\u5b66\u751f\u7684\u80fd\u529b\u3002\u4e86\u89e3\u66f4\u591a","sameAs":["https:\/\/statorials.org\/cn"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/posts\/1247","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/comments?post=1247"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/posts\/1247\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/media?parent=1247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/categories?post=1247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/cn\/wp-json\/wp\/v2\/tags?post=1247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}