{"id":1136861,"date":"2025-01-08T21:41:01","date_gmt":"2025-01-08T13:41:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1136861.html"},"modified":"2025-01-08T21:41:04","modified_gmt":"2025-01-08T13:41:04","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97%e4%ba%8c%e8%bf%9b%e5%88%b6%e5%ba%8f%e5%88%97%e7%9b%b8%e5%85%b3%e6%80%a7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1136861.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u76f8\u5173\u6027"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100838\/da7e6f1f-225e-4110-b570-1eba622fbba4.webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u76f8\u5173\u6027\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u76f8\u5173\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u7684\u76f8\u5173\u6027\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5176\u4e2d\u5305\u62ec<strong>\u4f7f\u7528NumPy\u3001\u5229\u7528SciPy\u5e93\u3001\u8ba1\u7b97\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570<\/strong>\u7b49\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u5e94\u7528\u573a\u666f\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528NumPy\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u76f8\u5173\u6027<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u3002\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u51fd\u6570\u6765\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\uff0c\u5176\u4e2d\u5305\u62ec\u8ba1\u7b97\u76f8\u5173\u6027\u7684\u51fd\u6570\u3002\u5728\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u76f8\u5173\u6027\u65f6\uff0cNumPy\u7684<code>corrcoef<\/code>\u51fd\u6570\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u76f8\u5173\u6027<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u6765\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u7684\u76f8\u5173\u6027\u662f\u4e00\u4e2a\u7b80\u5355\u800c\u6709\u6548\u7684\u65b9\u6cd5\u3002NumPy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a<code>corrcoef<\/code>\u7684\u51fd\u6570\uff0c\u5b83\u80fd\u591f\u8ba1\u7b97\u4e24\u4e2a\u5e8f\u5217\u4e4b\u95f4\u7684\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\u3002\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\u662f\u8861\u91cf\u4e24\u4e2a\u53d8\u91cf\u7ebf\u6027\u76f8\u5173\u6027\u7684\u7edf\u8ba1\u91cf\uff0c\u503c\u57df\u5728-1\u52301\u4e4b\u95f4\u3002\u503c\u4e3a1\u8868\u793a\u5b8c\u5168\u6b63\u76f8\u5173\uff0c\u503c\u4e3a-1\u8868\u793a\u5b8c\u5168\u8d1f\u76f8\u5173\uff0c\u503c\u4e3a0\u8868\u793a\u6ca1\u6709\u7ebf\u6027\u76f8\u5173\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165NumPy<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165NumPy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8ba1\u7b97\u76f8\u5173\u6027<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u4e8c\u8fdb\u5236\u5e8f\u5217<code>seq1<\/code>\u548c<code>seq2<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>corrcoef<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u5b83\u4eec\u7684\u76f8\u5173\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e8c\u8fdb\u5236\u5e8f\u5217<\/p>\n<p>seq1 = np.array([1, 0, 1, 1, 0, 1, 0, 1])<\/p>\n<p>seq2 = np.array([0, 1, 0, 1, 1, 0, 1, 0])<\/p>\n<h2><strong>\u8ba1\u7b97\u76f8\u5173\u6027\u77e9\u9635<\/strong><\/h2>\n<p>correlation_matrix = np.corrcoef(seq1, seq2)<\/p>\n<h2><strong>\u63d0\u53d6\u76f8\u5173\u6027\u503c<\/strong><\/h2>\n<p>correlation = correlation_matrix[0, 1]<\/p>\n<p>print(f&quot;\u76f8\u5173\u6027: {correlation}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e24\u4e2a\u4e8c\u8fdb\u5236\u5e8f\u5217<code>seq1<\/code>\u548c<code>seq2<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>np.corrcoef<\/code>\u51fd\u6570\u8ba1\u7b97\u8fd9\u4e24\u4e2a\u5e8f\u5217\u7684\u76f8\u5173\u6027\u77e9\u9635\u3002\u76f8\u5173\u6027\u77e9\u9635\u662f\u4e00\u4e2a2&#215;2\u7684\u77e9\u9635\uff0c\u5176\u4e2d\u5bf9\u89d2\u7ebf\u4e0a\u7684\u503c\u4e3a1\uff0c\u975e\u5bf9\u89d2\u7ebf\u4e0a\u7684\u503c\u4e3a\u4e24\u4e2a\u5e8f\u5217\u4e4b\u95f4\u7684\u76f8\u5173\u6027\u3002\u6700\u540e\uff0c\u6211\u4eec\u63d0\u53d6\u4e86\u76f8\u5173\u6027\u503c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528SciPy\u5e93\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u76f8\u5173\u6027<\/h3>\n<\/p>\n<p><p>SciPy\u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u3002SciPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7684\u79d1\u5b66\u8ba1\u7b97\u51fd\u6570\uff0c\u5305\u62ec\u8ba1\u7b97\u76f8\u5173\u6027\u7684\u51fd\u6570\u3002\u5728\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u76f8\u5173\u6027\u65f6\uff0cSciPy\u7684<code>pearsonr<\/code>\u51fd\u6570\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u548c\u5bfc\u5165SciPy<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86SciPy\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scipy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165SciPy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.stats import pearsonr<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8ba1\u7b97\u76f8\u5173\u6027<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u4e8c\u8fdb\u5236\u5e8f\u5217<code>seq1<\/code>\u548c<code>seq2<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528SciPy\u7684<code>pearsonr<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u5b83\u4eec\u7684\u76f8\u5173\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e8c\u8fdb\u5236\u5e8f\u5217<\/p>\n<p>seq1 = [1, 0, 1, 1, 0, 1, 0, 1]<\/p>\n<p>seq2 = [0, 1, 0, 1, 1, 0, 1, 0]<\/p>\n<h2><strong>\u8ba1\u7b97\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\u548cp\u503c<\/strong><\/h2>\n<p>correlation, p_value = pearsonr(seq1, seq2)<\/p>\n<p>print(f&quot;\u76f8\u5173\u6027: {correlation}&quot;)<\/p>\n<p>print(f&quot;p\u503c: {p_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e24\u4e2a\u4e8c\u8fdb\u5236\u5e8f\u5217<code>seq1<\/code>\u548c<code>seq2<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>pearsonr<\/code>\u51fd\u6570\u8ba1\u7b97\u8fd9\u4e24\u4e2a\u5e8f\u5217\u7684\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\u548cp\u503c\u3002\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\u8868\u793a\u4e24\u4e2a\u5e8f\u5217\u4e4b\u95f4\u7684\u7ebf\u6027\u76f8\u5173\u6027\uff0c\u800cp\u503c\u8868\u793a\u76f8\u5173\u6027\u7684\u663e\u8457\u6027\u6c34\u5e73\u3002\u6700\u540e\uff0c\u6211\u4eec\u6253\u5370\u4e86\u76f8\u5173\u6027\u503c\u548cp\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8ba1\u7b97\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570<\/h3>\n<\/p>\n<p><p>\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\u662f\u8861\u91cf\u4e24\u4e2a\u53d8\u91cf\u7ebf\u6027\u76f8\u5173\u6027\u7684\u7edf\u8ba1\u91cf\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u7edf\u8ba1\u5b66\u4e2d\u3002\u5b83\u7684\u503c\u57df\u5728-1\u52301\u4e4b\u95f4\uff0c\u503c\u4e3a1\u8868\u793a\u5b8c\u5168\u6b63\u76f8\u5173\uff0c\u503c\u4e3a-1\u8868\u793a\u5b8c\u5168\u8d1f\u76f8\u5173\uff0c\u503c\u4e3a0\u8868\u793a\u6ca1\u6709\u7ebf\u6027\u76f8\u5173\u6027\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u6216SciPy\u5e93\u6765\u8ba1\u7b97\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528NumPy\u8ba1\u7b97\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>corrcoef<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e8c\u8fdb\u5236\u5e8f\u5217<\/strong><\/h2>\n<p>seq1 = np.array([1, 0, 1, 1, 0, 1, 0, 1])<\/p>\n<p>seq2 = np.array([0, 1, 0, 1, 1, 0, 1, 0])<\/p>\n<h2><strong>\u8ba1\u7b97\u76f8\u5173\u6027\u77e9\u9635<\/strong><\/h2>\n<p>correlation_matrix = np.corrcoef(seq1, seq2)<\/p>\n<h2><strong>\u63d0\u53d6\u76f8\u5173\u6027\u503c<\/strong><\/h2>\n<p>correlation = correlation_matrix[0, 1]<\/p>\n<p>print(f&quot;\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570: {correlation}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e24\u4e2a\u4e8c\u8fdb\u5236\u5e8f\u5217<code>seq1<\/code>\u548c<code>seq2<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>np.corrcoef<\/code>\u51fd\u6570\u8ba1\u7b97\u5b83\u4eec\u7684\u76f8\u5173\u6027\u77e9\u9635\uff0c\u5e76\u63d0\u53d6\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528SciPy\u8ba1\u7b97\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528SciPy\u7684<code>pearsonr<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.stats import pearsonr<\/p>\n<h2><strong>\u5b9a\u4e49\u4e8c\u8fdb\u5236\u5e8f\u5217<\/strong><\/h2>\n<p>seq1 = [1, 0, 1, 1, 0, 1, 0, 1]<\/p>\n<p>seq2 = [0, 1, 0, 1, 1, 0, 1, 0]<\/p>\n<h2><strong>\u8ba1\u7b97\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\u548cp\u503c<\/strong><\/h2>\n<p>correlation, p_value = pearsonr(seq1, seq2)<\/p>\n<p>print(f&quot;\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570: {correlation}&quot;)<\/p>\n<p>print(f&quot;p\u503c: {p_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e24\u4e2a\u4e8c\u8fdb\u5236\u5e8f\u5217<code>seq1<\/code>\u548c<code>seq2<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>pearsonr<\/code>\u51fd\u6570\u8ba1\u7b97\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\u548cp\u503c\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u76f8\u5173\u6027\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528NumPy\u548cSciPy\u5e93\u8ba1\u7b97\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u7684\u76f8\u5173\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u8ba1\u7b97Jaccard\u76f8\u4f3c\u7cfb\u6570<\/h4>\n<\/p>\n<p><p>Jaccard\u76f8\u4f3c\u7cfb\u6570\u662f\u8861\u91cf\u4e24\u4e2a\u96c6\u5408\u76f8\u4f3c\u5ea6\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5e7f\u6cdb\u7528\u4e8e\u4e8c\u8fdb\u5236\u5e8f\u5217\u7684\u76f8\u5173\u6027\u5206\u6790\u3002\u5176\u503c\u57df\u57280\u52301\u4e4b\u95f4\uff0c\u503c\u4e3a1\u8868\u793a\u4e24\u4e2a\u96c6\u5408\u5b8c\u5168\u76f8\u540c\uff0c\u503c\u4e3a0\u8868\u793a\u4e24\u4e2a\u96c6\u5408\u5b8c\u5168\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def jaccard_similarity(seq1, seq2):<\/p>\n<p>    intersection = np.logical_and(seq1, seq2)<\/p>\n<p>    union = np.logical_or(seq1, seq2)<\/p>\n<p>    return intersection.sum() \/ float(union.sum())<\/p>\n<h2><strong>\u5b9a\u4e49\u4e8c\u8fdb\u5236\u5e8f\u5217<\/strong><\/h2>\n<p>seq1 = np.array([1, 0, 1, 1, 0, 1, 0, 1])<\/p>\n<p>seq2 = np.array([0, 1, 0, 1, 1, 0, 1, 0])<\/p>\n<h2><strong>\u8ba1\u7b97Jaccard\u76f8\u4f3c\u7cfb\u6570<\/strong><\/h2>\n<p>similarity = jaccard_similarity(seq1, seq2)<\/p>\n<p>print(f&quot;Jaccard\u76f8\u4f3c\u7cfb\u6570: {similarity}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>jaccard_similarity<\/code>\u6765\u8ba1\u7b97Jaccard\u76f8\u4f3c\u7cfb\u6570\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u8ba1\u7b97\u4e24\u4e2a\u4e8c\u8fdb\u5236\u5e8f\u5217\u7684\u76f8\u4f3c\u6027\uff0c\u5e76\u6253\u5370\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8ba1\u7b97\u6c49\u660e\u8ddd\u79bb<\/h4>\n<\/p>\n<p><p>\u6c49\u660e\u8ddd\u79bb\u662f\u8861\u91cf\u4e24\u4e2a\u7b49\u957f\u5b57\u7b26\u4e32\u4e4b\u95f4\u5dee\u5f02\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u5e7f\u6cdb\u7528\u4e8e\u4e8c\u8fdb\u5236\u5e8f\u5217\u7684\u76f8\u5173\u6027\u5206\u6790\u3002\u5176\u503c\u8868\u793a\u4e24\u4e2a\u5b57\u7b26\u4e32\u4e4b\u95f4\u4e0d\u540c\u5b57\u7b26\u7684\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def hamming_distance(seq1, seq2):<\/p>\n<p>    return np.sum(seq1 != seq2)<\/p>\n<h2><strong>\u5b9a\u4e49\u4e8c\u8fdb\u5236\u5e8f\u5217<\/strong><\/h2>\n<p>seq1 = np.array([1, 0, 1, 1, 0, 1, 0, 1])<\/p>\n<p>seq2 = np.array([0, 1, 0, 1, 1, 0, 1, 0])<\/p>\n<h2><strong>\u8ba1\u7b97\u6c49\u660e\u8ddd\u79bb<\/strong><\/h2>\n<p>distance = hamming_distance(seq1, seq2)<\/p>\n<p>print(f&quot;\u6c49\u660e\u8ddd\u79bb: {distance}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>hamming_distance<\/code>\u6765\u8ba1\u7b97\u6c49\u660e\u8ddd\u79bb\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u8ba1\u7b97\u4e24\u4e2a\u4e8c\u8fdb\u5236\u5e8f\u5217\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u5e76\u6253\u5370\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u4e86\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u7684\u76f8\u5173\u6027\u3002\u6211\u4eec\u4ecb\u7ecd\u4e86\u4f7f\u7528NumPy\u548cSciPy\u5e93\u8ba1\u7b97\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\u7684\u65b9\u6cd5\uff0c\u5e76\u5c55\u793a\u4e86\u5982\u4f55\u8ba1\u7b97Jaccard\u76f8\u4f3c\u7cfb\u6570\u548c\u6c49\u660e\u8ddd\u79bb\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u793a\u4f8b\u548c\u89e3\u91ca\uff0c\u4f60\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\u6765\u5206\u6790\u4e8c\u8fdb\u5236\u5e8f\u5217\u7684\u76f8\u5173\u6027\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u79d1\u5b66\u7814\u7a76\u3001\u6570\u636e\u5206\u6790\u8fd8\u662f<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\uff0c<strong>\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u7684\u76f8\u5173\u6027\u90fd\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u4efb\u52a1<\/strong>\u3002\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u548c\u6280\u672f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5904\u7406\u548c\u5206\u6790\u6570\u636e\uff0c\u4ece\u800c\u5f97\u51fa\u66f4\u6709\u610f\u4e49\u7684\u7ed3\u8bba\u548c\u89c1\u89e3\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u548c\u5904\u7406\u4e8c\u8fdb\u5236\u5e8f\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u3002\u53ef\u4ee5\u901a\u8fc7<code>file.read()<\/code>\u65b9\u6cd5\u83b7\u53d6\u6570\u636e\uff0c\u5e76\u4f7f\u7528<code>bytearray<\/code>\u6216<code>bytes<\/code>\u5bf9\u8c61\u6765\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u3002\u5bf9\u4e8e\u4e8c\u8fdb\u5236\u5e8f\u5217\u7684\u64cd\u4f5c\uff0cNumPy\u5e93\u4e5f\u975e\u5e38\u6709\u7528\uff0c\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\uff0c\u9002\u5408\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u76f8\u5173\u6027\u9700\u8981\u54ea\u4e9b\u5e93\u6216\u5de5\u5177\uff1f<\/strong><br \/>\u4e3a\u4e86\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u7684\u76f8\u5173\u6027\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528NumPy\u548cSciPy\u5e93\u3002NumPy\u80fd\u591f\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\uff0c\u800cSciPy\u63d0\u4f9b\u4e86\u76f8\u5173\u6027\u8ba1\u7b97\u7684\u51fd\u6570\uff0c\u5982<code>scipy.stats.pearsonr<\/code>\uff0c\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u76ae\u5c14\u900a\u76f8\u5173\u7cfb\u6570\uff0c\u5e2e\u52a9\u7406\u89e3\u5e8f\u5217\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\u3002<\/p>\n<p><strong>\u5982\u4f55\u53ef\u89c6\u5316\u4e8c\u8fdb\u5236\u5e8f\u5217\u4e4b\u95f4\u7684\u76f8\u5173\u6027\uff1f<\/strong><br \/>\u53ef\u89c6\u5316\u662f\u7406\u89e3\u6570\u636e\u5173\u7cfb\u7684\u91cd\u8981\u5de5\u5177\u3002\u53ef\u4ee5\u4f7f\u7528Matplotlib\u6216Seaborn\u5e93\u6765\u7ed8\u5236\u76f8\u5173\u77e9\u9635\u70ed\u56fe\u6216\u6563\u70b9\u56fe\uff0c\u4ece\u800c\u76f4\u89c2\u5c55\u793a\u4e8c\u8fdb\u5236\u5e8f\u5217\u4e4b\u95f4\u7684\u76f8\u5173\u6027\u3002\u901a\u8fc7\u5c06\u76f8\u5173\u6027\u6570\u503c\u4e0e\u53ef\u89c6\u5316\u7ed3\u5408\uff0c\u80fd\u591f\u66f4\u597d\u5730\u8bc6\u522b\u6a21\u5f0f\u548c\u8d8b\u52bf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u76f8\u5173\u6027 \u5728Python\u4e2d\uff0c\u8ba1\u7b97\u4e8c\u8fdb\u5236\u5e8f\u5217\u7684\u76f8\u5173\u6027\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1136873,"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\/1136861"}],"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=1136861"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136861\/revisions"}],"predecessor-version":[{"id":1136877,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136861\/revisions\/1136877"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1136873"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1136861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1136861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1136861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}