{"id":50863,"date":"2025-08-10T13:14:24","date_gmt":"2025-08-10T05:14:24","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/50863.html"},"modified":"2025-08-10T13:14:24","modified_gmt":"2025-08-10T05:14:24","slug":"%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0-dbscan","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/50863.html","title":{"rendered":"\u673a\u5668\u5b66\u4e60\u2014\u2014DBSCAN"},"content":{"rendered":"<h2>\u4e00\u00b7\u6982\u5ff5\u4ecb\u7ecd<\/h2>\n<p>\u6709\u76d1\u7763\u5b66\u4e60\u6709\u5916\u90e8\u7ed3\u679cy&#xff0c;\u65e0\u76d1\u7763\u5b66\u4e60\u65e0\u5916\u90e8\u7ed3\u679cy&#xff0c;\u6b64\u4e3a\u4e8c\u8005\u6838\u5fc3\u533a\u522b\u3002<\/p>\n<p>\u6982\u5ff5&#xff1a;<br \/>\n\u57fa\u4e8e\u5bc6\u5ea6\u7684\u5e26\u566a\u58f0\u7684\u7a7a\u95f4\u805a\u7c7b\u5e94\u7528\u7b97\u6cd5&#xff0c;\u5b83\u662f\u5c06\u7c07\u5b9a\u4e49\u4e3a\u5bc6\u5ea6\u76f8\u8fde\u7684\u70b9\u7684\u6700\u5927\u96c6\u5408&#xff0c;\u80fd\u591f\u628a\u5177\u6709\u8db3\u591f\u9ad8\u5bc6\u5ea6\u7684\u533a\u57df\u5212\u5206\u4e3a\u7c07&#xff0c;\u5e76\u5728\u566a\u58f0\u7684\u7a7a\u95f4\u6570\u636e\u96c6\u4e2d\u53d1\u73b0\u4efb\u610f\u5f62\u72b6\u7684\u805a\u7c7b\u3002<\/p>\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"214\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250810051420-68982aac54013.png\" width=\"285\" \/><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"200\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250810051420-68982aacb7f65.png\" width=\"320\" \/><\/p>\n<\/p>\n<p>\u8981\u70b9&#xff1a;<br \/>\n1. \u6838\u5fc3\u5bf9\u8c61&#xff1a;A \u70b9<br \/>\n2.E \u90bb\u57df&#xff1a;\u7ed9\u5b9a\u5bf9\u8c61\u534a\u5f84\u4e3a E \u5185\u7684\u533a\u57df<br \/>\n3. \u76f4\u63a5\u5bc6\u5ea6\u53ef\u8fbe&#xff1a;<br \/>\n4. \u5bc6\u5ea6\u53ef\u8fbe&#xff1a;<br \/>\n5. \u8fb9\u754c\u70b9&#xff1a;B \u70b9\u3001C \u70b9<br \/>\n6. \u79bb\u7fa4\u70b9&#xff1a;N \u70b9<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"251\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250810051421-68982aad20529.png\" width=\"335\" \/><\/p>\n<h4>\u5b9e\u73b0\u8fc7\u7a0b<\/h4>\n<li>\u8f93\u5165\u6570\u636e\u96c6<\/li>\n<li>\u6307\u5b9a\u534a\u5f84&#xff1b;<\/li>\n<li>\u6307\u5b9a\u5bc6\u5ea6\u9608\u503c&#xff1b;<\/li>\n<h2>\u4e8c\u00b7\u53c2\u6570<\/h2>\n<h4>1. \u521d\u59cb\u5316\u53c2\u6570<\/h4>\n<p>python<\/p>\n<p>class sklearn.cluster.DBSCAN(<br \/>\n    eps&#061;0.5,<br \/>\n    min_samples&#061;5,<br \/>\n    metric&#061;&#039;euclidean&#039;,<br \/>\n    metric_params&#061;None,<br \/>\n    algorithm&#061;&#039;auto&#039;,<br \/>\n    leaf_size&#061;30,<br \/>\n    p&#061;None,<br \/>\n    n_jobs&#061;None<br \/>\n)<\/p>\n<h4>2. \u5404\u53c2\u6570\u8be6\u7ec6\u8bf4\u660e<\/h4>\n<ul>\n<li>eps&#xff1a;DBSCAN \u7b97\u6cd5\u4e2d \u03b5- \u90bb\u57df\u7684\u8ddd\u79bb\u9608\u503c\u3002\u6837\u672c\u8ddd\u79bb\u8d85\u8fc7\u00a0eps\u00a0\u7684\u6837\u672c\u70b9\u4e0d\u5728 \u03b5- \u90bb\u57df\u5185&#xff0c;\u9ed8\u8ba4\u503c 0.5 &#xff0c;\u9700\u901a\u8fc7\u591a\u7ec4\u503c\u9009\u5408\u9002\u9608\u503c\u3002eps\u00a0\u8fc7\u5927&#xff0c;\u66f4\u591a\u70b9\u843d\u5165\u6838\u5fc3\u5bf9\u8c61 \u03b5- \u90bb\u57df&#xff0c;\u7c7b\u522b\u6570\u53ef\u80fd\u51cf\u5c11&#xff1b;\u8fc7\u5c0f\u5219\u7c7b\u522b\u6570\u53ef\u80fd\u589e\u5927&#xff0c;\u539f\u672c\u4e00\u7c7b\u7684\u6837\u672c\u4f1a\u88ab\u5206\u5f00\u3002<\/li>\n<li>min_samples&#xff1a;\u6837\u672c\u70b9\u6210\u4e3a\u6838\u5fc3\u5bf9\u8c61\u6240\u9700\u7684 \u03b5- \u90bb\u57df\u6837\u672c\u6570\u9608\u503c&#xff0c;\u9ed8\u8ba4\u503c 5 &#xff0c;\u5e38\u548c\u00a0eps\u00a0\u4e00\u8d77\u8c03\u53c2\u3002min_samples\u00a0\u8fc7\u5927&#xff0c;\u6838\u5fc3\u5bf9\u8c61\u8fc7\u5c11&#xff0c;\u7c07\u5185\u6837\u672c\u53ef\u80fd\u88ab\u6807\u4e3a\u566a\u97f3\u70b9&#xff0c;\u7c7b\u522b\u6570\u53d8\u591a&#xff1b;\u8fc7\u5c0f\u4f1a\u4ea7\u751f\u5927\u91cf\u6838\u5fc3\u5bf9\u8c61&#xff0c;\u53ef\u80fd\u5bfc\u81f4\u7c7b\u522b\u6570\u8fc7\u5c11\u3002<\/li>\n<li>metric&#xff1a;\u6700\u8fd1\u90bb\u8ddd\u79bb\u5ea6\u91cf\u53c2\u6570&#xff0c;\u5e38\u7528\u9ed8\u8ba4\u6b27\u5f0f\u8ddd\u79bb&#xff08;p&#061;2\u00a0\u7684\u95f5\u53ef\u592b\u65af\u57fa\u8ddd\u79bb &#xff09;\u5373\u53ef\u6ee1\u8db3\u9700\u6c42&#xff0c;\u53ef\u9009\u503c\u8fd8\u6709&#xff1a;\n<ul>\n<li>\u6b27\u5f0f\u8ddd\u79bb\u00a0\u201ceuclidean\u201d<\/li>\n<li>\u66fc\u54c8\u987f\u8ddd\u79bb\u00a0\u201cmanhattan\u201d<\/li>\n<li>\u5207\u6bd4\u96ea\u592b\u8ddd\u79bb\u00a0\u201cchebyshev\u201d<\/li>\n<\/ul>\n<\/li>\n<li>metric_params&#xff1a;\u8ddd\u79bb\u5ea6\u91cf\u7684\u989d\u5916\u53c2\u6570&#xff0c;\u4e00\u822c\u7528\u9ed8\u8ba4\u503c\u00a0None\u00a0\u5373\u53ef\u3002<\/li>\n<li>algorithm&#xff1a;\u6700\u8fd1\u90bb\u641c\u7d22\u7b97\u6cd5\u53c2\u6570&#xff0c;\u6709\u4e09\u79cd\u5b9e\u73b0\u65b9\u5f0f\u53ca\u56db\u79cd\u53ef\u9009\u8f93\u5165&#xff1a;\n<ul>\n<li>&#039;brute&#039;&#xff1a;\u86ee\u529b\u5b9e\u73b0<\/li>\n<li>&#039;kd_tree&#039;&#xff1a;KD \u6811\u5b9e\u73b0<\/li>\n<li>&#039;ball_tree&#039;&#xff1a;\u7403\u6811\u5b9e\u73b0<\/li>\n<li>&#039;auto&#039;&#xff1a;\u5728\u4e0a\u8ff0\u4e09\u79cd\u7b97\u6cd5\u4e2d\u6743\u8861\u9009\u6700\u4f18\u3002\u82e5\u8f93\u5165\u6837\u672c\u7279\u5f81\u7a00\u758f&#xff0c;\u6700\u7ec8\u4f1a\u7528\u00a0&#039;brute&#039;\u00a0\u3002\u4e00\u822c\u9ed8\u8ba4\u00a0&#039;auto&#039;\u00a0\u5373\u53ef&#xff0c;\u6570\u636e\u91cf\u5927 \/ \u7279\u5f81\u591a\u4e14\u00a0&#039;auto&#039;\u00a0\u5efa\u6811\u6162\u65f6&#xff0c;\u53ef\u6309\u9700\u8c03\u6574\u3002<\/li>\n<\/ul>\n<\/li>\n<li>leaf_size&#xff1a;\u6784\u5efa KD \u6811\u6216\u7403\u6811\u65f6\u7684\u53f6\u8282\u70b9\u5927\u5c0f&#xff0c;\u9ed8\u8ba4\u503c 30 &#xff0c;\u4f1a\u5f71\u54cd\u6811\u7684\u6784\u5efa\u901f\u5ea6\u548c\u67e5\u8be2\u6548\u7387&#xff0c;\u4e00\u822c\u7528\u9ed8\u8ba4\u503c\u3002<\/li>\n<li>p&#xff1a;\u95f5\u53ef\u592b\u65af\u57fa\u8ddd\u79bb\u4e2d\u00a0p\u00a0\u7684\u503c&#xff0c;p&#061;1\u00a0\u5bf9\u5e94\u66fc\u54c8\u987f\u8ddd\u79bb&#xff0c;p&#061;2\u00a0\u5bf9\u5e94\u6b27\u5f0f\u8ddd\u79bb&#xff0c;\u9ed8\u8ba4\u00a0None\u00a0&#xff08;\u6b64\u65f6\u7528\u9ed8\u8ba4\u6b27\u5f0f\u8ddd\u79bb &#xff09;\u3002<\/li>\n<li>n_jobs&#xff1a;\u7528\u4e8e\u5e76\u884c\u8ba1\u7b97\u7684 CPU \u6838\u5fc3\u6570&#xff0c;None\u00a0\u8868\u793a\u7528 1 \u4e2a\u6838\u5fc3&#xff0c;-1\u00a0\u8868\u793a\u7528\u6240\u6709\u53ef\u7528\u6838\u5fc3&#xff0c;\u53ef\u52a0\u901f\u6700\u8fd1\u90bb\u641c\u7d22\u7b49\u8fc7\u7a0b\u3002<\/li>\n<\/ul>\n<h4>3. \u5c5e\u6027<\/h4>\n<ul>\n<li>labels_&#xff1a;\u8bad\u7ec3\u540e&#xff0c;\u5b58\u50a8\u6bcf\u4e2a\u6837\u672c\u70b9\u7684\u5206\u7c7b\u6807\u7b7e&#xff0c;\u7528\u4e8e\u6807\u8bc6\u6837\u672c\u6240\u5c5e\u7684\u7c07&#xff08;\u5305\u62ec\u566a\u97f3\u70b9&#xff0c;\u566a\u97f3\u70b9\u6807\u7b7e\u901a\u5e38\u4e3a\u00a0-1\u00a0&#xff09;\u3002<\/li>\n<\/ul>\n<h2>\u4e09\u00b7\u4ee3\u7801<\/h2>\n<p>import pandas as pd<br \/>\nfrom sklearn.cluster import DBSCAN<br \/>\nfrom sklearn import metrics<\/p>\n<p># \u8bfb\u53d6\u6587\u4ef6<br \/>\nbeer &#061; pd.read_table(&#034;data.txt&#034;, sep&#061;&#039; &#039;, encoding&#061;&#039;utf8&#039;, engine&#061;&#039;python&#039;)<br \/>\n# \u4f20\u5165\u53d8\u91cf&#xff08;\u5217\u540d&#xff09;<br \/>\nX &#061; beer[[&#034;calories&#034;, &#034;sodium&#034;, &#034;alcohol&#034;, &#034;cost&#034;]]<br \/>\n# DBSCAN\u805a\u7c7b\u5206\u6790<br \/>\n&#034;&#034;&#034;<br \/>\neps:\u534a\u5f84<br \/>\nmin_samples: \u6700\u5c0f\u5bc6\u5ea6  \u3010\u5c31\u662f\u5706\u5185\u6700\u5c11\u6709\u51e0\u4e2a\u6837\u672c\u70b9\u3011<br \/>\nlabels: \u5206\u7c7b\u7ed3\u679c  \u3010\u81ea\u52a8\u5206\u7c7b&#xff0c;-1\u4e3a\u79bb\u7fa4\u70b9\u3011<br \/>\n&#034;&#034;&#034;<br \/>\ndb &#061; DBSCAN(eps&#061;20, min_samples&#061;2).fit(X)#\u5f52\u4e00\u5316&#xff0c;<br \/>\nlabels &#061; db.labels_<\/p>\n<p># \u6dfb\u52a0\u7ed3\u679c\u81f3\u539f\u6570\u636e\u6846<br \/>\n&#034;&#034;&#034;<br \/>\nmetrics.silhouette_score\u8f6e\u5ed3\u8bc4\u4ef7\u51fd\u6570&#xff0c;\u5b83\u662f\u805a\u7c7b\u6a21\u578b\u4f18\u52a3\u7684\u4e00\u79cd\u8bc4\u4f30\u65b9\u5f0f&#xff0c;\u53ef\u7528\u4e8e\u5bf9\u805a\u7c7b\u7ed3\u679c\u8fdb\u884c\u8bc4<br \/>\nX:\u6570\u636e\u96c6  scaled_cluster: \u805a\u7c7b\u7ed3\u679c<br \/>\nscore: \u975e\u6807\u51c6\u5316\u805a\u7c7b\u7ed3\u679c\u7684\u8f6e\u5ed3\u7cfb\u6570-&gt;\u805a\u7c7b<br \/>\n&#034;&#034;&#034;score &#061; metrics.silhouette_score(X, beer.cluster_db)<br \/>\nprint(score)<\/p>\n<p>#\u4f5c\u4e1a: KNN\u7684\u5bdd\u5ba4\u3002 y \u6682\u65f6\u4e0d\u7528y&#xff0c;\u7528kmens\u6216dbscan\u6765\u8bad\u7ec3\u3002 \u5c06\u4e00\u90e8\u5206\u4f5c\u4e3a\u6d4b\u8bd5\u96c6&#xff0c;\u6d4b\u8bd5\u4e00\u4e0b\u6548\u679c<\/p>\n<h4>1. \u5bfc\u5165\u6240\u9700\u5e93<\/h4>\n<p>\u9996\u5148\u5bfc\u5165\u4e86\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u805a\u7c7b\u6240\u9700\u7684\u5e93&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>pandas&#xff1a;\u7528\u4e8e\u6570\u636e\u8bfb\u53d6\u548c\u5904\u7406<\/li>\n<li>DBSCAN&#xff1a;\u6765\u81ea scikit-learn \u7684\u5bc6\u5ea6\u805a\u7c7b\u7b97\u6cd5<\/li>\n<li>metrics&#xff1a;\u6765\u81ea scikit-learn&#xff0c;\u7528\u4e8e\u805a\u7c7b\u7ed3\u679c\u8bc4\u4f30<\/li>\n<\/ul>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>import pandas as pd<br \/>\nfrom sklearn.cluster import DBSCAN<br \/>\nfrom sklearn import metrics<\/p>\n<h4>2. \u8bfb\u53d6\u6570\u636e\u6587\u4ef6<\/h4>\n<p>\u4f7f\u7528 pandas \u7684read_table\u51fd\u6570\u8bfb\u53d6\u6570\u636e\u6587\u4ef6&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>\u6570\u636e\u6587\u4ef6\u540d\u4e3a &#034;data.txt&#034;<\/li>\n<li>\u4f7f\u7528\u7a7a\u683c\u4f5c\u4e3a\u5206\u9694\u7b26&#xff08;sep&#061;&#039; &#039;&#xff09;<\/li>\n<li>\u6307\u5b9a\u7f16\u7801\u4e3a utf8<\/li>\n<li>\u4f7f\u7528 python \u5f15\u64ce\u89e3\u6790\u6587\u4ef6<\/li>\n<\/ul>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p># \u8bfb\u53d6\u6587\u4ef6<br \/>\nbeer &#061; pd.read_table(&#034;data.txt&#034;, sep&#061;&#039; &#039;, encoding&#061;&#039;utf8&#039;, engine&#061;&#039;python&#039;)<\/p>\n<h4>3. \u51c6\u5907\u7279\u5f81\u6570\u636e<\/h4>\n<p>\u4ece\u8bfb\u53d6\u7684\u6570\u636e\u4e2d\u9009\u53d6\u9700\u8981\u7528\u4e8e\u805a\u7c7b\u5206\u6790\u7684\u7279\u5f81\u5217&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>\u9009\u53d6\u4e86 &#034;calories&#034;&#xff08;\u5361\u8def\u91cc&#xff09;\u3001&#034;sodium&#034;&#xff08;\u94a0\u542b\u91cf&#xff09;\u3001&#034;alcohol&#034;&#xff08;\u9152\u7cbe\u542b\u91cf&#xff09;\u548c &#034;cost&#034;&#xff08;\u6210\u672c&#xff09;\u8fd9\u56db\u4e2a\u7279\u5f81<\/li>\n<li>\u8fd9\u4e9b\u7279\u5f81\u6570\u636e\u5c06\u4f5c\u4e3a DBSCAN \u7b97\u6cd5\u7684\u8f93\u5165<\/li>\n<\/ul>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p># \u4f20\u5165\u53d8\u91cf&#xff08;\u5217\u540d&#xff09;<br \/>\nX &#061; beer[[&#034;calories&#034;, &#034;sodium&#034;, &#034;alcohol&#034;, &#034;cost&#034;]]<\/p>\n<h4>4. \u6267\u884c DBSCAN \u805a\u7c7b<\/h4>\n<p>\u4f7f\u7528 DBSCAN \u7b97\u6cd5\u8fdb\u884c\u805a\u7c7b\u5206\u6790&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>eps&#061;20&#xff1a;\u8bbe\u7f6e\u805a\u7c7b\u7684\u534a\u5f84\u4e3a 20<\/li>\n<li>min_samples&#061;2&#xff1a;\u8bbe\u7f6e\u6bcf\u4e2a\u805a\u7c7b\u7684\u6700\u5c0f\u6837\u672c\u6570\u4e3a 2&#xff08;\u5373\u4e00\u4e2a\u805a\u7c7b\u81f3\u5c11\u9700\u8981\u5305\u542b 2 \u4e2a\u6837\u672c&#xff09;<\/li>\n<li>fit(X)&#xff1a;\u5bf9\u7279\u5f81\u6570\u636e X \u6267\u884c\u805a\u7c7b<\/li>\n<li>labels_&#xff1a;\u83b7\u53d6\u805a\u7c7b\u7ed3\u679c\u6807\u7b7e&#xff0c;\u5176\u4e2d &#8211; 1 \u8868\u793a\u79bb\u7fa4\u70b9<\/li>\n<\/ul>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p># DBSCAN\u805a\u7c7b\u5206\u6790<br \/>\n&#034;&#034;&#034;<br \/>\neps:\u534a\u5f84<br \/>\nmin_samples: \u6700\u5c0f\u5bc6\u5ea6  \u3010\u5c31\u662f\u5706\u5185\u6700\u5c11\u6709\u51e0\u4e2a\u6837\u672c\u70b9\u3011<br \/>\nlabels: \u5206\u7c7b\u7ed3\u679c  \u3010\u81ea\u52a8\u5206\u7c7b&#xff0c;-1\u4e3a\u79bb\u7fa4\u70b9\u3011<br \/>\n&#034;&#034;&#034;<br \/>\ndb &#061; DBSCAN(eps&#061;20, min_samples&#061;2).fit(X)#\u5f52\u4e00\u5316&#xff0c;<br \/>\nlabels &#061; db.labels_<\/p>\n<h4>5. \u8bc4\u4f30\u805a\u7c7b\u7ed3\u679c<\/h4>\n<p>\u4f7f\u7528\u8f6e\u5ed3\u7cfb\u6570&#xff08;silhouette score&#xff09;\u8bc4\u4f30\u805a\u7c7b\u6548\u679c&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>\u8f6e\u5ed3\u7cfb\u6570\u53d6\u503c\u8303\u56f4\u4e3a [-1, 1]<\/li>\n<li>\u63a5\u8fd1 1 \u8868\u793a\u805a\u7c7b\u6548\u679c\u597d&#xff0c;\u6837\u672c\u4e0e\u81ea\u8eab\u805a\u7c7b\u4e2d\u7684\u6837\u672c\u66f4\u76f8\u4f3c<\/li>\n<li>\u63a5\u8fd1 &#8211; 1 \u8868\u793a\u805a\u7c7b\u6548\u679c\u5dee&#xff0c;\u6837\u672c\u66f4\u5e94\u8be5\u5c5e\u4e8e\u5176\u4ed6\u805a\u7c7b<\/li>\n<\/ul>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p># \u8ba1\u7b97\u8f6e\u5ed3\u7cfb\u6570\u8bc4\u4f30\u805a\u7c7b\u7ed3\u679c<br \/>\n&#034;&#034;&#034;<br \/>\nmetrics.silhouette_score\u8f6e\u5ed3\u8bc4\u4ef7\u51fd\u6570&#xff0c;\u5b83\u662f\u805a\u7c7b\u6a21\u578b\u4f18\u52a3\u7684\u4e00\u79cd\u8bc4\u4f30\u65b9\u5f0f&#xff0c;\u53ef\u7528\u4e8e\u5bf9\u805a\u7c7b\u7ed3\u679c\u8fdb\u884c\u8bc4<br \/>\nX:\u6570\u636e\u96c6  labels: \u805a\u7c7b\u7ed3\u679c<br \/>\nscore: \u805a\u7c7b\u7ed3\u679c\u7684\u8f6e\u5ed3\u7cfb\u6570<br \/>\n&#034;&#034;&#034;<br \/>\nscore &#061; metrics.silhouette_score(X, labels)<br \/>\nprint(score)<\/p>\n<h4>\u6ce8\u610f\u4e8b\u9879<\/h4>\n<p>\u539f\u4ee3\u7801\u4e2d\u6709\u4e00\u5904\u7b14\u8bef&#xff0c;beer.cluster_db\u5e94\u8be5\u6539\u4e3alabels&#xff0c;\u56e0\u4e3a\u805a\u7c7b\u7ed3\u679c\u5b58\u50a8\u5728labels\u53d8\u91cf\u4e2d&#xff0c;\u800c\u4e0d\u662fbeer\u6570\u636e\u6846\u7684cluster_db\u5217\u4e2d&#xff08;\u8be5\u5217\u5c1a\u672a\u521b\u5efa&#xff09;\u3002\u4e0a\u9762\u7684\u4ee3\u7801\u5df2\u7ecf\u4fee\u6b63\u4e86\u8fd9\u4e00\u95ee\u9898\u3002<\/p>\n<\/p>\n<p>\u5982\u679c\u9700\u8981\u5c06\u805a\u7c7b\u7ed3\u679c\u6dfb\u52a0\u5230\u539f\u6570\u636e\u6846\u4e2d&#xff0c;\u53ef\u4ee5\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801&#xff1a;<\/p>\n<\/p>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p># \u6dfb\u52a0\u805a\u7c7b\u7ed3\u679c\u5230\u539f\u6570\u636e\u6846<br \/>\nbeer[&#039;cluster_db&#039;] &#061; labels<\/p>\n<p>\u4e0a\u9762\u5462\u662f\u8f6e\u5ed3\u7cfb\u6570&#xff0c;\u4e0b\u9762\u7684\u662f\u5206\u7c7b\u6307\u6807\u505a\u7684<\/p>\n<p>\u603b\u4ee3\u7801<\/p>\n<p>import os<br \/>\nimport pandas as pd<\/p>\n<p>file_path &#061; &#034;datingTestSet2.txt&#034;<br \/>\nif not os.path.exists(file_path):<br \/>\n    print(f&#034;Error: File &#039;{file_path}&#039; not found&#034;)<br \/>\n    exit(1)<\/p>\n<p>try:<br \/>\n    data &#061; pd.read_csv(file_path, sep&#061;None, engine&#061;&#039;python&#039;)<br \/>\nexcept Exception as e:<br \/>\n    print(f&#034;File read error: {e}&#034;)<br \/>\n    exit(1)<\/p>\n<p>X &#061; data.iloc[:, :-1]<br \/>\ny_true &#061; data.iloc[:, -1].astype(int)<\/p>\n<p>import numpy as np<br \/>\nfrom scipy.optimize import linear_sum_assignment<\/p>\n<p>def align_labels(true_labels, cluster_labels):<br \/>\n    unique_true &#061; np.unique(true_labels)<br \/>\n    unique_cluster &#061; np.unique(cluster_labels)<br \/>\n    n_true &#061; len(unique_true)<br \/>\n    n_cluster &#061; len(unique_cluster)<\/p>\n<p>    # \u53d6\u8f83\u5c0f\u7684\u7c7b\u522b\u6570\u4f5c\u4e3a\u77e9\u9635\u7ef4\u5ea6&#xff0c;\u907f\u514d\u4e0d\u5339\u914d<br \/>\n    n &#061; min(n_true, n_cluster)<br \/>\n    cost_matrix &#061; np.zeros((n, n))<\/p>\n<p>    for i in range(n):<br \/>\n        for j in range(n):<br \/>\n            cost_matrix[i, j] &#061; -np.sum((true_labels &#061;&#061; unique_true[i]) &amp; (cluster_labels &#061;&#061; unique_cluster[j]))<\/p>\n<p>    row_ind, col_ind &#061; linear_sum_assignment(cost_matrix)<br \/>\n    label_map &#061; {}<br \/>\n    # \u53ea\u6620\u5c04\u5b58\u5728\u7684\u7d22\u5f15&#xff0c;\u907f\u514d\u8d8a\u754c<br \/>\n    for i, j in zip(row_ind, col_ind):<br \/>\n        if j &lt; len(unique_cluster) and i &lt; len(unique_true):<br \/>\n            label_map[unique_cluster[j]] &#061; unique_true[i]<\/p>\n<p>    aligned_labels &#061; np.array([label_map.get(c, -1) for c in cluster_labels])<br \/>\n    return aligned_labels<\/p>\n<p>from sklearn.cluster import KMeans<br \/>\nimport matplotlib.pyplot as plt<br \/>\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score<\/p>\n<p>k_values &#061; [2, 3, 4, 5]<br \/>\nmetrics_kmeans &#061; {&#039;accuracy&#039;: [], &#039;precision&#039;: [], &#039;recall&#039;: [], &#039;f1&#039;: []}<\/p>\n<p>for k in k_values:<br \/>\n    kmeans &#061; KMeans(n_clusters&#061;k, random_state&#061;42).fit(X)<br \/>\n    aligned_labels &#061; align_labels(y_true, kmeans.labels_)<\/p>\n<p>    mask &#061; aligned_labels !&#061; -1<br \/>\n    if np.sum(mask) &gt; 0:<br \/>\n        acc &#061; accuracy_score(y_true[mask], aligned_labels[mask])<br \/>\n        prec &#061; precision_score(y_true[mask], aligned_labels[mask], average&#061;&#039;macro&#039;, zero_division&#061;0)<br \/>\n        rec &#061; recall_score(y_true[mask], aligned_labels[mask], average&#061;&#039;macro&#039;, zero_division&#061;0)<br \/>\n        f1 &#061; f1_score(y_true[mask], aligned_labels[mask], average&#061;&#039;macro&#039;, zero_division&#061;0)<\/p>\n<p>        metrics_kmeans[&#039;accuracy&#039;].append(acc)<br \/>\n        metrics_kmeans[&#039;precision&#039;].append(prec)<br \/>\n        metrics_kmeans[&#039;recall&#039;].append(rec)<br \/>\n        metrics_kmeans[&#039;f1&#039;].append(f1)<\/p>\n<p>        print(f&#034;KMeans(k&#061;{k}) &#8211; Acc: {acc:.3f}, Prec: {prec:.3f}, Rec: {rec:.3f}, F1: {f1:.3f}&#034;)<\/p>\n<p>plt.figure(figsize&#061;(12, 8))<br \/>\nfor i, metric in enumerate(metrics_kmeans.keys()):<br \/>\n    plt.subplot(2, 2, i &#043; 1)<br \/>\n    plt.plot(k_values, metrics_kmeans[metric], &#039;o-&#039;)<br \/>\n    plt.xlabel(&#039;k&#039;)<br \/>\n    plt.ylabel(metric)<br \/>\n    plt.title(f&#039;KMeans {metric}&#039;)<\/p>\n<p>plt.tight_layout()<br \/>\nplt.show()<\/p>\n<p>from sklearn.cluster import DBSCAN<\/p>\n<p>db &#061; DBSCAN(eps&#061;0.5, min_samples&#061;5).fit(X)<br \/>\ndb_labels &#061; db.labels_<\/p>\n<p>mask_noise &#061; db_labels !&#061; -1<br \/>\nif np.sum(mask_noise) &gt; 0:<br \/>\n    aligned_db_labels &#061; align_labels(y_true[mask_noise], db_labels[mask_noise])<\/p>\n<p>    acc_db &#061; accuracy_score(y_true[mask_noise], aligned_db_labels)<br \/>\n    prec_db &#061; precision_score(y_true[mask_noise], aligned_db_labels, average&#061;&#039;macro&#039;, zero_division&#061;0)<br \/>\n    rec_db &#061; recall_score(y_true[mask_noise], aligned_db_labels, average&#061;&#039;macro&#039;, zero_division&#061;0)<br \/>\n    f1_db &#061; f1_score(y_true[mask_noise], aligned_db_labels, average&#061;&#039;macro&#039;, zero_division&#061;0)<\/p>\n<p>    print(f&#034;\\\\nDBSCAN &#8211; Acc: {acc_db:.3f}, Prec: {prec_db:.3f}, Rec: {rec_db:.3f}, F1: {f1_db:.3f}&#034;)<br \/>\nelse:<br \/>\n    print(&#034;\\\\nDBSCAN: Too many noise points, cannot calculate metrics&#034;)<\/p>\n<p>data[&#039;db_cluster&#039;] &#061; db_labels<br \/>\nprint(&#034;\\\\nSample results:&#034;)<br \/>\nprint(data.head(10))<\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u662f\u4e0a\u4e00\u4e2a\u805a\u7c7b\u5206\u6790\u4ee3\u7801\u7684\u6539\u8fdb\u7248\u672c&#xff0c;\u6838\u5fc3\u533a\u522b\u5728\u4e8e\u8bc4\u4f30\u6307\u6807\u7684\u53d8\u5316&#xff1a;\u4e0a\u4e00\u4e2a\u4ee3\u7801\u4f7f\u7528\u8f6e\u5ed3\u7cfb\u6570&#xff08;\u805a\u7c7b\u4e13\u7528\u6307\u6807&#xff0c;\u9002\u7528\u4e8e\u65e0\u771f\u5b9e\u6807\u7b7e\u7684\u60c5\u51b5&#xff09;&#xff0c;\u800c\u672c\u4ee3\u7801\u56e0\u4e3a\u6709\u771f\u5b9e\u6807\u7b7e&#xff08;y_true&#xff09;&#xff0c;\u6240\u4ee5\u91c7\u7528\u4e86\u5206\u7c7b\u4efb\u52a1\u4e2d\u5e38\u7528\u7684\u8bc4\u4f30\u6307\u6807&#xff08;\u51c6\u786e\u7387\u3001\u7cbe\u786e\u7387\u3001\u53ec\u56de\u7387\u3001F1 \u503c&#xff09;\u6765\u66f4\u76f4\u89c2\u5730\u8861\u91cf\u805a\u7c7b\u6548\u679c\u4e0e\u771f\u5b9e\u7c7b\u522b\u7684\u5339\u914d\u7a0b\u5ea6\u3002<\/p>\n<h4>1. \u5bfc\u5165\u57fa\u7840\u5e93\u4e0e\u6587\u4ef6\u5904\u7406<\/h4>\n<p>\u9996\u5148\u5bfc\u5165\u57fa\u7840\u5de5\u5177\u5e93&#xff0c;\u68c0\u67e5\u5e76\u8bfb\u53d6\u6570\u636e\u6587\u4ef6&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>os&#xff1a;\u7528\u4e8e\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u5b58\u5728<\/li>\n<li>pandas&#xff1a;\u7528\u4e8e\u6570\u636e\u8bfb\u53d6\u548c\u5904\u7406<\/li>\n<\/ul>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>import os<br \/>\nimport pandas as pd<\/p>\n<p>file_path &#061; &#034;datingTestSet2.txt&#034;<br \/>\nif not os.path.exists(file_path):<br \/>\n    print(f&#034;Error: File &#039;{file_path}&#039; not found&#034;)<br \/>\n    exit(1)<\/p>\n<p>try:<br \/>\n    data &#061; pd.read_csv(file_path, sep&#061;None, engine&#061;&#039;python&#039;)  # \u81ea\u52a8\u8bc6\u522b\u5206\u9694\u7b26\u8bfb\u53d6\u6570\u636e<br \/>\nexcept Exception as e:<br \/>\n    print(f&#034;File read error: {e}&#034;)<br \/>\n    exit(1)<\/p>\n<h4>2. \u51c6\u5907\u7279\u5f81\u6570\u636e\u4e0e\u771f\u5b9e\u6807\u7b7e<\/h4>\n<p>\u4ece\u8bfb\u53d6\u7684\u6570\u636e\u4e2d\u62c6\u5206\u51fa\u7279\u5f81\u548c\u771f\u5b9e\u6807\u7b7e&#xff08;\u7528\u4e8e\u540e\u7eed\u8bc4\u4f30\u805a\u7c7b\u6548\u679c&#xff09;&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>X&#xff1a;\u6240\u6709\u884c&#xff0c;\u9664\u6700\u540e\u4e00\u5217\u5916\u7684\u5217&#xff08;\u7279\u5f81\u6570\u636e&#xff09;<\/li>\n<li>y_true&#xff1a;\u6240\u6709\u884c\u7684\u6700\u540e\u4e00\u5217&#xff08;\u771f\u5b9e\u7c7b\u522b\u6807\u7b7e&#xff0c;\u8f6c\u4e3a\u6574\u6570\u7c7b\u578b&#xff09;<\/li>\n<\/ul>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>X &#061; data.iloc[:, :-1]  # \u7279\u5f81\u6570\u636e&#xff08;\u6240\u6709\u7279\u5f81\u5217&#xff09;<br \/>\ny_true &#061; data.iloc[:, -1].astype(int)  # \u771f\u5b9e\u6807\u7b7e&#xff08;\u6700\u540e\u4e00\u5217&#xff09;<\/p>\n<h4>3. \u5bfc\u5165\u6807\u7b7e\u5bf9\u9f50\u6240\u9700\u5e93<\/h4>\n<p>\u805a\u7c7b\u7b97\u6cd5\u8f93\u51fa\u7684\u6807\u7b7e\u4ec5\u4ee3\u8868\u7c7b\u522b\u7f16\u53f7&#xff08;\u4e0e\u771f\u5b9e\u6807\u7b7e\u7684\u7f16\u53f7\u53ef\u80fd\u4e0d\u4e00\u81f4&#xff09;&#xff0c;\u9700\u8981\u901a\u8fc7\u6807\u7b7e\u5bf9\u9f50\u5c06\u805a\u7c7b\u6807\u7b7e\u6620\u5c04\u5230\u771f\u5b9e\u6807\u7b7e&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>numpy&#xff1a;\u7528\u4e8e\u6570\u503c\u8ba1\u7b97<\/li>\n<li>linear_sum_assignment&#xff1a;\u7528\u4e8e\u627e\u5230\u6700\u4f18\u6807\u7b7e\u6620\u5c04\u5173\u7cfb&#xff08;\u5308\u7259\u5229\u7b97\u6cd5&#xff09;<\/li>\n<\/ul>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>import numpy as np<br \/>\nfrom scipy.optimize import linear_sum_assignment<\/p>\n<h4>4. \u5b9a\u4e49\u6807\u7b7e\u5bf9\u9f50\u51fd\u6570<\/h4>\n<p>align_labels\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u805a\u7c7b\u8f93\u51fa\u7684\u6807\u7b7e&#xff08;cluster_labels&#xff09;\u4e0e\u771f\u5b9e\u6807\u7b7e&#xff08;true_labels&#xff09;\u8fdb\u884c\u6620\u5c04&#xff0c;\u89e3\u51b3 \u201c\u7c7b\u522b\u7f16\u53f7\u4e0d\u5339\u914d\u201d \u95ee\u9898&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>\u6784\u5efa\u6210\u672c\u77e9\u9635&#xff1a;\u8ba1\u7b97\u805a\u7c7b\u6807\u7b7e\u4e0e\u771f\u5b9e\u6807\u7b7e\u7684\u5339\u914d\u7a0b\u5ea6<\/li>\n<li>\u7528\u5308\u7259\u5229\u7b97\u6cd5\u627e\u5230\u6700\u4f18\u6620\u5c04\u5173\u7cfb<\/li>\n<li>\u5c06\u805a\u7c7b\u6807\u7b7e\u8f6c\u6362\u4e3a\u4e0e\u771f\u5b9e\u6807\u7b7e\u4e00\u81f4\u7684\u7f16\u53f7&#xff08;\u65e0\u6cd5\u6620\u5c04\u7684\u6807\u7b7e\u8bb0\u4e3a &#8211; 1&#xff09;<\/li>\n<\/ul>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>def align_labels(true_labels, cluster_labels):<br \/>\n    unique_true &#061; np.unique(true_labels)  # \u771f\u5b9e\u6807\u7b7e\u7684\u552f\u4e00\u503c<br \/>\n    unique_cluster &#061; np.unique(cluster_labels)  # \u805a\u7c7b\u6807\u7b7e\u7684\u552f\u4e00\u503c<br \/>\n    n_true &#061; len(unique_true)<br \/>\n    n_cluster &#061; len(unique_cluster)<\/p>\n<p>    # \u53d6\u8f83\u5c0f\u7684\u7c7b\u522b\u6570\u4f5c\u4e3a\u77e9\u9635\u7ef4\u5ea6&#xff0c;\u907f\u514d\u4e0d\u5339\u914d<br \/>\n    n &#061; min(n_true, n_cluster)<br \/>\n    cost_matrix &#061; np.zeros((n, n))<\/p>\n<p>    # \u6784\u5efa\u6210\u672c\u77e9\u9635&#xff08;\u503c\u8d8a\u5c0f\u8868\u793a\u5339\u914d\u5ea6\u8d8a\u9ad8&#xff09;<br \/>\n    for i in range(n):<br \/>\n        for j in range(n):<br \/>\n            # \u8ba1\u7b97\u805a\u7c7b\u6807\u7b7ej\u4e0e\u771f\u5b9e\u6807\u7b7ei\u7684\u91cd\u53e0\u6570\u91cf&#xff08;\u53d6\u8d1f\u53f7\u8f6c\u4e3a\u6210\u672c&#xff09;<br \/>\n            cost_matrix[i, j] &#061; -np.sum((true_labels &#061;&#061; unique_true[i]) &amp; (cluster_labels &#061;&#061; unique_cluster[j]))<\/p>\n<p>    # \u7528\u5308\u7259\u5229\u7b97\u6cd5\u627e\u5230\u6700\u4f18\u6620\u5c04<br \/>\n    row_ind, col_ind &#061; linear_sum_assignment(cost_matrix)<br \/>\n    label_map &#061; {}<br \/>\n    # \u6784\u5efa\u6620\u5c04\u5173\u7cfb&#xff08;\u805a\u7c7b\u6807\u7b7e\u2192\u771f\u5b9e\u6807\u7b7e&#xff09;<br \/>\n    for i, j in zip(row_ind, col_ind):<br \/>\n        if j &lt; len(unique_cluster) and i &lt; len(unique_true):<br \/>\n            label_map[unique_cluster[j]] &#061; unique_true[i]<\/p>\n<p>    # \u5c06\u805a\u7c7b\u6807\u7b7e\u8f6c\u6362\u4e3a\u6620\u5c04\u540e\u7684\u771f\u5b9e\u6807\u7b7e\u683c\u5f0f<br \/>\n    aligned_labels &#061; np.array([label_map.get(c, -1) for c in cluster_labels])<br \/>\n    return aligned_labels<\/p>\n<h4>5. \u5bfc\u5165 KMeans \u805a\u7c7b\u4e0e\u8bc4\u4f30\u5e93<\/h4>\n<p>\u5bfc\u5165 KMeans \u7b97\u6cd5\u3001\u7ed8\u56fe\u5de5\u5177\u548c\u5206\u7c7b\u8bc4\u4f30\u6307\u6807&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>KMeans&#xff1a;\u805a\u7c7b\u7b97\u6cd5<\/li>\n<li>matplotlib.pyplot&#xff1a;\u7528\u4e8e\u7ed8\u5236\u6307\u6807\u53d8\u5316\u56fe<\/li>\n<li>\u5206\u7c7b\u8bc4\u4f30\u6307\u6807&#xff1a;accuracy_score&#xff08;\u51c6\u786e\u7387&#xff09;\u3001precision_score&#xff08;\u7cbe\u786e\u7387&#xff09;\u7b49<\/li>\n<\/ul>\n<p>python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>from sklearn.cluster import KMeans<br \/>\nimport matplotlib.pyplot as plt<br \/>\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score<\/p>\n<h4>6. \u6267\u884c KMeans \u805a\u7c7b\u4e0e\u8bc4\u4f30<\/h4>\n<p>\u5c1d\u8bd5\u4e0d\u540c\u7684\u805a\u7c7b\u6570&#xff08;k&#061;2,3,4,5&#xff09;&#xff0c;\u5bf9\u6bcf\u4e2ak\u8fdb\u884c\u805a\u7c7b\u5e76\u8bc4\u4f30&#xff1a;<\/p>\n<ul>\n<li>\u805a\u7c7b\u540e\u901a\u8fc7align_labels\u5bf9\u9f50\u6807\u7b7e<\/li>\n<li>\u8fc7\u6ee4\u65e0\u6cd5\u6620\u5c04\u7684\u6807\u7b7e&#xff08;-1&#xff09;&#xff0c;\u8ba1\u7b97\u8bc4\u4f30\u6307\u6807<\/li>\n<li>\u5b58\u50a8\u6307\u6807\u7ed3\u679c\u5e76\u6253\u5370<\/li>\n<\/ul>\n<p>python\u8fd0\u884c<\/p>\n<p>k_values &#061; [2, 3, 4, 5]  # \u5c1d\u8bd5\u7684\u805a\u7c7b\u6570<br \/>\nmetrics_kmeans &#061; {&#039;accuracy&#039;: [], &#039;precision&#039;: [], &#039;recall&#039;: [], &#039;f1&#039;: []}  # \u5b58\u50a8\u5404\u6307\u6807\u7ed3\u679c<\/p>\n<p>for k in k_values:<br \/>\n    # \u8bad\u7ec3KMeans\u6a21\u578b<br \/>\n    kmeans &#061; KMeans(n_clusters&#061;k, random_state&#061;42).fit(X)<br \/>\n    # \u5bf9\u9f50\u805a\u7c7b\u6807\u7b7e\u4e0e\u771f\u5b9e\u6807\u7b7e<br \/>\n    aligned_labels &#061; align_labels(y_true, kmeans.labels_)<\/p>\n<p>    # \u8fc7\u6ee4\u65e0\u6cd5\u6620\u5c04\u7684\u6807\u7b7e&#xff08;\u4ec5\u4fdd\u7559\u6709\u6548\u6807\u7b7e&#xff09;<br \/>\n    mask &#061; aligned_labels !&#061; -1<br \/>\n    if np.sum(mask) &gt; 0:<br \/>\n        # \u8ba1\u7b97\u8bc4\u4f30\u6307\u6807<br \/>\n        acc &#061; accuracy_score(y_true[mask], aligned_labels[mask])<br \/>\n        prec &#061; precision_score(y_true[mask], aligned_labels[mask], average&#061;&#039;macro&#039;, zero_division&#061;0)<br \/>\n        rec &#061; recall_score(y_true[mask], aligned_labels[mask], average&#061;&#039;macro&#039;, zero_division&#061;0)<br \/>\n        f1 &#061; f1_score(y_true[mask], aligned_labels[mask], average&#061;&#039;macro&#039;, zero_division&#061;0)<\/p>\n<p>        # \u5b58\u50a8\u6307\u6807<br \/>\n        metrics_kmeans[&#039;accuracy&#039;].append(acc)<br \/>\n        metrics_kmeans[&#039;precision&#039;].append(prec)<br \/>\n        metrics_kmeans[&#039;recall&#039;].append(rec)<br \/>\n        metrics_kmeans[&#039;f1&#039;].append(f1)<\/p>\n<p>        # \u6253\u5370\u7ed3\u679c<br \/>\n        print(f&#034;KMeans(k&#061;{k}) &#8211; Acc: {acc:.3f}, Prec: {prec:.3f}, Rec: {rec:.3f}, F1: {f1:.3f}&#034;)<\/p>\n<h4>7. \u7ed8\u5236 KMeans \u6307\u6807\u53d8\u5316\u56fe<\/h4>\n<p>\u7528\u5b50\u56fe\u5c55\u793a\u4e0d\u540ck\u503c\u4e0b\u5404\u8bc4\u4f30\u6307\u6807\u7684\u53d8\u5316\u8d8b\u52bf&#xff0c;\u76f4\u89c2\u5bf9\u6bd4\u805a\u7c7b\u6548\u679c&#xff1a;<\/p>\n<p>python\u8fd0\u884c<\/p>\n<p>plt.figure(figsize&#061;(12, 8))<br \/>\nfor i, metric in enumerate(metrics_kmeans.keys()):<br \/>\n    plt.subplot(2, 2, i &#043; 1)  # 2\u884c2\u5217\u5b50\u56fe<br \/>\n    plt.plot(k_values, metrics_kmeans[metric], &#039;o-&#039;)  # \u7ed8\u5236\u6298\u7ebf\u56fe<br \/>\n    plt.xlabel(&#039;k&#039;)  # x\u8f74&#xff1a;\u805a\u7c7b\u6570k<br \/>\n    plt.ylabel(metric)  # y\u8f74&#xff1a;\u8bc4\u4f30\u6307\u6807<br \/>\n    plt.title(f&#039;KMeans {metric}&#039;)  # \u5b50\u56fe\u6807\u9898<\/p>\n<p>plt.tight_layout()  # \u8c03\u6574\u5e03\u5c40<br \/>\nplt.show()  # \u663e\u793a\u56fe\u50cf<\/p>\n<h4>8. \u6267\u884c DBSCAN \u805a\u7c7b\u4e0e\u8bc4\u4f30<\/h4>\n<p>\u4f7f\u7528 DBSCAN \u7b97\u6cd5\u8fdb\u884c\u805a\u7c7b&#xff0c;\u540c\u6837\u901a\u8fc7\u6807\u7b7e\u5bf9\u9f50\u548c\u5206\u7c7b\u6307\u6807\u8bc4\u4f30&#xff1a;<\/p>\n<\/p>\n<ul>\n<li>\u5148\u8fc7\u6ee4\u566a\u58f0\u70b9&#xff08;DBSCAN \u4e2d\u6807\u7b7e\u4e3a-1\u7684\u6837\u672c&#xff09;<\/li>\n<li>\u5bf9\u6709\u6548\u6837\u672c\u7684\u6807\u7b7e\u8fdb\u884c\u5bf9\u9f50&#xff0c;\u8ba1\u7b97\u8bc4\u4f30\u6307\u6807\u5e76\u6253\u5370<\/li>\n<li>python\u8fd0\u884c<\/li>\n<\/ul>\n<p>from sklearn.cluster import DBSCAN<\/p>\n<p># \u8bad\u7ec3DBSCAN\u6a21\u578b&#xff08;eps&#xff1a;\u534a\u5f84&#xff0c;min_samples&#xff1a;\u6700\u5c0f\u6837\u672c\u6570&#xff09;<br \/>\ndb &#061; DBSCAN(eps&#061;0.5, min_samples&#061;5).fit(X)<br \/>\ndb_labels &#061; db.labels_  # \u83b7\u53d6\u805a\u7c7b\u6807\u7b7e&#xff08;-1\u4e3a\u566a\u58f0\u70b9&#xff09;<\/p>\n<p># \u8fc7\u6ee4\u566a\u58f0\u70b9<br \/>\nmask_noise &#061; db_labels !&#061; -1<br \/>\nif np.sum(mask_noise) &gt; 0:<br \/>\n    # \u5bf9\u9f50\u6709\u6548\u6837\u672c\u7684\u6807\u7b7e<br \/>\n    aligned_db_labels &#061; align_labels(y_true[mask_noise], db_labels[mask_noise])<\/p>\n<p>    # \u8ba1\u7b97\u8bc4\u4f30\u6307\u6807<br \/>\n    acc_db &#061; accuracy_score(y_true[mask_noise], aligned_db_labels)<br \/>\n    prec_db &#061; precision_score(y_true[mask_noise], aligned_db_labels, average&#061;&#039;macro&#039;, zero_division&#061;0)<br \/>\n    rec_db &#061; recall_score(y_true[mask_noise], aligned_db_labels, average&#061;&#039;macro&#039;, zero_division&#061;0)<br \/>\n    f1_db &#061; f1_score(y_true[mask_noise], aligned_db_labels, average&#061;&#039;macro&#039;, zero_division&#061;0)<\/p>\n<p>    # \u6253\u5370\u7ed3\u679c<br \/>\n    print(f&#034;\\\\nDBSCAN &#8211; Acc: {acc_db:.3f}, Prec: {prec_db:.3f}, Rec: {rec_db:.3f}, F1: {f1_db:.3f}&#034;)<br \/>\nelse:<br \/>\n    print(&#034;\\\\nDBSCAN: Too many noise points, cannot calculate metrics&#034;)<\/p>\n<h4>9. \u5c55\u793a\u805a\u7c7b\u7ed3\u679c\u6837\u672c<\/h4>\n<p>\u5c06 DBSCAN \u7684\u805a\u7c7b\u7ed3\u679c\u6dfb\u52a0\u5230\u539f\u59cb\u6570\u636e\u4e2d&#xff0c;\u6253\u5370\u524d 10 \u884c\u6837\u672c&#xff0c;\u76f4\u89c2\u67e5\u770b\u805a\u7c7b\u6807\u7b7e&#xff1a;python<\/p>\n<p>\u8fd0\u884c<\/p>\n<p>data[&#039;db_cluster&#039;] &#061; db_labels  # \u6dfb\u52a0DBSCAN\u805a\u7c7b\u7ed3\u679c\u5230\u6570\u636e\u6846<br \/>\nprint(&#034;\\\\nSample results:&#034;)<br \/>\nprint(data.head(10))  # \u663e\u793a\u524d10\u884c\u6570\u636e\u53ca\u805a\u7c7b\u6807\u7b7e<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb328\u6b21\uff0c\u70b9\u8d5e3\u6b21\uff0c\u6536\u85cf3\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86DBSCAN\u5bc6\u5ea6\u805a\u7c7b\u7b97\u6cd5\u7684\u539f\u7406\u3001\u53c2\u6570\u8c03\u4f18\u53ca\u5b9e\u73b0\u8fc7\u7a0b\u3002DBSCAN\u901a\u8fc7\u5b9a\u4e49\u6838\u5fc3\u5bf9\u8c61\u3001E\u90bb\u57df\u548c\u5bc6\u5ea6\u53ef\u8fbe\u7b49\u6982\u5ff5\uff0c\u80fd\u591f\u53d1\u73b0\u4efb\u610f\u5f62\u72b6\u7684\u805a\u7c7b\u5e76\u8bc6\u522b\u566a\u58f0\u70b9\u3002\u6587\u4e2d\u8be6\u7ec6\u8bf4\u660e\u4e86Python\u4e2dsklearn.cluster.DBSCAN\u7684\u53c2\u6570\u8bbe\u7f6e\uff0c\u5305\u62eceps\u90bb\u57df\u534a\u5f84\u3001min_samples\u6700\u5c0f\u6837\u672c\u6570\u7b49\u5173\u952e\u53c2\u6570\u7684\u5f71\u54cd\u3002\u540c\u65f6\u63d0\u4f9b\u4e86\u5b8c\u6574\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u5305\u62ec\u6570\u636e\u9884\u5904\u7406\u3001DBSCAN\u6a21\u578b\u8bad\u7ec3\u3001\u8f6e\u5ed3\u7cfb\u6570\u8bc4\u4f30\u7b49\u6b65\u9aa4\uff0c\u5e76\u5bf9\u6bd4\u4e86KMeans\u4e0eDBSCAN\u5728\u771f\u5b9e\u6570\u636e\u96c6\u4e0a\u7684\u8868\u73b0\u3002\u6700\u540e\u901a\u8fc7\u6807\u7b7e\u5bf9\u9f50\u65b9\u6cd5\u89e3\u51b3\u4e86\u805a\u7c7b\u6807\u7b7e\u4e0e\u771f\u5b9e\u6807\u7b7e\u7684\u6620\u5c04\u95ee\u9898\uff0c\u91c7\u7528\u5206\u7c7b\u6307<\/p>\n","protected":false},"author":2,"featured_media":50860,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[50,207,427],"topic":[],"class_list":["post-50863","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-50","tag-207","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u673a\u5668\u5b66\u4e60\u2014\u2014DBSCAN - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/50863.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u673a\u5668\u5b66\u4e60\u2014\u2014DBSCAN - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb328\u6b21\uff0c\u70b9\u8d5e3\u6b21\uff0c\u6536\u85cf3\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86DBSCAN\u5bc6\u5ea6\u805a\u7c7b\u7b97\u6cd5\u7684\u539f\u7406\u3001\u53c2\u6570\u8c03\u4f18\u53ca\u5b9e\u73b0\u8fc7\u7a0b\u3002DBSCAN\u901a\u8fc7\u5b9a\u4e49\u6838\u5fc3\u5bf9\u8c61\u3001E\u90bb\u57df\u548c\u5bc6\u5ea6\u53ef\u8fbe\u7b49\u6982\u5ff5\uff0c\u80fd\u591f\u53d1\u73b0\u4efb\u610f\u5f62\u72b6\u7684\u805a\u7c7b\u5e76\u8bc6\u522b\u566a\u58f0\u70b9\u3002\u6587\u4e2d\u8be6\u7ec6\u8bf4\u660e\u4e86Python\u4e2dsklearn.cluster.DBSCAN\u7684\u53c2\u6570\u8bbe\u7f6e\uff0c\u5305\u62eceps\u90bb\u57df\u534a\u5f84\u3001min_samples\u6700\u5c0f\u6837\u672c\u6570\u7b49\u5173\u952e\u53c2\u6570\u7684\u5f71\u54cd\u3002\u540c\u65f6\u63d0\u4f9b\u4e86\u5b8c\u6574\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u5305\u62ec\u6570\u636e\u9884\u5904\u7406\u3001DBSCAN\u6a21\u578b\u8bad\u7ec3\u3001\u8f6e\u5ed3\u7cfb\u6570\u8bc4\u4f30\u7b49\u6b65\u9aa4\uff0c\u5e76\u5bf9\u6bd4\u4e86KMeans\u4e0eDBSCAN\u5728\u771f\u5b9e\u6570\u636e\u96c6\u4e0a\u7684\u8868\u73b0\u3002\u6700\u540e\u901a\u8fc7\u6807\u7b7e\u5bf9\u9f50\u65b9\u6cd5\u89e3\u51b3\u4e86\u805a\u7c7b\u6807\u7b7e\u4e0e\u771f\u5b9e\u6807\u7b7e\u7684\u6620\u5c04\u95ee\u9898\uff0c\u91c7\u7528\u5206\u7c7b\u6307\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/50863.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-08-10T05:14:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250810051420-68982aac54013.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/50863.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/50863.html\",\"name\":\"\u673a\u5668\u5b66\u4e60\u2014\u2014DBSCAN - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-08-10T05:14:24+00:00\",\"dateModified\":\"2025-08-10T05:14:24+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/50863.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/50863.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/50863.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u673a\u5668\u5b66\u4e60\u2014\u2014DBSCAN\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u673a\u5668\u5b66\u4e60\u2014\u2014DBSCAN - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","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:\/\/www.wsisp.com\/helps\/50863.html","og_locale":"zh_CN","og_type":"article","og_title":"\u673a\u5668\u5b66\u4e60\u2014\u2014DBSCAN - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb328\u6b21\uff0c\u70b9\u8d5e3\u6b21\uff0c\u6536\u85cf3\u6b21\u3002\u672c\u6587\u4ecb\u7ecd\u4e86DBSCAN\u5bc6\u5ea6\u805a\u7c7b\u7b97\u6cd5\u7684\u539f\u7406\u3001\u53c2\u6570\u8c03\u4f18\u53ca\u5b9e\u73b0\u8fc7\u7a0b\u3002DBSCAN\u901a\u8fc7\u5b9a\u4e49\u6838\u5fc3\u5bf9\u8c61\u3001E\u90bb\u57df\u548c\u5bc6\u5ea6\u53ef\u8fbe\u7b49\u6982\u5ff5\uff0c\u80fd\u591f\u53d1\u73b0\u4efb\u610f\u5f62\u72b6\u7684\u805a\u7c7b\u5e76\u8bc6\u522b\u566a\u58f0\u70b9\u3002\u6587\u4e2d\u8be6\u7ec6\u8bf4\u660e\u4e86Python\u4e2dsklearn.cluster.DBSCAN\u7684\u53c2\u6570\u8bbe\u7f6e\uff0c\u5305\u62eceps\u90bb\u57df\u534a\u5f84\u3001min_samples\u6700\u5c0f\u6837\u672c\u6570\u7b49\u5173\u952e\u53c2\u6570\u7684\u5f71\u54cd\u3002\u540c\u65f6\u63d0\u4f9b\u4e86\u5b8c\u6574\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u5305\u62ec\u6570\u636e\u9884\u5904\u7406\u3001DBSCAN\u6a21\u578b\u8bad\u7ec3\u3001\u8f6e\u5ed3\u7cfb\u6570\u8bc4\u4f30\u7b49\u6b65\u9aa4\uff0c\u5e76\u5bf9\u6bd4\u4e86KMeans\u4e0eDBSCAN\u5728\u771f\u5b9e\u6570\u636e\u96c6\u4e0a\u7684\u8868\u73b0\u3002\u6700\u540e\u901a\u8fc7\u6807\u7b7e\u5bf9\u9f50\u65b9\u6cd5\u89e3\u51b3\u4e86\u805a\u7c7b\u6807\u7b7e\u4e0e\u771f\u5b9e\u6807\u7b7e\u7684\u6620\u5c04\u95ee\u9898\uff0c\u91c7\u7528\u5206\u7c7b\u6307","og_url":"https:\/\/www.wsisp.com\/helps\/50863.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-08-10T05:14:24+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/08\/20250810051420-68982aac54013.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"9 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/50863.html","url":"https:\/\/www.wsisp.com\/helps\/50863.html","name":"\u673a\u5668\u5b66\u4e60\u2014\u2014DBSCAN - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-08-10T05:14:24+00:00","dateModified":"2025-08-10T05:14:24+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/50863.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/50863.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/50863.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u673a\u5668\u5b66\u4e60\u2014\u2014DBSCAN"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/50863","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=50863"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/50863\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/50860"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=50863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=50863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=50863"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=50863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}