{"id":1004157,"date":"2024-12-27T10:23:46","date_gmt":"2024-12-27T02:23:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1004157.html"},"modified":"2024-12-27T10:23:52","modified_gmt":"2024-12-27T02:23:52","slug":"python%e5%a6%82%e4%bd%95%e6%89%93%e6%ac%a7%e5%bc%8f%e8%b7%9d%e7%a6%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1004157.html","title":{"rendered":"python\u5982\u4f55\u6253\u6b27\u5f0f\u8ddd\u79bb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081214\/4c3be473-6e32-4d9f-85a0-40b547779dd5.webp\" alt=\"python\u5982\u4f55\u6253\u6b27\u5f0f\u8ddd\u79bb\" \/><\/p>\n<p><p> <strong>Python\u8ba1\u7b97\u6b27\u5f0f\u8ddd\u79bb\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u3001NumPy\u5e93\u3001SciPy\u5e93\u7b49\u3002\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\uff0cNumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u8ba1\u7b97\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u5927\u89c4\u6a21\u6570\u636e\u7684\u8ddd\u79bb\u8ba1\u7b97\u3002<\/strong>\u4e0b\u9762\u5c06\u5bf9\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p>NumPy\u5e93\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u6570\u5b66\u51fd\u6570\u548c\u5de5\u5177\u3002\u4f7f\u7528NumPy\u8ba1\u7b97\u6b27\u5f0f\u8ddd\u79bb\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u4f7f\u7528\u5176\u5185\u7f6e\u7684<code>numpy.linalg.norm<\/code>\u51fd\u6570\u5373\u53ef\u3002\u8be5\u51fd\u6570\u53ef\u4ee5\u8ba1\u7b97\u5411\u91cf\u7684\u8303\u6570\uff0c\u5176\u4e2d\u9ed8\u8ba4\u8ba1\u7b97\u7684\u662f\u6b27\u5f0f\u8ddd\u79bb\uff08\u5373L2\u8303\u6570\uff09\u3002\u5177\u4f53\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u70b9<\/strong><\/h2>\n<p>point1 = np.array([1, 2, 3])<\/p>\n<p>point2 = np.array([4, 5, 6])<\/p>\n<h2><strong>\u8ba1\u7b97\u6b27\u5f0f\u8ddd\u79bb<\/strong><\/h2>\n<p>distance = np.linalg.norm(point1 - point2)<\/p>\n<p>print(distance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86NumPy\u5e93\uff0c\u5e76\u5b9a\u4e49\u4e86\u4e24\u4e2a\u70b9\u7684\u5750\u6807\u3002\u7136\u540e\uff0c\u901a\u8fc7<code>np.linalg.norm<\/code>\u51fd\u6570\u8ba1\u7b97\u8fd9\u4e24\u4e2a\u70b9\u4e4b\u95f4\u7684\u6b27\u5f0f\u8ddd\u79bb\u3002\u6b64\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u6548\u7387\u66f4\u9ad8\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u6b27\u5f0f\u8ddd\u79bb\u7684\u57fa\u672c\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u6b27\u5f0f\u8ddd\u79bb\u662f\u4e00\u79cd\u8ba1\u7b97\u4e24\u70b9\u4e4b\u95f4\u76f4\u7ebf\u8ddd\u79bb\u7684\u5ea6\u91cf\u65b9\u6cd5\uff0c\u662f\u6700\u5e38\u7528\u7684\u8ddd\u79bb\u8ba1\u7b97\u65b9\u6cd5\u4e4b\u4e00\u3002\u5176\u516c\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ d = \\sqrt{\\sum_{i=1}^{n} (x_i &#8211; y_i)^2} ]<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c(x_i)\u548c(y_i)\u5206\u522b\u662f\u4e24\u4e2a\u70b9\u5728\u7b2c(i)\u7ef4\u7684\u5750\u6807\u3002\u6b27\u5f0f\u8ddd\u79bb\u9002\u7528\u4e8e\u5404\u79cd\u5e94\u7528\u573a\u666f\uff0c\u5982<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u3001\u6570\u636e\u6316\u6398\u3001\u56fe\u50cf\u5904\u7406\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u6b27\u5f0f\u8ddd\u79bb\u5728\u4e8c\u7ef4\u7a7a\u95f4\u4e2d\u8868\u73b0\u4e3a\u76f4\u7ebf\u8ddd\u79bb\uff0c\u800c\u5728\u9ad8\u7ef4\u7a7a\u95f4\u4e2d\u5219\u662f\u4e24\u70b9\u4e4b\u95f4\u7684\u6700\u77ed\u8def\u5f84\u3002\u7531\u4e8e\u5176\u7b80\u5355\u76f4\u89c2\u7684\u5b9a\u4e49\uff0c\u6b27\u5f0f\u8ddd\u79bb\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u9886\u57df\uff0c\u5982\u805a\u7c7b\u5206\u6790\u3001\u5206\u7c7b\u5668\u7684\u6784\u5efa\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Python\u8ba1\u7b97\u6b27\u5f0f\u8ddd\u79bb\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u8ba1\u7b97\u6b27\u5f0f\u8ddd\u79bb\u7684\u65b9\u6cd5\uff0c\u4ee5\u4e0b\u5c06\u5206\u522b\u4ecb\u7ecd\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u8fd9\u662f\u6700\u57fa\u7840\u7684\u65b9\u6cd5\uff0c\u76f4\u63a5\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u8ba1\u7b97\u4e24\u4e2a\u70b9\u4e4b\u95f4\u7684\u6b27\u5f0f\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def euclidean_distance(point1, point2):<\/p>\n<p>    return math.sqrt(sum((x - y)  2 for x, y in zip(point1, point2)))<\/p>\n<p>point1 = (1, 2, 3)<\/p>\n<p>point2 = (4, 5, 6)<\/p>\n<p>distance = euclidean_distance(point1, point2)<\/p>\n<p>print(distance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>euclidean_distance<\/code>\uff0c\u5b83\u63a5\u6536\u4e24\u4e2a\u70b9\u7684\u5750\u6807\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u4e4b\u95f4\u7684\u6b27\u5f0f\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528NumPy\u5e93<\/h4>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u8ba1\u7b97\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>point1 = np.array([1, 2, 3])<\/p>\n<p>point2 = np.array([4, 5, 6])<\/p>\n<p>distance = np.linalg.norm(point1 - point2)<\/p>\n<p>print(distance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u7684<code>linalg.norm<\/code>\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u8ba1\u7b97\u6b27\u5f0f\u8ddd\u79bb\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u9ad8\u6548\u6027\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528SciPy\u5e93<\/h4>\n<\/p>\n<p><p>SciPy\u662f\u4e00\u4e2a\u57fa\u4e8eNumPy\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u9ad8\u7ea7\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.spatial import distance<\/p>\n<p>point1 = (1, 2, 3)<\/p>\n<p>point2 = (4, 5, 6)<\/p>\n<p>distance = distance.euclidean(point1, point2)<\/p>\n<p>print(distance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>SciPy\u7684<code>spatial.distance<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8ba1\u7b97\u6b27\u5f0f\u8ddd\u79bb\u7684\u51fd\u6570<code>euclidean<\/code>\uff0c\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u6b27\u5f0f\u8ddd\u79bb\u5728\u8bb8\u591a\u5e94\u7528\u573a\u666f\u4e2d\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u4e3b\u8981\u7684\u5e94\u7528\u9886\u57df\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u673a\u5668\u5b66\u4e60<\/h4>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u6b27\u5f0f\u8ddd\u79bb\u5e38\u7528\u4e8e\u805a\u7c7b\u5206\u6790\u548c\u5206\u7c7b\u4efb\u52a1\u4e2d\u3002\u4f8b\u5982\uff0cK-means\u805a\u7c7b\u7b97\u6cd5\u4f7f\u7528\u6b27\u5f0f\u8ddd\u79bb\u6765\u8861\u91cf\u6837\u672c\u4e0e\u7c07\u4e2d\u5fc3\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u4ece\u800c\u5c06\u6837\u672c\u5f52\u7c7b\u5230\u6700\u8fd1\u7684\u7c07\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u6b27\u5f0f\u8ddd\u79bb\u5728KNN\uff08K-Nearest Neighbors\uff09\u7b97\u6cd5\u4e2d\u4e5f\u8d77\u5230\u5173\u952e\u4f5c\u7528\u3002KNN\u662f\u4e00\u79cd\u57fa\u4e8e\u5b9e\u4f8b\u7684\u5b66\u4e60\u7b97\u6cd5\uff0c\u901a\u8fc7\u8ba1\u7b97\u6d4b\u8bd5\u6837\u672c\u4e0e\u8bad\u7ec3\u6837\u672c\u4e4b\u95f4\u7684\u6b27\u5f0f\u8ddd\u79bb\uff0c\u627e\u5230\u8ddd\u79bb\u6700\u8fd1\u7684K\u4e2a\u90bb\u5c45\uff0c\u4ee5\u6b64\u6765\u8fdb\u884c\u5206\u7c7b\u6216\u56de\u5f52\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u6b27\u5f0f\u8ddd\u79bb\u5e38\u7528\u4e8e\u56fe\u50cf\u5206\u5272\u548c\u5bf9\u8c61\u68c0\u6d4b\u4e2d\u3002\u901a\u8fc7\u8ba1\u7b97\u50cf\u7d20\u4e4b\u95f4\u7684\u6b27\u5f0f\u8ddd\u79bb\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8bc6\u522b\u548c\u5206\u5272\u4e0d\u540c\u7684\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><p>\u56fe\u50cf\u7684\u989c\u8272\u8ddd\u79bb\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6b27\u5f0f\u8ddd\u79bb\u6765\u8ba1\u7b97\u3002\u4f8b\u5982\uff0c\u5728RGB\u8272\u5f69\u7a7a\u95f4\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u4e24\u4e2a\u989c\u8272\u5411\u91cf\u4e4b\u95f4\u7684\u6b27\u5f0f\u8ddd\u79bb\u6765\u8861\u91cf\u5b83\u4eec\u7684\u76f8\u4f3c\u6027\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u6570\u636e\u6316\u6398<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u6316\u6398\u4e2d\uff0c\u6b27\u5f0f\u8ddd\u79bb\u5e38\u7528\u4e8e\u76f8\u4f3c\u5ea6\u5206\u6790\u548c\u5f02\u5e38\u68c0\u6d4b\u3002\u901a\u8fc7\u8ba1\u7b97\u6837\u672c\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u53ef\u4ee5\u8bc6\u522b\u51fa\u4e0e\u5927\u591a\u6570\u6837\u672c\u5dee\u5f02\u8f83\u5927\u7684\u5f02\u5e38\u6837\u672c\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5e02\u573a\u7bee\u5206\u6790\u4e2d\uff0c\u6b27\u5f0f\u8ddd\u79bb\u4e5f\u53ef\u7528\u4e8e\u8bc6\u522b\u8d2d\u4e70\u884c\u4e3a\u76f8\u4f3c\u7684\u5ba2\u6237\u7fa4\u4f53\uff0c\u4ece\u800c\u8fdb\u884c\u7cbe\u51c6\u8425\u9500\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6b27\u5f0f\u8ddd\u79bb\u7684\u4f18\u7f3a\u70b9<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u6b27\u5f0f\u8ddd\u79bb\u5728\u8bb8\u591a\u9886\u57df\u5f97\u5230\u4e86\u5e7f\u6cdb\u5e94\u7528\uff0c\u4f46\u5b83\u4e5f\u5b58\u5728\u4e00\u4e9b\u5c40\u9650\u6027\u3002\u4e86\u89e3\u8fd9\u4e9b\u4f18\u7f3a\u70b9\u6709\u52a9\u4e8e\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u66f4\u597d\u5730\u9009\u62e9\u5408\u9002\u7684\u8ddd\u79bb\u5ea6\u91cf\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f18\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u76f4\u89c2\u7b80\u5355<\/strong>\uff1a\u6b27\u5f0f\u8ddd\u79bb\u7684\u6982\u5ff5\u7b80\u5355\u6613\u61c2\uff0c\u8ba1\u7b97\u4e5f\u8f83\u4e3a\u76f4\u63a5\u3002<\/li>\n<li><strong>\u5e94\u7528\u5e7f\u6cdb<\/strong>\uff1a\u9002\u7528\u4e8e\u5404\u79cd\u7c7b\u578b\u7684\u6570\u636e\u548c\u573a\u666f\uff0c\u5c24\u5176\u662f\u8fde\u7eed\u578b\u6570\u636e\u3002<\/li>\n<li><strong>\u8ba1\u7b97\u6548\u7387\u9ad8<\/strong>\uff1a\u5728\u4f4e\u7ef4\u7a7a\u95f4\u4e2d\uff0c\u6b27\u5f0f\u8ddd\u79bb\u7684\u8ba1\u7b97\u6548\u7387\u8f83\u9ad8\u3002<\/li>\n<\/ul>\n<p><h4>2\u3001\u7f3a\u70b9<\/h4>\n<\/p>\n<ul>\n<li><strong>\u7ef4\u5ea6\u707e\u96be<\/strong>\uff1a\u5728\u9ad8\u7ef4\u7a7a\u95f4\u4e2d\uff0c\u6b27\u5f0f\u8ddd\u79bb\u53ef\u80fd\u5931\u53bb\u5176\u6709\u6548\u6027\uff0c\u56e0\u4e3a\u6240\u6709\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u8d8b\u4e8e\u76f8\u4f3c\uff0c\u8fd9\u88ab\u79f0\u4e3a\u201c\u7ef4\u5ea6\u707e\u96be\u201d\u3002<\/li>\n<li><strong>\u5bf9\u5c3a\u5ea6\u654f\u611f<\/strong>\uff1a\u6b27\u5f0f\u8ddd\u79bb\u5bf9\u6570\u636e\u7684\u5c3a\u5ea6\u975e\u5e38\u654f\u611f\uff0c\u53d8\u91cf\u7684\u91cf\u7eb2\u4e0d\u540c\u53ef\u80fd\u5bfc\u81f4\u8ddd\u79bb\u8ba1\u7b97\u7ed3\u679c\u7684\u4e0d\u51c6\u786e\u3002<\/li>\n<li><strong>\u4e0d\u9002\u7528\u4e8e\u79bb\u6563\u6570\u636e<\/strong>\uff1a\u5bf9\u4e8e\u79bb\u6563\u6570\u636e\u6216\u975e\u6570\u503c\u6570\u636e\uff0c\u6b27\u5f0f\u8ddd\u79bb\u53ef\u80fd\u4e0d\u9002\u7528\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u6539\u8fdb\u4e0e\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u514b\u670d\u6b27\u5f0f\u8ddd\u79bb\u7684\u5c40\u9650\u6027\uff0c\u7814\u7a76\u4eba\u5458\u63d0\u51fa\u4e86\u4e00\u4e9b\u6539\u8fdb\u548c\u6269\u5c55\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u6539\u8fdb\u63aa\u65bd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6807\u51c6\u5316\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97\u6b27\u5f0f\u8ddd\u79bb\u4e4b\u524d\uff0c\u5bf9\u6570\u636e\u8fdb\u884c\u6807\u51c6\u5316\u53ef\u4ee5\u7f13\u89e3\u5c3a\u5ea6\u654f\u611f\u6027\u7684\u95ee\u9898\u3002\u6807\u51c6\u5316\u53ef\u4ee5\u901a\u8fc7\u51cf\u53bb\u5747\u503c\u5e76\u9664\u4ee5\u6807\u51c6\u5dee\u6765\u5b9e\u73b0\uff0c\u4ece\u800c\u4f7f\u6bcf\u4e2a\u53d8\u91cf\u7684\u5747\u503c\u4e3a0\uff0c\u6807\u51c6\u5dee\u4e3a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import StandardScaler<\/p>\n<p>scaler = StandardScaler()<\/p>\n<p>data_standardized = scaler.fit_transform(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u52a0\u6743\u6b27\u5f0f\u8ddd\u79bb<\/h4>\n<\/p>\n<p><p>\u52a0\u6743\u6b27\u5f0f\u8ddd\u79bb\u4e3a\u6bcf\u4e2a\u5750\u6807\u5206\u914d\u4e0d\u540c\u7684\u6743\u91cd\uff0c\u4ece\u800c\u589e\u5f3a\u6216\u51cf\u5f31\u67d0\u4e9b\u7ef4\u5ea6\u7684\u5f71\u54cd\u3002\u6743\u91cd\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7684\u91cd\u8981\u6027\u6216\u5148\u9a8c\u77e5\u8bc6\u6765\u786e\u5b9a\u3002<\/p>\n<\/p>\n<p><p>[ d_w = \\sqrt{\\sum_{i=1}^{n} w_i \\cdot (x_i &#8211; y_i)^2} ]<\/p>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528\u5176\u4ed6\u8ddd\u79bb\u5ea6\u91cf<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u5176\u4ed6\u8ddd\u79bb\u5ea6\u91cf\u53ef\u80fd\u66f4\u52a0\u5408\u9002\u3002\u4f8b\u5982\uff0c\u66fc\u54c8\u987f\u8ddd\u79bb\uff08L1\u8ddd\u79bb\uff09\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u6bd4\u6b27\u5f0f\u8ddd\u79bb\u66f4\u80fd\u53cd\u6620\u6570\u636e\u7684\u7279\u5f81\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u8df5\u6848\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u6b27\u5f0f\u8ddd\u79bb\u7684\u5e94\u7528\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u5b9e\u8df5\u6848\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6848\u4f8b\u80cc\u666f<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5ba2\u6237\u8d2d\u4e70\u6570\u636e\u96c6\uff0c\u5176\u4e2d\u5305\u542b\u6bcf\u4e2a\u5ba2\u6237\u5728\u4e0d\u540c\u7c7b\u522b\u5546\u54c1\u4e0a\u7684\u6d88\u8d39\u91d1\u989d\u3002\u6211\u4eec\u7684\u76ee\u6807\u662f\u8bc6\u522b\u8d2d\u4e70\u884c\u4e3a\u76f8\u4f3c\u7684\u5ba2\u6237\u7fa4\u4f53\uff0c\u4ee5\u4fbf\u8fdb\u884c\u7cbe\u51c6\u8425\u9500\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u51c6\u5907<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u6570\u636e\u5e76\u8fdb\u884c\u6807\u51c6\u5316\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from sklearn.preprocessing import StandardScaler<\/p>\n<h2><strong>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u5ba2\u6237\u8d2d\u4e70\u6570\u636e\u7684DataFrame<\/strong><\/h2>\n<p>data = pd.DataFrame({<\/p>\n<p>    &#39;\u5ba2\u6237ID&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>    &#39;\u98df\u54c1&#39;: [200, 150, 300, 250, 100],<\/p>\n<p>    &#39;\u670d\u88c5&#39;: [100, 200, 150, 100, 250],<\/p>\n<p>    &#39;\u7535\u5b50\u4ea7\u54c1&#39;: [300, 400, 200, 100, 300]<\/p>\n<p>})<\/p>\n<h2><strong>\u53bb\u6389&#39;\u5ba2\u6237ID&#39;\u5217\uff0c\u5e76\u5bf9\u6570\u636e\u8fdb\u884c\u6807\u51c6\u5316<\/strong><\/h2>\n<p>scaler = StandardScaler()<\/p>\n<p>data_standardized = scaler.fit_transform(data.drop(&#39;\u5ba2\u6237ID&#39;, axis=1))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8ba1\u7b97\u5ba2\u6237\u4e4b\u95f4\u7684\u6b27\u5f0f\u8ddd\u79bb<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528SciPy\u5e93\u8ba1\u7b97\u5ba2\u6237\u4e4b\u95f4\u7684\u6b27\u5f0f\u8ddd\u79bb\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.spatial.distance import pdist, squareform<\/p>\n<p>distance_matrix = squareform(pdist(data_standardized, metric=&#39;euclidean&#39;))<\/p>\n<p>print(distance_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u8bc6\u522b\u76f8\u4f3c\u5ba2\u6237\u7fa4\u4f53<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5206\u6790\u8ddd\u79bb\u77e9\u9635\uff0c\u6211\u4eec\u53ef\u4ee5\u8bc6\u522b\u51fa\u8d2d\u4e70\u884c\u4e3a\u76f8\u4f3c\u7684\u5ba2\u6237\u7fa4\u4f53\u3002\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f7f\u7528\u805a\u7c7b\u7b97\u6cd5\uff08\u5982K-means\uff09\u6765\u5bf9\u5ba2\u6237\u8fdb\u884c\u5206\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.cluster import KMeans<\/p>\n<p>kmeans = KMeans(n_clusters=2)<\/p>\n<p>clusters = kmeans.fit_predict(data_standardized)<\/p>\n<p>data[&#39;\u7fa4\u7ec4&#39;] = clusters<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u6b27\u5f0f\u8ddd\u79bb\u4f5c\u4e3a\u4e00\u79cd\u57fa\u7840\u7684\u8ddd\u79bb\u5ea6\u91cf\u65b9\u6cd5\uff0c\u5728\u8bb8\u591a\u9886\u57df\u4e2d\u5f97\u5230\u4e86\u5e7f\u6cdb\u5e94\u7528\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b97\u6b27\u5f0f\u8ddd\u79bb\u4ee5\u53ca\u5176\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u4f18\u52bf\u548c\u5c40\u9650\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u8ddd\u79bb\u5ea6\u91cf\u65b9\u6cd5\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u6570\u636e\u7279\u5f81\u548c\u5e94\u7528\u573a\u666f\u6765\u51b3\u5b9a\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6570\u5b66\u516c\u5f0f\u8ba1\u7b97\uff0c\u8fd8\u662f\u4f7f\u7528NumPy\u3001SciPy\u7b49\u5e93\uff0c\u90fd\u80fd\u591f\u9ad8\u6548\u5730\u5b8c\u6210\u6b27\u5f0f\u8ddd\u79bb\u7684\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u6b27\u5f0f\u8ddd\u79bb\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u6b27\u5f0f\u8ddd\u79bb\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u6700\u5e38\u7528\u7684\u662f\u5229\u7528NumPy\u5e93\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>numpy.linalg.norm<\/code>\u51fd\u6570\u76f4\u63a5\u8ba1\u7b97\u4e24\u4e2a\u70b9\u4e4b\u95f4\u7684\u6b27\u5f0f\u8ddd\u79bb\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u4e24\u4e2a\u70b9A(x1, y1)\u548cB(x2, y2)\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nA = np.array([x1, y1])\nB = np.array([x2, y2])\ndistance = np.linalg.norm(A - B)\nprint(distance)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u6d01\u9ad8\u6548\uff0c\u9002\u5408\u5904\u7406\u9ad8\u7ef4\u6570\u636e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u6709\u6ca1\u6709\u73b0\u6210\u7684\u5e93\u53ef\u4ee5\u8ba1\u7b97\u6b27\u5f0f\u8ddd\u79bb\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u7684<code>scipy<\/code>\u5e93\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u51fd\u6570\u6765\u8ba1\u7b97\u6b27\u5f0f\u8ddd\u79bb\u3002\u4f7f\u7528<code>scipy.spatial.distance<\/code>\u6a21\u5757\u4e2d\u7684<code>euclidean<\/code>\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u8ba1\u7b97\u4e24\u4e2a\u70b9\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from scipy.spatial.distance import euclidean\n\npoint1 = [x1, y1]\npoint2 = [x2, y2]\ndistance = euclidean(point1, point2)\nprint(distance)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u548c\u673a\u5668\u5b66\u4e60\u5e94\u7528\u3002<\/p>\n<p><strong>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u6b27\u5f0f\u8ddd\u79bb\u6709\u4ec0\u4e48\u7528\u5904\uff1f<\/strong><br \/>\u6b27\u5f0f\u8ddd\u79bb\u5728\u673a\u5668\u5b66\u4e60\u4e2d\u5e94\u7528\u5e7f\u6cdb\uff0c\u5c24\u5176\u662f\u5728\u805a\u7c7b\u548c\u5206\u7c7b\u7b97\u6cd5\u4e2d\u3002\u4f8b\u5982\uff0c\u5728K\u5747\u503c\u805a\u7c7b\u7b97\u6cd5\u4e2d\uff0c\u7b97\u6cd5\u901a\u8fc7\u8ba1\u7b97\u6837\u672c\u70b9\u4e4b\u95f4\u7684\u6b27\u5f0f\u8ddd\u79bb\u6765\u786e\u5b9a\u6837\u672c\u7684\u805a\u7c7b\u5f52\u5c5e\u3002\u6b64\u5916\uff0c\u5728K\u6700\u8fd1\u90bb\uff08KNN\uff09\u7b97\u6cd5\u4e2d\uff0c\u6b27\u5f0f\u8ddd\u79bb\u7528\u4e8e\u627e\u51fa\u6700\u8fd1\u7684K\u4e2a\u90bb\u5c45\uff0c\u4ece\u800c\u505a\u51fa\u5206\u7c7b\u51b3\u7b56\u3002\u4f7f\u7528\u6b27\u5f0f\u8ddd\u79bb\u80fd\u591f\u6709\u6548\u5730\u8bc4\u4f30\u6837\u672c\u4e4b\u95f4\u7684\u76f8\u4f3c\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8ba1\u7b97\u6b27\u5f0f\u8ddd\u79bb\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u3001NumPy\u5e93\u3001SciPy\u5e93\u7b49\u3002\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u9002\u5408\u7684\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1004173,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004157"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1004157"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004157\/revisions"}],"predecessor-version":[{"id":1004174,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004157\/revisions\/1004174"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1004173"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1004157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1004157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1004157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}