{"id":7568,"date":"2020-09-07T18:59:59","date_gmt":"2020-09-07T10:59:59","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=7568"},"modified":"2020-09-07T19:03:39","modified_gmt":"2020-09-07T11:03:39","slug":"%e4%bd%bf%e7%94%a8-dbscan-%e8%81%9a%e7%b1%bb%e7%ae%97%e6%b3%95%e5%89%94%e9%99%a4%e5%bc%82%e5%b8%b8%e7%9f%a9%e5%bd%a2","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/7568.html","title":{"rendered":"\u4f7f\u7528 DBSCAN \u805a\u7c7b\u7b97\u6cd5\u5254\u9664\u5f02\u5e38\u77e9\u5f62"},"content":{"rendered":"<h2>\u95ee\u9898\u63cf\u8ff0<\/h2>\n<p>\u5047\u5982\u73b0\u5728\u6709\u4e00\u7ec4\u77e9\u5f62\u6846\uff0c\u5df2\u77e5\u6bcf\u4e2a\u77e9\u5f62\u56db\u4e2a\u9876\u70b9\u7684\u5750\u6807\uff0c\u8fd9\u4e9b\u77e9\u5f62\u6709\u4e9b\u662f\u6328\u4e00\u8d77\u7684\uff0c\u6709\u4e9b\u79bb\u5f97\u6bd4\u8f83\u8fdc\uff0c\u73b0\u5728\u9700\u8981\u89e3\u51b3\u7684\u95ee\u9898\u662f\uff0c\u5c06\u90a3\u4e9b\u660e\u663e \u201c\u4e0d\u5408\u7fa4\u201d \u7684\u77e9\u5f62\u627e\u51fa\u6765\u3002<\/p>\n<p>\u5df2\u77e5\u7684\u793a\u4f8b\u6570\u636e\u5982\u4e0b\uff0c\u6bcf\u4e00\u884c\u662f\u4e00\u4e2a\u77e9\u5f62\u7684\u56db\u4e2a\u9876\u70b9\u5750\u6807\uff0c\u683c\u5f0f\u4e3a x1 y1 x2 y2 x3 y3 x4 y4\uff0c\u4e0b\u9762\u662f 100 \u4e2a\u77e9\u5f62\u7684\u6d4b\u8bd5\u6570\u636e\uff1a<\/p>\n<pre><code class=\"language-text \">904.00 41.00 904.00 61.00 869.00 61.00 869.00 41.00\n103.00 35.00 103.00 67.00 62.00 67.00 62.00 35.00\n598.11 23.05 597.95 69.05 285.89 67.95 286.05 21.95\n682.00 23.00 682.00 70.00 602.00 70.00 602.00 23.00\n612.00 66.00 612.00 96.00 350.00 96.00 350.00 66.00\n328.03 65.04 327.90 28.03 629.97 26.96 630.10 63.97\n325.93 92.97 326.03 64.97 633.07 66.03 632.97 94.03\n329.03 65.04 328.90 28.03 629.97 26.96 630.10 63.97\n325.93 92.97 326.03 64.97 634.07 66.03 633.97 94.03\n568.00 28.00 568.00 64.00 203.00 64.00 203.00 28.00\n759.03 67.02 758.98 94.02 199.97 92.98 200.02 65.98\n756.00 26.00 756.00 66.00 550.00 66.00 550.00 26.00\n553.14 66.13 552.72 29.14 755.86 26.87 756.28 63.86\n759.03 67.02 758.98 94.02 199.97 92.98 200.02 65.98\n568.00 28.00 568.00 64.00 203.00 64.00 203.00 28.00\n533.00 26.00 533.00 68.00 424.00 68.00 424.00 26.00\n520.00 66.00 520.00 94.00 439.00 94.00 439.00 66.00\n567.00 25.00 567.00 68.00 393.00 68.00 393.00 25.00\n533.00 67.00 533.00 95.00 427.00 95.00 427.00 67.00\n664.00 25.00 664.00 65.00 296.00 65.00 296.00 25.00\n228.02 95.02 227.96 67.02 727.98 65.98 728.04 93.98\n297.08 68.08 296.84 28.08 663.92 25.92 664.16 65.92\n729.00 65.00 729.00 95.00 227.00 95.00 227.00 65.00\n84.00 40.00 84.00 55.00 64.00 55.00 64.00 40.00\n395.08 67.08 394.83 27.08 576.92 25.92 577.17 65.92\n394.05 95.05 393.89 67.06 576.95 65.95 577.11 93.94\n393.08 66.07 392.84 29.08 575.92 27.93 576.16 64.92\n577.00 67.00 577.00 95.00 395.00 95.00 395.00 67.00\n700.00 24.00 700.00 66.00 261.00 66.00 261.00 24.00\n331.06 95.06 330.87 68.07 628.94 65.94 629.13 92.93\n260.03 67.03 259.93 25.03 699.97 23.97 700.07 65.97\n105.00 30.00 105.00 69.00 54.00 69.00 54.00 30.00\n631.00 65.00 631.00 95.00 329.00 95.00 329.00 65.00\n630.00 65.00 630.00 95.00 329.00 95.00 329.00 65.00\n700.00 24.00 700.00 67.00 260.00 67.00 260.00 24.00\n136.00 15.00 136.00 77.00 42.00 77.00 42.00 15.00\n270.02 93.02 269.96 68.02 688.98 66.98 689.04 91.98\n666.00 27.00 666.00 66.00 294.00 66.00 294.00 27.00\n97.00 28.00 97.00 45.00 68.00 45.00 68.00 28.00\n271.02 93.02 270.96 68.02 688.98 66.98 689.04 91.98\n665.07 29.03 664.97 65.03 294.93 63.97 295.03 27.97\n345.03 65.05 344.88 28.03 613.97 26.95 614.12 63.97\n571.00 68.00 571.00 92.00 390.00 92.00 390.00 68.00\n507.00 29.00 507.00 63.00 238.00 63.00 238.00 29.00\n724.00 29.00 724.00 63.00 522.00 63.00 522.00 29.00\n685.00 68.00 685.00 92.00 272.00 92.00 272.00 68.00\n508.00 29.00 508.00 63.00 238.00 63.00 238.00 29.00\n725.14 28.07 724.93 65.07 517.86 63.93 518.07 26.93\n685.00 68.00 685.00 92.00 272.00 92.00 272.00 68.00\n685.00 68.00 685.00 92.00 272.00 92.00 272.00 68.00\n508.00 29.00 508.00 63.00 238.00 63.00 238.00 29.00\n725.00 28.00 725.00 65.00 518.00 65.00 518.00 28.00\n665.00 28.00 665.00 64.00 296.00 64.00 296.00 28.00\n650.06 66.03 649.97 96.00 308.94 94.97 309.03 64.97\n329.03 65.04 328.90 28.03 626.97 26.96 627.10 63.97\n624.00 67.00 624.00 94.00 335.00 94.00 335.00 67.00\n92.00 40.00 92.00 61.00 64.00 61.00 64.00 40.00\n624.00 67.00 624.00 94.00 335.00 94.00 335.00 67.00\n329.03 65.04 328.90 28.03 626.97 26.96 627.10 63.97\n93.00 37.00 93.00 62.00 61.00 62.00 61.00 37.00\n651.05 67.03 650.97 94.03 305.95 92.97 306.03 65.97\n218.06 68.06 217.89 26.06 741.94 23.94 742.11 65.94\n528.00 27.00 528.00 64.00 220.00 64.00 220.00 27.00\n652.00 65.00 652.00 95.00 305.00 95.00 305.00 65.00\n742.00 25.00 742.00 65.00 533.00 65.00 533.00 25.00\n347.07 67.10 346.76 30.07 611.93 27.90 612.24 64.93\n599.00 67.00 599.00 94.00 361.00 94.00 361.00 67.00\n501.00 27.00 501.00 70.00 457.00 70.00 457.00 27.00\n502.00 69.00 502.00 92.00 456.00 92.00 456.00 69.00\n228.03 67.03 227.94 25.03 732.97 23.97 733.06 65.97\n645.00 67.00 645.00 94.00 315.00 94.00 315.00 67.00\n861.00 38.00 861.00 56.00 835.00 56.00 835.00 38.00\n501.00 28.00 501.00 70.00 459.00 70.00 459.00 28.00\n502.00 69.00 502.00 92.00 456.00 92.00 456.00 69.00\n855.00 33.00 855.00 43.00 841.00 43.00 841.00 33.00\n237.03 69.03 236.93 24.03 719.97 22.97 720.07 67.97\n296.90 90.95 297.05 68.95 607.10 71.05 606.95 93.05\n661.00 74.00 661.00 89.00 605.00 89.00 605.00 74.00\n720.00 23.00 720.00 69.00 238.00 69.00 238.00 23.00\n604.16 71.08 603.92 95.08 295.84 91.92 296.08 67.92\n661.00 74.00 661.00 89.00 605.00 89.00 605.00 74.00\n330.05 67.09 329.79 30.06 630.95 27.91 631.21 64.94\n348.94 90.97 349.03 68.97 613.06 70.03 612.97 92.03\n330.05 65.08 329.81 31.05 627.95 28.92 628.19 62.95\n588.08 69.04 587.96 94.04 370.92 92.96 371.04 67.96\n639.00 30.00 639.00 63.00 321.00 63.00 321.00 30.00\n634.00 68.00 634.00 92.00 325.00 92.00 325.00 68.00\n319.08 66.08 318.84 30.08 638.92 27.92 639.16 63.92\n635.00 68.00 635.00 92.00 325.00 92.00 325.00 68.00\n284.04 66.04 283.93 26.04 672.96 24.96 673.07 64.96\n628.06 68.03 627.97 95.03 332.94 93.97 333.03 66.97\n402.08 65.13 401.68 31.09 671.92 27.87 672.32 61.91\n626.06 69.03 625.97 93.03 333.94 91.97 334.03 67.97\n399.00 27.00 399.00 65.00 285.00 65.00 285.00 27.00\n399.00 41.00 399.00 53.00 385.00 53.00 385.00 41.00\n668.00 68.00 668.00 92.00 291.00 92.00 291.00 68.00\n470.07 65.10 469.75 31.08 703.93 28.90 704.25 62.92\n456.00 30.00 456.00 64.00 252.00 64.00 252.00 30.00\n708.00 26.00 708.00 65.00 251.00 65.00 251.00 26.00\n668.00 68.00 668.00 92.00 291.00 92.00 291.00 68.00\n<\/code><\/pre>\n<p>\u6211\u4eec\u5728\u540c\u4e00\u5e73\u9762\u4e0a\uff0c\u628a\u77e9\u5f62\u753b\u51fa\u6765\uff0c\u770b\u8d77\u6765\u662f\u8fd9\u4e2a\u6837\u5b50\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2020\/09\/dataset.png\" alt=\"\" \/><\/p>\n<p>\u4ece\u4e0a\u56fe\u53ef\u4ee5\u6bd4\u8f83\u76f4\u89c2\u5730\u770b\u5230\uff0c\u7edd\u5927\u90e8\u5206\u77e9\u5f62\u90fd\u5206\u5e03\u5728\u9760\u4e2d\u95f4\u7684\u4f4d\u7f6e\uff0c\u91cd\u53e0\u5728\u4e00\u8d77\uff0c\u5de6\u8fb9\u548c\u53f3\u8fb9\u6709\u5c11\u6570\u51e0\u4e2a\u504f\u5f97\u6bd4\u8f83\u8fdc\u7684\u77e9\u5f62\u3002\u73b0\u5728\u5c31\u662f\u9700\u8981\u627e\u5230\u4e00\u4e2a\u5f02\u5e38\u503c\u5254\u9664\u7b97\u6cd5\uff0c\u5c06\u5de6\u53f3\u8fd9\u4e24\u7ec4\u5f02\u5e38\u7684\u77e9\u5f62\u627e\u51fa\u6765\u3002<\/p>\n<h2>DBSCAN \u7b97\u6cd5<\/h2>\n<p>\u8fd9\u4e2a\u95ee\u9898\u53ef\u4ee5\u91c7\u7528\u805a\u7c7b\u7b97\u6cd5\u6765\u89e3\u51b3\uff0c\u6700\u7b80\u5355\u7684\u805a\u7c7b\u50cf\u662f K-Means \u8fd9\u79cd\uff0c\u6211\u4eec\u5728\u805a\u7c7b\u7684\u540c\u65f6\uff0c\u9700\u8981\u627e\u5230\u6ca1\u529e\u6cd5\u805a\u5230\u4e00\u8d77\u7684\u5f02\u5e38\u503c\uff0cK-Means \u505a\u4e0d\u5230\u8fd9\u4e00\u70b9\uff0c\u800c\u57fa\u4e8e\u5bc6\u5ea6\u7684\u805a\u7c7b\u7b97\u6cd5 DBSCAN \u53ef\u4ee5\u5728\u805a\u597d\u7c7b\u7684\u540c\u65f6\uff0c\u5c06\u566a\u58f0\u503c\u4e5f\u8f93\u51fa\uff0c\u6070\u597d\u53ef\u4ee5\u89e3\u51b3\u6211\u4eec\u7684\u573a\u666f\u3002<\/p>\n<p>\u4e0b\u56fe\u662f\u4e0d\u540c\u805a\u7c7b\u7b97\u6cd5\u6548\u679c\u548c\u6027\u80fd\uff0c\u6765\u6e90\u81ea scikit-learn \u7f51\u7ad9\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2020\/09\/sphx_glr_plot_cluster_comparison_001.png\" alt=\"\" \/><\/p>\n<p>DBSCAN \u7b97\u6cd5\u6709\u4e24\u4e2a\u53c2\u6570\uff0ceps \u548c minPts\uff0c\u7b97\u6cd5\u6b65\u9aa4\u5927\u6982\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u8ba1\u7b97\u6570\u636e\u96c6\u4e2d\u6bcf\u4e24\u4e2a\u6570\u636e\u95f4\u7684\u8ddd\u79bb\uff0c\u5c06\u7b97\u597d\u7684\u503c\u4fdd\u5b58\u597d\u5f85\u67e5\u3002<\/li>\n<li>\u5bf9\u6570\u636e\u96c6\u4e2d\u7684\u6570\u636e\u904d\u5386\uff1a\n<ol>\n<li>\u627e\u5f53\u524d\u70b9 eps \u8ddd\u79bb\u8303\u56f4\u5185\u6709\u591a\u5c11\u4e2a\u6570\u636e\uff0c\u5982\u679c >= minPts \u4e2a\u6570\u636e\uff0c\u5219\u8ba4\u4e3a\u662f\u4e00\u4e2a\u805a\u7c7b\u3002\u5982\u679c &lt; minPts\uff0c\u5219\u628a\u70b9\u6807\u8bb0\u6210\u566a\u58f0\u70b9\u3002\u5c06\u5f53\u524d\u70b9\u6807\u8bb0\u4e3a\u8bbf\u95ee\u8fc7\u3002<\/li>\n<li>\u5bf9\u4e0a\u6b65\u7684\u5728 eps \u8303\u56f4\u5185\u7684\u70b9\uff0c\u518d\u904d\u5386\u4ed6\u4eec\uff0c\u8ba1\u7b97\u4ed6\u4eec eps \u8303\u56f4\u5185\u6709\u591a\u5c11\u4e2a\u6570\u636e\uff0c\u5982\u679c >= minPts \u4e2a\uff0c\u5219\u628a\u8fd9\u4e9b\u70b9\u4e5f\u52a0\u5165\u5230\u5f53\u524d\u805a\u7c7b\u4e2d\u3002\u5e76\u628a\u8ba1\u7b97\u8fc7\u7684\u70b9\uff0c\u6807\u8bb0\u4e3a\u8bbf\u95ee\u8fc7\u3002<\/li>\n<li>\u5982\u679c\u5f53\u524d\u70b9\u5df2\u7ecf\u88ab\u8bbf\u95ee\u8fc7\u4e86\uff0c\u5c31\u8df3\u8fc7\u3002<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>DBSCAN \u7b97\u6cd5\u7684\u4f2a\u4ee3\u7801\u53ef\u4ee5\u5199\u6210\uff1a<\/p>\n<pre><code class=\"language-text \">DBSCAN(D, eps, MinPts) {\n   C = 0\n   for each point P in dataset D {\n      if P is visited\n         continue next point\n      mark P as visited\n      NeighborPts = regionQuery(P, eps)\n      if sizeof(NeighborPts) &lt; MinPts\n         mark P as NOISE\n      else {\n         C = next cluster\n         expandCluster(P, NeighborPts, C, eps, MinPts)\n      }\n   }\n}\n\nexpandCluster(P, NeighborPts, C, eps, MinPts) {\n   add P to cluster C\n   for each point P' in NeighborPts { \n      if P' is not visited {\n         mark P' as visited\n         NeighborPts' = regionQuery(P', eps)\n         if sizeof(NeighborPts') &gt;= MinPts\n            NeighborPts = NeighborPts joined with NeighborPts'\n      }\n      if P' is not yet member of any cluster\n         add P' to cluster C\n   }\n}\n\nregionQuery(P, eps)\n   return all points within P's eps-neighborhood (including P)\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u6709\u4e00\u4e2a\u975e\u5e38\u68d2\u7684\u7b97\u6cd5\u539f\u7406\u6f14\u793a\uff1a<a href=\"https:\/\/www.naftaliharris.com\/blog\/visualizing-dbscan-clustering\/\">https:\/\/www.naftaliharris.com\/blog\/visualizing-dbscan-clustering\/<\/a><\/p>\n<p>\u8981\u5b9e\u73b0\u805a\u7c7b\u7b97\u6cd5\uff0c\u7b2c\u4e00\u6b65\u5c31\u662f\u8981\u786e\u5b9a\u6211\u4eec\u7684\u6570\u636e\u96c6\u4e2d\uff0c\u5982\u4f55\u8ba1\u7b97\u4e24\u4e2a\u6570\u636e\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002\u5982\u679c\u6570\u636e\u662f\u5355\u4e2a\u503c\uff0c\u5219\u8ddd\u79bb\u662f\u4e24\u4e2a\u503c\u76f8\u51cf\u7684\u7edd\u5bf9\u503c\uff0c\u5982\u679c\u6570\u636e\u662f\u4e8c\u7ef4\u70b9\uff0c\u5219\u8ddd\u79bb\u662f\u4e24\u70b9\u95f4\u7684\u6b27\u6c0f\u8ddd\u79bb\uff0c\u8fd9\u4e24\u79cd\u60c5\u51b5\u90fd\u6bd4\u8f83\u597d\u529e\u3002\u4f46\u95ee\u9898\u662f\u6211\u4eec\u7684\u6570\u636e\u96c6\u662f\u77e9\u5f62\uff0c\u6240\u4ee5\u7b2c\u4e00\u6b65\u6211\u4eec\u9700\u8981\u80fd\u591f\u8ba1\u7b97\u4e24\u4e2a\u77e9\u5f62\u4e4b\u95f4\u7684\u8ddd\u79bb\u503c\u3002<\/p>\n<h2>\u77e9\u5f62\u4e4b\u95f4\u8ddd\u79bb<\/h2>\n<p>\u6211\u4eec\u5b9a\u4e49\u4e24\u4e2a\u77e9\u5f62\u95f4\u7684\u8ddd\u79bb\u4e3a\uff1a<\/p>\n<ol>\n<li>\u5982\u679c\u4e24\u4e2a\u77e9\u5f62\u6709\u4ea4\u53c9\uff0c\u5219\u8ddd\u79bb\u4e3a 0\u3002<\/li>\n<li>\u77e9\u5f62 1 \u7684\u56db\u4e2a\u9876\u70b9\u5230\u77e9\u5f62 2 \u7684\u6700\u77ed\u8ddd\u79bb\u4e3a d1\uff0c\u53cd\u4e4b\uff0c\u77e9\u5f62 2 \u7684\u9876\u70b9\u5230\u77e9\u5f62 1 \u6700\u77ed\u4e3a d2\u3002<\/li>\n<li>d1 \u548c d2 \u4e24\u4e2a\u503c\uff0c\u6700\u5c0f\u7684\u5c31\u662f\u6211\u4eec\u8981\u7684\u77e9\u5f62\u95f4\u8ddd\u79bb\u503c\u3002<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2020\/09\/nLDmq.png\" alt=\"\" \/><\/p>\n<h3>\u77e9\u5f62\u4ea4\u53c9\u8ba1\u7b97<\/h3>\n<p>\u5bf9\u4e8e\u4e0a\u9762\u7684\u7b2c 1 \u6b65\uff0c\u53ef\u4ee5\u5206\u522b\u8ba1\u7b97\u4e24\u4e2a\u77e9\u5f62\u95f4\u7684 4 \u6761\u8fb9\uff0c\u662f\u5426\u6709\u4ea4\u53c9\uff0c\u5982\u679c\u6709\uff0c\u5219\u77e9\u5f62\u5c31\u6709\u4ea4\u53c9\u3002<\/p>\n<p>\u4e24\u6761\u7ebf\u6bb5\u4e4b\u95f4\u662f\u5426\u4ea4\u53c9\uff0c\u53ef\u4ee5\u76f4\u63a5\u5229\u7528\u6570\u5b66\u516c\u5f0f\u6765\u5224\u65ad\uff0c\u5bf9\u4e8e\u7ebf\u6bb5 (x1, y1) &#8211; (x2, y2)\uff0c\u548c\u7ebf\u6bb5 (x3, y3) &#8211; (x4, y4)\uff0c\u6309\u7167\u5982\u4e0b\u516c\u5f0f\u8ba1\u7b97\u503c t \u548c u\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2020\/09\/\u622a\u5c4f2020-09-0718.56.34.png\" alt=\"\" \/><\/p>\n<p>\u6ce8\u610f\u8ba1\u7b97\u7684\u65f6\u5019\uff0c\u9700\u8981\u5224\u65ad\u7279\u6b8a\u60c5\u51b5\uff0c\u5c31\u662f\u4e24\u7ebf\u6bb5\u5e73\u884c\u7684\u65f6\u5019\uff0c\u4e5f\u5c31\u662f\u4e0a\u9762\u516c\u5f0f\u4e2d\u5206\u6bcd\u4e3a 0 \u7684\u60c5\u51b5\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2020\/09\/\u622a\u5c4f2020-09-0718.57.16.png\" alt=\"\" \/><\/p>\n<p>\u5982\u679c\u8ba1\u7b97\u51fa\u6765\u7684 0.0 \u2264 t \u2264 1.0 \uff0c\u5e76\u4e14 0.0 \u2264 u \u2264 1.0 \u65f6\uff0c\u4e24\u7ebf\u6bb5\u76f8\u4ea4\uff0c\u5426\u5219\u4e0d\u76f8\u4ea4\u3002<\/p>\n<p>\u5224\u65ad\u4e24\u7ebf\u6bb5\u662f\u5426\u76f8\u4ea4\u7684 Go \u8bed\u8a00\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-go \">\/\/ IsLineSegmentIntersect \u5224\u65ad\u4e24\u6761\u7ebf\u6bb5\u662f\u5426\u76f8\u4ea4\uff0c\u7ebf\u6bb51 (p1, p2)\uff0c\u7ebf\u6bb52 \uff08p3, p4\uff09\nfunc IsLineSegmentIntersect(p1, p2, p3, p4 []float64) bool {\n    m := (p1[0]-p2[0])*(p3[1]-p4[1]) - (p1[1]-p2[1])*(p3[0]-p4[0])\n    if m == 0 {\n        return false\n    }\n    t := ((p1[0]-p3[0])*(p3[1]-p4[1]) - (p1[1]-p3[1])*(p3[0]-p4[0])) \/ m\n    if t &gt; 1 || t &lt; 0 {\n        return false\n    }\n    u := -((p1[0]-p2[0])*(p1[1]-p3[1]) - (p1[1]-p2[1])*(p1[0]-p3[0])) \/ m\n    if u &gt; 1 || u &lt; 0 {\n        return false\n    }\n    return true\n}\n<\/code><\/pre>\n<h3>\u70b9\u5230\u77e9\u5f62\u7684\u8ddd\u79bb<\/h3>\n<p>\u5bf9\u4e8e\u4e0a\u9762\u7684\u7b2c 2 \u6b65\uff0c\u9700\u8981\u8ba1\u7b97\u4e00\u4e2a\u70b9\u5230\u77e9\u5f62\u7684\u8ddd\u79bb\uff0c\u53ef\u4ee5\u5206\u522b\u5148\u8ba1\u7b97\u8fd9\u4e2a\u70b9\u5230\u77e9\u5f62 4 \u6761\u8fb9\u7684\u8ddd\u79bb\uff0c\u518d\u53d6\u6700\u5c0f\u503c\u3002<\/p>\n<p>\u8fd9\u53c8\u6d89\u53ca\u5230\u5982\u4f55\u8ba1\u7b97\u4e00\u4e2a\u70b9\u5230\u4e00\u6761\u7ebf\u6bb5\u95f4\u7684\u8ddd\u79bb\uff0c\u5206\u4e24\u79cd\u60c5\u51b5\u3002<\/p>\n<p>\u7b2c\u4e00\u79cd\u60c5\u51b5\u662f\uff0c\u70b9\u5230\u7ebf\u6bb5\u7684\u5782\u8db3\uff0c\u843d\u5728\u7ebf\u6bb5\u4e2d\u95f4\uff0c\u5982\u4e0b\u56fe\uff0c\u5782\u8db3\u70b9 D \u5728\u7ebf\u6bb5 AC \u4e0a\u9762\uff0c\u8fd9\u65f6\u8ddd\u79bb\u503c\u53d6 d = AD \u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2020\/09\/JFIeR.png\" alt=\"\" \/><\/p>\n<p>\u7b2c\u4e8c\u79cd\u60c5\u51b5\u662f\uff0c\u70b9\u5230\u7ebf\u6bb5\u7684\u5782\u8db3\uff0c\u843d\u5728\u4e86\u7ebf\u6bb5\u5916\u9762\uff0c\u5982\u4e0b\u56fe\uff0c\u5219\u8ddd\u79bb\u503c\u53d6 \ud835\udc51 = min(\ud835\udc34\ud835\udc35, \ud835\udc34\ud835\udc36)\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2020\/09\/FYtlF.png\" alt=\"\" \/><\/p>\n<p>\u6211\u5728\u7f51\u4e0a\u627e\u5230\u4e86\u4e00\u6bb5\u4ee3\u7801\uff0c\u7528\u4e8e\u8ba1\u7b97\u70b9\u5230\u7ebf\u6bb5\u7684\u8ddd\u79bb\uff0cc++ \u8bed\u8a00\u7248\u672c\u7684\uff1a<\/p>\n<pre><code class=\"language-cpp \">float minimum_distance(vec2 v, vec2 w, vec2 p) {\n  \/\/ Return minimum distance between line segment vw and point p\n  const float l2 = length_squared(v, w);  \/\/ i.e. |w-v|^2 -  avoid a sqrt\n  if (l2 == 0.0) return distance(p, v);   \/\/ v == w case\n  \/\/ Consider the line extending the segment, parameterized as v + t (w - v).\n  \/\/ We find projection of point p onto the line. \n  \/\/ It falls where t = [(p-v) . (w-v)] \/ |w-v|^2\n  \/\/ We clamp t from [0,1] to handle points outside the segment vw.\n  const float t = max(0, min(1, dot(p - v, w - v) \/ l2));\n  const vec2 projection = v + t * (w - v);  \/\/ Projection falls on the segment\n  return distance(p, projection);\n}\n<\/code><\/pre>\n<p>\u6211\u628a\u5b83\u6539\u9020\u6210 Go \u8bed\u8a00\u7248\u672c\uff1a<\/p>\n<pre><code class=\"language-go \">\/\/ dist2 \u8ba1\u7b97\u4e24\u70b9 v\u3001w \u7684\u8ddd\u79bb\u5e73\u65b9\uff0c\u4e0d\u505a sqrt \u64cd\u4f5c\nfunc dist2(v, w []float64) float64 {\n    return (v[0]-w[0])*(v[0]-w[0]) + (v[1]-w[1])*(v[1]-w[1])\n}\n\n\/\/ MinDistanceSqrBetweenPointAndLineSegment \u8ba1\u7b97\u70b9 p0\uff0c\u5230\u7ebf\u6bb5 lineP1-lineP2 \u7684\u6700\u77ed\u8ddd\u79bb\uff0c\u8fd4\u56de\u6ca1\u6709 sqrt \u7684\u503c\nfunc MinDistanceSqrBetweenPointAndLineSegment(p0 []float64, lineP1, lineP2 []float64) float64 {\n    l2 := dist2(lineP1, lineP2)\n    if l2 == 0 {\n        return dist2(p0, lineP1)\n    }\n    t := ((p0[0]-lineP1[0])*(lineP2[0]-lineP1[0]) + (p0[1]-lineP1[1])*(lineP2[1]-lineP1[1])) \/ l2\n    if t &gt;= 1 {\n        t = 1\n    }\n    if t &lt; 0 {\n        t = 0\n    }\n    px := []float64{lineP1[0] + t*(lineP2[0]-lineP1[0]), lineP1[1] + t*(lineP2[1]-lineP1[1])}\n    return dist2(p0, px)\n}\n<\/code><\/pre>\n<h3>\u77e9\u5f62\u8ddd\u79bb<\/h3>\n<p>\u6709\u4e86\u4e0a\u9762\u4ee3\u7801\uff0c\u518d\u8ba1\u7b97\u4e24\u77e9\u5f62\u95f4\u8ddd\u79bb\u5c31\u5bb9\u6613\u4e86\uff0c\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5bf9\u77e9\u5f62\u7684 4 \u6761\u8fb9\uff0c\u4e24\u4e24\u5224\u65ad\u662f\u5426\u76f8\u4ea4\uff0c\u5982\u679c\u4efb\u4f55\u8fb9\u6709\u76f8\u4ea4\u60c5\u51b5\uff0c\u5219\u77e9\u5f62\u8ddd\u79bb\u4e3a 0\uff0c\u76f4\u63a5\u8fd4\u56de\u3002<\/li>\n<li>\u8ba1\u7b97\u77e9\u5f62 1 \u7684\u56db\u4e2a\u9876\u70b9\uff0c\u5230\u77e9\u5f62 2 \u56db\u6761\u8fb9\u7684\u8ddd\u79bb\uff0c\u5f97\u5230 4&#215;4 = 16 \u4e2a\u503c\u3002<\/li>\n<li>\u8ba1\u7b97\u77e9\u5f62 2 \u7684\u56db\u4e2a\u9876\u70b9\uff0c\u5230\u77e9\u5f62 1 \u56db\u6761\u8fb9\u7684\u8ddd\u79bb\uff0c\u5f97\u5230 4&#215;4 = 16 \u4e2a\u503c\u3002<\/li>\n<li>\u4e0a\u9762\u4e24\u4e2a\u6b65\u9aa4\u52a0\u8d77\u6765 32 \u4e2a\u503c\u4e2d\uff0c\u53d6\u6700\u5c0f\u503c\u4e3a\u77e9\u5f62\u95f4\u8ddd\u79bb\uff0c\u8fd4\u56de\u3002<\/li>\n<\/ol>\n<p>\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-go \">import \"math\"\n\n\/\/ MinDistanceSqrBetweenRectangle \u8fd4\u56de\u4e24\u4e2a\u77e9\u5f62\u4e4b\u95f4\u7684\u6700\u5c0f\u8ddd\u79bb\u503c\uff0c\u8fd4\u56de\u6ca1\u6709 sqrt \u7684\u503c\n\/\/ \u77e9\u5f62\u4e2d\u7684\u70b9\u9700\u8981\u662f\u987a\u65f6\u9488\u6392\u5e8f\u597d\u7684\nfunc MinDistanceSqrBetweenRectangle(rect1, rect2 [][]float64) float64 {\n    if len(rect1) != 4 || len(rect2) != 4 {\n        return 0\n    }\n    \/\/ find out if any segment from the first rectangle intersects with any segment from the second rectangle.\n    for i := 1; i &lt; 4; i++ {\n        for j := 1; j &lt; 4; j++ {\n            interset := IsLineSegmentIntersect(rect1[i-1], rect1[i], rect2[j-1], rect2[j])\n            if interset {\n                return 0\n            }\n        }\n    }\n    \/\/ For every corner point from the first rectangle find distances to all segments from the second rectangle\n    minDistance := math.MaxFloat64\n    for i := range rect1 {\n        for j := 1; j &lt; 4; j++ {\n            dist := MinDistanceSqrBetweenPointAndLineSegment(rect1[i], rect2[j-1], rect2[j])\n            if dist &lt; minDistance {\n                minDistance = dist\n            }\n        }\n    }\n    for i := range rect2 {\n        for j := 1; j &lt; 4; j++ {\n            dist := MinDistanceSqrBetweenPointAndLineSegment(rect2[i], rect1[j-1], rect1[j])\n            if dist &lt; minDistance {\n                minDistance = dist\n            }\n        }\n    }\n    return minDistance\n}\n<\/code><\/pre>\n<h2>\u4ee3\u7801\u5b9e\u73b0 DBSCAN<\/h2>\n<p>\u6700\u540e\u6211\u4eec\u6765\u901a\u8fc7 Go \u8bed\u8a00\u5b9e\u73b0 DBSCAN \u7b97\u6cd5\uff0c\u9996\u5148\u5b9a\u4e49\u7b97\u6cd5\u53c2\u6570\u7ed3\u6784\uff1a<\/p>\n<pre><code class=\"language-go \">\/\/ DBSCANParameters describes the parameters of the density-based\n\/\/ clustering algorithm DBSCAN\ntype DBSCANParameters struct {\n    \/\/ Eps represents the \"reachability\", or the maximum\n    \/\/ distance any point can be before being considered for\n    \/\/ inclusion.\n    Eps float64\n\n    \/\/ MinCount represents how many points need to be\n    \/\/ in a cluster before it is considered one.\n    MinCount int\n}\n<\/code><\/pre>\n<p>\u5bf9\u4e8e\u77e9\u5f62\u6570\u7ec4\uff0c\u4e8b\u5148\u8ba1\u7b97\u597d\u6bcf\u4e24\u4e2a\u77e9\u5f62\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u4ee5\u4f9b\u540e\u9762\u5f85\u67e5\uff1a<\/p>\n<pre><code class=\"language-go \">import (\n    \"gonum.org\/v1\/gonum\/mat\"\n)\n\nfunc computePairwiseDistances(rectArray [][][]float64) *mat.Dense {\n    \/\/ Compute pair-wise distances\n    \/\/ Do an n^2 computation of all pairwise distances\n    rows := len(rectArray)\n    dist := mat.NewDense(rows, rows, nil)\n    for i := 0; i &lt; rows; i++ {\n        for j := i + 1; j &lt; rows; j++ {\n            d := MinDistanceSqrBetweenRectangle(rectArray[i], rectArray[j])\n            dist.Set(i, j, d)\n            dist.Set(j, i, d)\n        }\n    }\n    return dist\n}\n<\/code><\/pre>\n<p>\u5b9e\u73b0\u7b97\u6cd5\u7684 regionQuery \u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-go \">\/\/ RegionQuery is simple way O(N) to find points in neighbourhood\n\/\/ It is roughly equivalent to kdTree.InRange(points[i], eps, nil)\nfunc regionQuery(dist *mat.Dense, p int, eps float64) []int {\n    result := []int{}\n    rows, _ := dist.Dims()\n    \/\/ Return any points within the Eps neighbourhood\n    for i := 0; i &lt; rows; i++ {\n        if dist.At(p, i) &lt;= eps {\n            result = append(result, i) \/\/ Mark as neighbour\n        }\n    }\n    return result\n}\n<\/code><\/pre>\n<p>\u6700\u540e DBSCAN \u4e3b\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-go \">import (\n    \"math\/big\"\n)\n\nfunc DBSCAN(rectArray [][][]float64, params DBSCANParameters) (clusters map[int][]int, noise []int) {\n    rows := len(rectArray)\n    visited := make([]bool, rows)\n    members := make([]bool, rows)\n    clusters = make(map[int][]int)\n    noise = []int{}\n    c := 0\n\n    dist := computePairwiseDistances(rectArray)\n\n    for i := 0; i &lt; rows; i++ {\n        if visited[i] {\n            continue\n        }\n        visited[i] = true\n\n        neighborPts := regionQuery(dist, i, params.Eps)\n        if len(neighborPts) &lt; params.MinCount {\n            noise = append(noise, i)\n        } else {\n            clusters = []int{i}\n            members[i] = true\n\n            \/\/ expandCluster goes here inline\n            neighborUnique := big.NewInt(0)\n            for j := 0; j &lt; len(neighborPts); j++ {\n                neighborUnique.SetBit(neighborUnique, neighborPts[j], 1)\n            }\n\n            for j := 0; j &lt; len(neighborPts); j++ {\n                k := neighborPts[j]\n                if !visited[k] {\n                    visited[k] = true\n                    moreNeighbors := regionQuery(dist, k, params.Eps)\n                    if len(moreNeighbors) &gt;= params.MinCount {\n                        for _, p := range moreNeighbors {\n                            if neighborUnique.Bit(p) == 0 {\n                                neighborPts = append(neighborPts, p)\n                                neighborUnique.SetBit(neighborUnique, p, 1)\n                            }\n                        }\n                    }\n                    if !members[k] {\n                        clusters = append(clusters, k)\n                        members[k] = true\n                    }\n                }\n            }\n\n            c++\n        }\n    }\n    return\n}\n<\/code><\/pre>\n<h2>\u7b97\u6cd5\u6548\u679c<\/h2>\n<p>\u5bf9\u4e8e\u6587\u7ae0\u5f00\u5934\u7684\u6d4b\u8bd5\u6570\u636e\uff0c\u901a\u8fc7 DBSCAN \u7b97\u6cd5\u540e\uff0c\u8fd4\u56de\u566a\u58f0\u70b9\u6570\u636e\uff0c\u6211\u4eec\u5c06\u5176\u7528\u4e0d\u540c\u989c\u8272\u753b\u51fa\u6765\uff0c\u5982\u4e0b\u56fe\uff0c\u6548\u679c\u8fd8\u662f\u6bd4\u8f83\u660e\u663e\u7684\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/kyle.ai\/blog\/wp-content\/uploads\/2020\/09\/group.png\" alt=\"\" \/><\/p>\n<p>\u8fd9\u91cc\u6211\u9009\u62e9\u7684\u53c2\u6570 eps = 5 \uff0cminPts = 8\u3002\u53c2\u6570\u503c\u7684\u9009\u62e9\uff0c\u53ef\u4ee5\u6839\u636e\u6211\u4eec\u6570\u636e\u5206\u5e03\u60c5\u51b5\u7075\u6d3b\u9009\u62e9\uff0c\u53c2\u6570\u9009\u62e9\u4e0d\u597d\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6700\u7ec8\u6548\u679c\u3002<\/p>\n<h2>\u53c2\u8003\u8d44\u6599\uff1a<\/h2>\n<ol>\n<li>https:\/\/math.stackexchange.com\/questions\/3232521\/shortest-distance-between-two-rectangles-in-2d<\/li>\n<li>https:\/\/en.wikipedia.org\/wiki\/DBSCAN<\/li>\n<li>https:\/\/en.wikipedia.org\/wiki\/Line%E2%80%93line_intersection<\/li>\n<li>https:\/\/stackoverflow.com\/questions\/849211\/shortest-distance-between-a-point-and-a-line-segment<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\u63cf\u8ff0 \u5047\u5982\u73b0\u5728\u6709\u4e00\u7ec4\u77e9\u5f62\u6846\uff0c\u5df2\u77e5\u6bcf\u4e2a\u77e9\u5f62\u56db\u4e2a\u9876\u70b9\u7684\u5750\u6807\uff0c\u8fd9\u4e9b\u77e9\u5f62\u6709\u4e9b\u662f\u6328\u4e00\u8d77\u7684\uff0c\u6709\u4e9b\u79bb\u5f97\u6bd4\u8f83\u8fdc\uff0c\u73b0\u5728\u9700\u8981\u89e3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-7568","post","type-post","status-publish","format-standard","hentry","category-diary"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/7568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=7568"}],"version-history":[{"count":3,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/7568\/revisions"}],"predecessor-version":[{"id":7579,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/7568\/revisions\/7579"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=7568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=7568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=7568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}