{"id":981449,"date":"2024-12-27T07:02:42","date_gmt":"2024-12-26T23:02:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/981449.html"},"modified":"2024-12-27T07:02:44","modified_gmt":"2024-12-26T23:02:44","slug":"python%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0apriori%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/981449.html","title":{"rendered":"Python\u5982\u4f55\u6dfb\u52a0APRIORI\u7b97\u6cd5"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210401\/9bd2cb29-2e52-4251-ac68-7e72888da018.webp\" alt=\"Python\u5982\u4f55\u6dfb\u52a0APRIORI\u7b97\u6cd5\" \/><\/p>\n<p><p> <strong>Python\u6dfb\u52a0APRIORI\u7b97\u6cd5\u7684\u8fc7\u7a0b\u5305\u62ec\uff1a\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3001\u52a0\u8f7d\u6570\u636e\u3001\u5e94\u7528APRIORI\u7b97\u6cd5\u3001\u89e3\u6790\u7ed3\u679c\u3002<\/strong>\u5728\u8fd9\u4e9b\u6b65\u9aa4\u4e2d\uff0c\u6700\u4e3a\u5173\u952e\u7684\u662f\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u5b9e\u73b0APRIORI\u7b97\u6cd5\uff0c\u5e76\u901a\u8fc7\u9002\u5f53\u7684\u53c2\u6570\u8bbe\u7f6e\u6765\u63d0\u53d6\u6709\u4ef7\u503c\u7684\u5173\u8054\u89c4\u5219\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5404\u4e2a\u6b65\u9aa4\u53ca\u5176\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5e94\u7528APRIORI\u7b97\u6cd5\uff0c\u6700\u5e38\u7528\u7684\u5e93\u662f<code>mlxtend<\/code>\u3002\u8be5\u5e93\u4e0d\u4ec5\u6613\u4e8e\u4f7f\u7528\uff0c\u8fd8\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5173\u8054\u89c4\u5219\u6316\u6398\u529f\u80fd\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5<code>mlxtend<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mlxtend<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u5b89\u88c5<code>pandas<\/code>\u548c<code>numpy<\/code>\u5e93\u6765\u5904\u7406\u6570\u636e\u548c\u6267\u884c\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u52a0\u8f7d\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528APRIORI\u7b97\u6cd5\u524d\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u597d\u6570\u636e\u96c6\u3002\u901a\u5e38\uff0c\u6570\u636e\u96c6\u4ee5\u4ea4\u6613\u8bb0\u5f55\u7684\u5f62\u5f0f\u5b58\u5728\uff0c\u5176\u4e2d\u6bcf\u4e00\u884c\u4ee3\u8868\u4e00\u6b21\u4ea4\u6613\uff0c\u5217\u8868\u793a\u4ea4\u6613\u4e2d\u7684\u5546\u54c1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e\u96c6<\/strong><\/h2>\n<p>data = {&#39;Transaction&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>        &#39;Items&#39;: [[&#39;Milk&#39;, &#39;Bread&#39;], [&#39;Milk&#39;, &#39;Diaper&#39;, &#39;Beer&#39;, &#39;Eggs&#39;],<\/p>\n<p>                  [&#39;Milk&#39;, &#39;Bread&#39;, &#39;Diaper&#39;, &#39;Beer&#39;], [&#39;Bread&#39;, &#39;Milk&#39;, &#39;Diaper&#39;, &#39;Beer&#39;],<\/p>\n<p>                  [&#39;Bread&#39;, &#39;Diaper&#39;, &#39;Milk&#39;, &#39;Eggs&#39;]]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u96c6\u53ef\u80fd\u6765\u81eaCSV\u6587\u4ef6\u6216\u6570\u636e\u5e93\u3002\u4f7f\u7528<code>pandas<\/code>\u7684<code>read_csv<\/code>\u6216\u6570\u636e\u5e93\u8fde\u63a5\u529f\u80fd\u53ef\u4ee5\u65b9\u4fbf\u5730\u52a0\u8f7d\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5e94\u7528APRIORI\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u51c6\u5907\u597d\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5e94\u7528APRIORI\u7b97\u6cd5\u3002<code>mlxtend<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\u6765\u5b9e\u73b0\u8fd9\u4e00\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mlxtend.preprocessing import TransactionEncoder<\/p>\n<p>from mlxtend.frequent_patterns import apriori, association_rules<\/p>\n<h2><strong>\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5408\u9002\u7684\u683c\u5f0f<\/strong><\/h2>\n<p>te = TransactionEncoder()<\/p>\n<p>te_ary = te.fit(df[&#39;Items&#39;]).transform(df[&#39;Items&#39;])<\/p>\n<p>df_trans = pd.DataFrame(te_ary, columns=te.columns_)<\/p>\n<h2><strong>\u4f7f\u7528APRIORI\u7b97\u6cd5<\/strong><\/h2>\n<p>frequent_itemsets = apriori(df_trans, min_support=0.6, use_colnames=True)<\/p>\n<h2><strong>\u63d0\u53d6\u5173\u8054\u89c4\u5219<\/strong><\/h2>\n<p>rules = association_rules(frequent_itemsets, metric=&quot;confidence&quot;, min_threshold=0.7)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u4ea4\u6613\u6570\u636e\u8f6c\u6362\u4e3a\u5e03\u5c14\u77e9\u9635\u683c\u5f0f\uff0c\u7136\u540e\u4f7f\u7528<code>apriori<\/code>\u51fd\u6570\u63d0\u53d6\u9891\u7e41\u9879\u96c6\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>association_rules<\/code>\u51fd\u6570\u4ece\u9891\u7e41\u9879\u96c6\u4e2d\u63d0\u53d6\u5173\u8054\u89c4\u5219\u3002<strong>\u5173\u952e\u53c2\u6570\u5982<code>min_support<\/code>\u548c<code>min_threshold<\/code>\u9700\u8981\u6839\u636e\u5177\u4f53\u5e94\u7528\u573a\u666f\u8fdb\u884c\u8c03\u6574<\/strong>\uff0c\u4ee5\u4fbf\u63d0\u53d6\u51fa\u6709\u610f\u4e49\u7684\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u89e3\u6790\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u6700\u540e\u4e00\u6b65\u662f\u5bf9\u751f\u6210\u7684\u9891\u7e41\u9879\u96c6\u548c\u5173\u8054\u89c4\u5219\u8fdb\u884c\u89e3\u6790\u548c\u5206\u6790\u3002\u6211\u4eec\u53ef\u4ee5\u5bf9\u89c4\u5219\u8fdb\u884c\u7b5b\u9009\uff0c\u4ee5\u627e\u5230\u6700\u6709\u5546\u4e1a\u4ef7\u503c\u7684\u6d1e\u5bdf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u770b\u9891\u7e41\u9879\u96c6<\/p>\n<p>print(frequent_itemsets)<\/p>\n<h2><strong>\u67e5\u770b\u5173\u8054\u89c4\u5219<\/strong><\/h2>\n<p>print(rules)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u89c4\u5219\u8fdb\u884c\u8fdb\u4e00\u6b65\u7b5b\u9009<\/strong>\uff0c\u4f8b\u5982\u6839\u636e\u63d0\u5347\u5ea6\uff08lift\uff09\u3001\u7f6e\u4fe1\u5ea6\uff08confidence\uff09\u6216\u5176\u4ed6\u81ea\u5b9a\u4e49\u6307\u6807\u6765\u8bc4\u4f30\u89c4\u5219\u7684\u91cd\u8981\u6027\u3002\u6b64\u5916\uff0c\u7ed3\u5408\u4e1a\u52a1\u80cc\u666f\u7406\u89e3\u8fd9\u4e9b\u89c4\u5219\u662f\u81f3\u5173\u91cd\u8981\u7684\uff0c\u56e0\u4e3a\u5173\u8054\u89c4\u5219\u6316\u6398\u7684\u7ed3\u679c\u53ef\u80fd\u5e76\u4e0d\u603b\u662f\u7b26\u5408\u76f4\u89c9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f18\u5316\u548c\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cAPRIORI\u7b97\u6cd5\u53ef\u80fd\u4f1a\u9762\u4e34\u6027\u80fd\u74f6\u9888\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u65f6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316\u548c\u6269\u5c55\u7684\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8c03\u6574\u7b97\u6cd5\u53c2\u6570<\/strong>\uff1a\u901a\u8fc7\u51cf\u5c11<code>min_support<\/code>\u548c<code>min_confidence<\/code>\uff0c\u53ef\u4ee5\u51cf\u5c11\u8ba1\u7b97\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u9884\u5904\u7406<\/strong>\uff1a\u5728\u5e94\u7528APRIORI\u4e4b\u524d\uff0c\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u4f8b\u5982\u53bb\u9664\u4f4e\u9891\u5546\u54c1\u6216\u5408\u5e76\u76f8\u4f3c\u5546\u54c1\uff0c\u4ee5\u51cf\u5c11\u6570\u636e\u7684\u590d\u6742\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e76\u884c\u8ba1\u7b97<\/strong>\uff1a\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\uff0c\u8003\u8651\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u6280\u672f\u6765\u63d0\u9ad8\u7b97\u6cd5\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed3\u5408\u5176\u4ed6\u7b97\u6cd5<\/strong>\uff1a\u53ef\u4ee5\u8003\u8651\u5c06APRIORI\u4e0e\u5176\u4ed6\u6570\u636e\u6316\u6398\u7b97\u6cd5\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u83b7\u5f97\u66f4\u4e30\u5bcc\u7684\u6d1e\u5bdf\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7ed3\u5408\u805a\u7c7b\u7b97\u6cd5\u6765\u53d1\u73b0\u4e0d\u540c\u5ba2\u6237\u7fa4\u4f53\u7684\u8d2d\u4e70\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u4f18\u5316\u7b56\u7565\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u6709\u6548\u5730\u5e94\u7528APRIORI\u7b97\u6cd5\u8fdb\u884c\u5173\u8054\u89c4\u5219\u6316\u6398\uff0c\u4ece\u800c\u4e3a\u5546\u4e1a\u51b3\u7b56\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u5b9e\u73b0APRIORI\u7b97\u6cd5\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5b9e\u73b0APRIORI\u7b97\u6cd5\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>mlxtend<\/code>\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u51fd\u6570\u6765\u6267\u884c\u5173\u8054\u89c4\u5219\u6316\u6398\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86\u8be5\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install mlxtend<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u60a8\u9700\u8981\u51c6\u5907\u60a8\u7684\u6570\u636e\u96c6\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u9002\u5408\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\u4f7f\u7528one-hot\u7f16\u7801\uff0c\u7136\u540e\u8c03\u7528<code>apriori<\/code>\u51fd\u6570\u6765\u63d0\u53d6\u9891\u7e41\u9879\u96c6\uff0c\u6700\u540e\u53ef\u4ee5\u4f7f\u7528<code>association_rules<\/code>\u51fd\u6570\u6765\u751f\u6210\u5173\u8054\u89c4\u5219\u3002<\/p>\n<p><strong>APRIORI\u7b97\u6cd5\u6709\u54ea\u4e9b\u5b9e\u9645\u5e94\u7528\uff1f<\/strong><br \/>APRIORI\u7b97\u6cd5\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5e02\u573a\u7bee\u5206\u6790\u3001\u63a8\u8350\u7cfb\u7edf\u548c\u7528\u6237\u884c\u4e3a\u5206\u6790\u7b49\u9886\u57df\u3002\u5728\u96f6\u552e\u884c\u4e1a\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5546\u5bb6\u8bc6\u522b\u54ea\u4e9b\u4ea7\u54c1\u7ecf\u5e38\u4e00\u8d77\u8d2d\u4e70\uff0c\u4ece\u800c\u4f18\u5316\u4ea7\u54c1\u6446\u653e\u548c\u8425\u9500\u7b56\u7565\u3002\u6b64\u5916\uff0c\u5728\u793e\u4ea4\u5a92\u4f53\u5206\u6790\u4e2d\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u53d1\u73b0\u7528\u6237\u4e4b\u95f4\u7684\u5171\u540c\u5174\u8da3\uff0c\u8fdb\u800c\u63d0\u4f9b\u4e2a\u6027\u5316\u5185\u5bb9\u63a8\u8350\u3002<\/p>\n<p><strong>\u5982\u4f55\u9009\u62e9APRIORI\u7b97\u6cd5\u4e2d\u7684\u652f\u6301\u5ea6\u548c\u7f6e\u4fe1\u5ea6\u9608\u503c\uff1f<\/strong><br \/>\u9009\u62e9\u9002\u5f53\u7684\u652f\u6301\u5ea6\u548c\u7f6e\u4fe1\u5ea6\u9608\u503c\u662f\u4f7f\u7528APRIORI\u7b97\u6cd5\u7684\u5173\u952e\u3002\u652f\u6301\u5ea6\u51b3\u5b9a\u4e86\u9879\u96c6\u5728\u6570\u636e\u96c6\u4e2d\u51fa\u73b0\u7684\u9891\u7387\uff0c\u901a\u5e38\u9700\u8981\u6839\u636e\u6570\u636e\u91cf\u548c\u4e1a\u52a1\u9700\u6c42\u8fdb\u884c\u8c03\u6574\u3002\u7f6e\u4fe1\u5ea6\u5219\u8861\u91cf\u4e86\u89c4\u5219\u7684\u53ef\u9760\u6027\uff0c\u5efa\u8bae\u9009\u62e9\u4e00\u4e2a\u8f83\u9ad8\u7684\u9608\u503c\uff08\u59820.7\u62160.8\uff09\u6765\u786e\u4fdd\u751f\u6210\u7684\u89c4\u5219\u5177\u6709\u8f83\u9ad8\u7684\u53ef\u4fe1\u5ea6\u3002\u53ef\u4ee5\u901a\u8fc7\u5b9e\u9a8c\u548c\u4ea4\u53c9\u9a8c\u8bc1\u7684\u65b9\u6cd5\u6765\u4f18\u5316\u8fd9\u4e9b\u53c2\u6570\uff0c\u4ee5\u4fbf\u627e\u5230\u6700\u9002\u5408\u60a8\u6570\u636e\u96c6\u7684\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u6dfb\u52a0APRIORI\u7b97\u6cd5\u7684\u8fc7\u7a0b\u5305\u62ec\uff1a\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u3001\u52a0\u8f7d\u6570\u636e\u3001\u5e94\u7528APRIORI\u7b97\u6cd5\u3001\u89e3\u6790\u7ed3\u679c\u3002\u5728\u8fd9\u4e9b [&hellip;]","protected":false},"author":3,"featured_media":981459,"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\/981449"}],"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=981449"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981449\/revisions"}],"predecessor-version":[{"id":981464,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981449\/revisions\/981464"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/981459"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=981449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=981449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=981449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}