{"id":1156396,"date":"2025-01-13T18:15:34","date_gmt":"2025-01-13T10:15:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1156396.html"},"modified":"2025-01-13T18:15:38","modified_gmt":"2025-01-13T10:15:38","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%85%b3%e8%81%94%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1156396.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u5173\u8054\u5206\u6790"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25194849\/7650dc80-ffe3-4170-a1d4-3411e8134a15.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u5173\u8054\u5206\u6790\" \/><\/p>\n<p><p> <strong>Python \u5b9e\u73b0\u5173\u8054\u5206\u6790<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5b9e\u73b0\u5173\u8054\u5206\u6790\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u4f8b\u5982<strong>Apriori\u7b97\u6cd5\u3001FP-Growth\u7b97\u6cd5\u3001MLxtend\u5e93\u3001Orange\u5e93<\/strong>\u7b49\u3002\u5176\u4e2d\uff0cApriori\u7b97\u6cd5\u662f\u6700\u5e38\u7528\u7684\u5173\u8054\u89c4\u5219\u6316\u6398\u7b97\u6cd5\u4e4b\u4e00\u3002<strong>\u901a\u8fc7Apriori\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\u9891\u7e41\u9879\u96c6\u548c\u5f3a\u5173\u8054\u89c4\u5219<\/strong>\u3002\u8fd9\u91cc\uff0c\u6211\u4eec\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Apriori\u7b97\u6cd5\u6765\u5b9e\u73b0\u5173\u8054\u5206\u6790\u3002<\/p>\n<\/p>\n<p><p><strong>Apriori\u7b97\u6cd5\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong><\/p>\n<p>Apriori\u7b97\u6cd5\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u6316\u6398\u9891\u7e41\u9879\u96c6\u548c\u5173\u8054\u89c4\u5219\u7684\u7b97\u6cd5\u3002\u5b83\u57fa\u4e8e\u4e00\u4e2a\u7b80\u5355\u7684\u539f\u5219\uff1a\u5982\u679c\u4e00\u4e2a\u9879\u96c6\u662f\u9891\u7e41\u7684\uff0c\u90a3\u4e48\u5b83\u7684\u6240\u6709\u5b50\u96c6\u4e5f\u662f\u9891\u7e41\u7684\u3002\u7b97\u6cd5\u7684\u4e3b\u8981\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u751f\u6210\u5019\u9009\u9879\u96c6\uff1a\u4ece\u9891\u7e41\u9879\u96c6\u751f\u6210\u5019\u9009\u9879\u96c6\u3002<\/li>\n<li>\u652f\u6301\u5ea6\u8ba1\u7b97\uff1a\u8ba1\u7b97\u6bcf\u4e2a\u5019\u9009\u9879\u96c6\u7684\u652f\u6301\u5ea6\uff0c\u7b5b\u9009\u51fa\u9891\u7e41\u9879\u96c6\u3002<\/li>\n<li>\u751f\u6210\u5173\u8054\u89c4\u5219\uff1a\u4ece\u9891\u7e41\u9879\u96c6\u4e2d\u751f\u6210\u5173\u8054\u89c4\u5219\uff0c\u5e76\u8ba1\u7b97\u7f6e\u4fe1\u5ea6\u548c\u63d0\u5347\u5ea6\u3002<\/li>\n<\/ol>\n<hr>\n<p><h3>\u4e00\u3001Apriori\u7b97\u6cd5<\/h3>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u5e76\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff0c\u5982pandas\u548cmlxtend\uff0c\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u548c\u6316\u6398\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">!pip install pandas mlxtend<\/p>\n<p>import pandas as pd<\/p>\n<p>from mlxtend.frequent_patterns import apriori, association_rules<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u52a0\u8f7d\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u96c6\uff0c\u4f8b\u5982\u8d85\u5e02\u7684\u8d2d\u7269\u8bb0\u5f55\u3002\u6570\u636e\u901a\u5e38\u4ee5\u4e00\u4e2a\u8868\u683c\u7684\u5f62\u5f0f\u5b58\u50a8\uff0c\u6bcf\u884c\u4ee3\u8868\u4e00\u6b21\u4ea4\u6613\uff0c\u6bcf\u5217\u4ee3\u8868\u4e00\u4e2a\u5546\u54c1\uff0c\u503c\u4e3a1\u8868\u793a\u8be5\u5546\u54c1\u5728\u8be5\u4ea4\u6613\u4e2d\u8d2d\u4e70\uff0c0\u8868\u793a\u672a\u8d2d\u4e70\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = pd.read_csv(&#39;supermarket_data.csv&#39;)<\/p>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6570\u636e\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u9002\u5e94Apriori\u7b97\u6cd5\u7684\u8981\u6c42\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3002\u4e3b\u8981\u5305\u62ec\u5c06\u6570\u636e\u8f6c\u5316\u4e3a\u9002\u5408\u7b97\u6cd5\u8f93\u5165\u7684\u683c\u5f0f\uff08\u5982one-hot\u7f16\u7801\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6570\u636e\u8f6c\u6362\u4e3aone-hot\u7f16\u7801<\/p>\n<p>basket = pd.get_dummies(data.set_index(&#39;Transaction&#39;)[&#39;Item&#39;]).groupby(level=0).sum()<\/p>\n<p>basket = basket.applymap(lambda x: 1 if x &gt; 0 else 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u751f\u6210\u9891\u7e41\u9879\u96c6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Apriori\u7b97\u6cd5\u751f\u6210\u9891\u7e41\u9879\u96c6\uff0c\u5e76\u6307\u5b9a\u6700\u5c0f\u652f\u6301\u5ea6\u9608\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">frequent_itemsets = apriori(basket, min_support=0.01, use_colnames=True)<\/p>\n<p>print(frequent_itemsets.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u751f\u6210\u5173\u8054\u89c4\u5219<\/h4>\n<\/p>\n<p><p>\u4ece\u9891\u7e41\u9879\u96c6\u4e2d\u751f\u6210\u5173\u8054\u89c4\u5219\uff0c\u5e76\u6307\u5b9a\u6700\u5c0f\u7f6e\u4fe1\u5ea6\u9608\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rules = association_rules(frequent_itemsets, metric=&quot;confidence&quot;, min_threshold=0.5)<\/p>\n<p>print(rules.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001FP-Growth\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>FP-Growth\u7b97\u6cd5\u662f\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u9891\u7e41\u9879\u96c6\u6316\u6398\u7b97\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u6570\u636e\u96c6\u3002\u5b83\u901a\u8fc7\u6784\u5efa\u9891\u7e41\u6a21\u5f0f\u6811\uff08FP-Tree\uff09\u6765\u9ad8\u6548\u5730\u53d1\u73b0\u9891\u7e41\u9879\u96c6\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u5e76\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff0c\u5982pandas\u548cmlxtend\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">!pip install pandas mlxtend<\/p>\n<p>import pandas as pd<\/p>\n<p>from mlxtend.frequent_patterns import fpgrowth, association_rules<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u52a0\u8f7d\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = pd.read_csv(&#39;supermarket_data.csv&#39;)<\/p>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6570\u636e\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5c06\u6570\u636e\u8f6c\u5316\u4e3aone-hot\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">basket = pd.get_dummies(data.set_index(&#39;Transaction&#39;)[&#39;Item&#39;]).groupby(level=0).sum()<\/p>\n<p>basket = basket.applymap(lambda x: 1 if x &gt; 0 else 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u751f\u6210\u9891\u7e41\u9879\u96c6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528FP-Growth\u7b97\u6cd5\u751f\u6210\u9891\u7e41\u9879\u96c6\uff0c\u5e76\u6307\u5b9a\u6700\u5c0f\u652f\u6301\u5ea6\u9608\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">frequent_itemsets = fpgrowth(basket, min_support=0.01, use_colnames=True)<\/p>\n<p>print(frequent_itemsets.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u751f\u6210\u5173\u8054\u89c4\u5219<\/h4>\n<\/p>\n<p><p>\u4ece\u9891\u7e41\u9879\u96c6\u4e2d\u751f\u6210\u5173\u8054\u89c4\u5219\uff0c\u5e76\u6307\u5b9a\u6700\u5c0f\u7f6e\u4fe1\u5ea6\u9608\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rules = association_rules(frequent_itemsets, metric=&quot;confidence&quot;, min_threshold=0.5)<\/p>\n<p>print(rules.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001MLxtend\u5e93<\/h3>\n<\/p>\n<p><p>MLxtend\uff08Machine Learning Extensions\uff09\u662f\u4e00\u4e2a\u6269\u5c55\u4e86scikit-learn\u529f\u80fd\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u6570\u636e\u6316\u6398\u5de5\u5177\u3002\u53ef\u4ee5\u4f7f\u7528MLxtend\u5e93\u4e2d\u7684\u5173\u8054\u89c4\u5219\u6316\u6398\u51fd\u6570\u6765\u8fdb\u884c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5e76\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">!pip install pandas mlxtend<\/p>\n<p>import pandas as pd<\/p>\n<p>from mlxtend.frequent_patterns import apriori, association_rules<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u52a0\u8f7d\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = pd.read_csv(&#39;supermarket_data.csv&#39;)<\/p>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6570\u636e\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5c06\u6570\u636e\u8f6c\u5316\u4e3aone-hot\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">basket = pd.get_dummies(data.set_index(&#39;Transaction&#39;)[&#39;Item&#39;]).groupby(level=0).sum()<\/p>\n<p>basket = basket.applymap(lambda x: 1 if x &gt; 0 else 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u751f\u6210\u9891\u7e41\u9879\u96c6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Apriori\u7b97\u6cd5\u751f\u6210\u9891\u7e41\u9879\u96c6\uff0c\u5e76\u6307\u5b9a\u6700\u5c0f\u652f\u6301\u5ea6\u9608\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">frequent_itemsets = apriori(basket, min_support=0.01, use_colnames=True)<\/p>\n<p>print(frequent_itemsets.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u751f\u6210\u5173\u8054\u89c4\u5219<\/h4>\n<\/p>\n<p><p>\u4ece\u9891\u7e41\u9879\u96c6\u4e2d\u751f\u6210\u5173\u8054\u89c4\u5219\uff0c\u5e76\u6307\u5b9a\u6700\u5c0f\u7f6e\u4fe1\u5ea6\u9608\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rules = association_rules(frequent_itemsets, metric=&quot;confidence&quot;, min_threshold=0.5)<\/p>\n<p>print(rules.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001Orange\u5e93<\/h3>\n<\/p>\n<p><p>Orange\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u6570\u636e\u53ef\u89c6\u5316\u548c\u5206\u6790\u5de5\u5177\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u6316\u6398\u548c\u673a\u5668\u5b66\u4e60\u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7\u5176Python API\u8fdb\u884c\u5173\u8054\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u5e76\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">!pip install orange3<\/p>\n<p>import Orange<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u52a0\u8f7d\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Orange\u52a0\u8f7d\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = Orange.data.Table(&#39;supermarket_data.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u751f\u6210\u9891\u7e41\u9879\u96c6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Orange\u7684\u5173\u8054\u89c4\u5219\u6316\u6398\u51fd\u6570\u751f\u6210\u9891\u7e41\u9879\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rules = Orange.associate.AssociationRulesSparseInducer(data, support=0.01)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u751f\u6210\u5173\u8054\u89c4\u5219<\/h4>\n<\/p>\n<p><p>\u4ece\u9891\u7e41\u9879\u96c6\u4e2d\u751f\u6210\u5173\u8054\u89c4\u5219\uff0c\u5e76\u6307\u5b9a\u6700\u5c0f\u7f6e\u4fe1\u5ea6\u9608\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for rule in rules:<\/p>\n<p>    print(rule)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5173\u8054\u89c4\u5219\u8bc4\u4f30<\/h3>\n<\/p>\n<p><p>\u5728\u5173\u8054\u5206\u6790\u4e2d\uff0c\u8bc4\u4f30\u5173\u8054\u89c4\u5219\u7684\u8d28\u91cf\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4e3b\u8981\u7684\u8bc4\u4f30\u6307\u6807\u5305\u62ec\u652f\u6301\u5ea6\u3001\u7f6e\u4fe1\u5ea6\u548c\u63d0\u5347\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>1. \u652f\u6301\u5ea6\uff08Support\uff09<\/h4>\n<\/p>\n<p><p>\u652f\u6301\u5ea6\u8868\u793a\u67d0\u4e2a\u9879\u96c6\u5728\u6240\u6709\u4ea4\u6613\u4e2d\u51fa\u73b0\u7684\u9891\u7387\u3002\u9ad8\u652f\u6301\u5ea6\u8868\u793a\u8be5\u9879\u96c6\u5728\u6570\u636e\u96c6\u4e2d\u51fa\u73b0\u9891\u7e41\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">support = rules[&#39;support&#39;]<\/p>\n<p>print(support.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7f6e\u4fe1\u5ea6\uff08Confidence\uff09<\/h4>\n<\/p>\n<p><p>\u7f6e\u4fe1\u5ea6\u8868\u793a\u5728\u5305\u542b\u9879\u96c6A\u7684\u4ea4\u6613\u4e2d\uff0c\u4e5f\u5305\u542b\u9879\u96c6B\u7684\u6bd4\u4f8b\u3002\u9ad8\u7f6e\u4fe1\u5ea6\u8868\u793a\u89c4\u5219\u7684\u53ef\u9760\u6027\u9ad8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">confidence = rules[&#39;confidence&#39;]<\/p>\n<p>print(confidence.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u63d0\u5347\u5ea6\uff08Lift\uff09<\/h4>\n<\/p>\n<p><p>\u63d0\u5347\u5ea6\u8868\u793a\u9879\u96c6A\u7684\u51fa\u73b0\u5bf9\u9879\u96c6B\u51fa\u73b0\u7684\u5f71\u54cd\u7a0b\u5ea6\u3002\u63d0\u5347\u5ea6\u5927\u4e8e1\u8868\u793a\u9879\u96c6A\u5bf9\u9879\u96c6B\u6709\u6b63\u5411\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lift = rules[&#39;lift&#39;]<\/p>\n<p>print(lift.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<p><h4>1. \u6570\u636e\u96c6\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u8d85\u5e02\u8d2d\u7269\u6570\u636e\u96c6\uff0c\u5305\u542b\u591a\u4e2a\u4ea4\u6613\u8bb0\u5f55\uff0c\u6bcf\u4e2a\u8bb0\u5f55\u5305\u542b\u8d2d\u4e70\u7684\u5546\u54c1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = pd.read_csv(&#39;supermarket_data.csv&#39;)<\/p>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5c06\u6570\u636e\u8f6c\u5316\u4e3aone-hot\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">basket = pd.get_dummies(data.set_index(&#39;Transaction&#39;)[&#39;Item&#39;]).groupby(level=0).sum()<\/p>\n<p>basket = basket.applymap(lambda x: 1 if x &gt; 0 else 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u751f\u6210\u9891\u7e41\u9879\u96c6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Apriori\u7b97\u6cd5\u751f\u6210\u9891\u7e41\u9879\u96c6\uff0c\u5e76\u6307\u5b9a\u6700\u5c0f\u652f\u6301\u5ea6\u9608\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">frequent_itemsets = apriori(basket, min_support=0.01, use_colnames=True)<\/p>\n<p>print(frequent_itemsets.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u751f\u6210\u5173\u8054\u89c4\u5219<\/h4>\n<\/p>\n<p><p>\u4ece\u9891\u7e41\u9879\u96c6\u4e2d\u751f\u6210\u5173\u8054\u89c4\u5219\uff0c\u5e76\u6307\u5b9a\u6700\u5c0f\u7f6e\u4fe1\u5ea6\u9608\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rules = association_rules(frequent_itemsets, metric=&quot;confidence&quot;, min_threshold=0.5)<\/p>\n<p>print(rules.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u89c4\u5219\u8bc4\u4f30<\/h4>\n<\/p>\n<p><p>\u8bc4\u4f30\u751f\u6210\u7684\u5173\u8054\u89c4\u5219\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">support = rules[&#39;support&#39;]<\/p>\n<p>confidence = rules[&#39;confidence&#39;]<\/p>\n<p>lift = rules[&#39;lift&#39;]<\/p>\n<p>print(support.head())<\/p>\n<p>print(confidence.head())<\/p>\n<p>print(lift.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u63cf\u8ff0\u4e86\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5173\u8054\u5206\u6790\u3002<strong>\u901a\u8fc7\u4f7f\u7528Apriori\u7b97\u6cd5\u3001FP-Growth\u7b97\u6cd5\u548cMLxtend\u5e93\u7b49\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u53d1\u73b0\u9891\u7e41\u9879\u96c6\u548c\u5f3a\u5173\u8054\u89c4\u5219<\/strong>\u3002\u5173\u8054\u89c4\u5219\u6316\u6398\u5728\u5e02\u573a\u7bee\u5206\u6790\u3001\u63a8\u8350\u7cfb\u7edf\u548c\u6b3a\u8bc8\u68c0\u6d4b\u7b49\u9886\u57df\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u524d\u666f\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5e94\u7528\u5173\u8054\u5206\u6790\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u5173\u8054\u5206\u6790\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8fdb\u884c\u5173\u8054\u5206\u6790\u7684\u57fa\u672c\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\u6570\u636e\u51c6\u5907\u3001\u6570\u636e\u6e05\u6d17\u3001\u4f7f\u7528\u9002\u5f53\u7684\u5e93\uff08\u5982pandas\u548cmlxtend\uff09\u8fdb\u884c\u6570\u636e\u5efa\u6a21\uff0c\u4ee5\u53ca\u5206\u6790\u7ed3\u679c\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u6570\u636e\u96c6\u662f\u5e72\u51c0\u7684\uff0c\u5e76\u4e14\u5305\u542b\u4e86\u9700\u8981\u5206\u6790\u7684\u53d8\u91cf\u3002\u63a5\u4e0b\u6765\uff0c\u4f7f\u7528pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\uff0cmlxtend\u5e93\u4e2d\u7684apriori\u7b97\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u4f60\u627e\u5230\u9891\u7e41\u9879\u96c6\uff0c\u6700\u540e\u4f7f\u7528\u8fd9\u4e9b\u9891\u7e41\u9879\u96c6\u751f\u6210\u5173\u8054\u89c4\u5219\uff0c\u5e76\u8bc4\u4f30\u5176\u652f\u6301\u5ea6\u548c\u7f6e\u4fe1\u5ea6\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u6267\u884c\u5173\u8054\u5206\u6790\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u5173\u8054\u5206\u6790\u3002\u5176\u4e2d\uff0cpandas\u662f\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u7684\u57fa\u7840\u5e93\uff0cmlxtend\u63d0\u4f9b\u4e86\u7528\u4e8e\u6316\u6398\u9891\u7e41\u9879\u96c6\u548c\u5173\u8054\u89c4\u5219\u7684\u529f\u80fd\uff0c\u800cseaborn\u548cmatplotlib\u53ef\u4ee5\u5e2e\u52a9\u53ef\u89c6\u5316\u5206\u6790\u7ed3\u679c\u3002\u6b64\u5916\uff0cscikit-learn\u4e5f\u5e38\u7528\u4e8e\u673a\u5668\u5b66\u4e60\u548c\u6570\u636e\u6316\u6398\uff0c\u867d\u7136\u5176\u4e3b\u8981\u7528\u4e8e\u5206\u7c7b\u548c\u56de\u5f52\uff0c\u4f46\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u5de5\u5177\u3002<\/p>\n<p><strong>\u5173\u8054\u5206\u6790\u4e2d\u5982\u4f55\u8bc4\u4f30\u89c4\u5219\u7684\u6709\u6548\u6027\uff1f<\/strong><br \/>\u5728\u5173\u8054\u5206\u6790\u4e2d\uff0c\u8bc4\u4f30\u89c4\u5219\u7684\u6709\u6548\u6027\u901a\u5e38\u4f9d\u8d56\u4e8e\u652f\u6301\u5ea6\u3001\u7f6e\u4fe1\u5ea6\u548c\u63d0\u5347\u5ea6\u3002\u652f\u6301\u5ea6\u8868\u793a\u89c4\u5219\u5728\u6570\u636e\u96c6\u4e2d\u51fa\u73b0\u7684\u9891\u7387\uff0c\u7f6e\u4fe1\u5ea6\u5219\u662f\u89c4\u5219\u7684\u53ef\u9760\u6027\uff0c\u8868\u793a\u5728\u6ee1\u8db3\u524d\u63d0\u6761\u4ef6\u7684\u60c5\u51b5\u4e0b\uff0c\u540e\u679c\u53d1\u751f\u7684\u53ef\u80fd\u6027\u3002\u63d0\u5347\u5ea6\u5219\u8868\u793a\u89c4\u5219\u7684\u5f3a\u5ea6\uff0c\u53cd\u6620\u4e86\u524d\u63d0\u4e0e\u540e\u679c\u4e4b\u95f4\u7684\u5173\u8054\u7a0b\u5ea6\u3002\u901a\u8fc7\u8fd9\u4e9b\u6307\u6807\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7b5b\u9009\u51fa\u5177\u6709\u5b9e\u9645\u5e94\u7528\u4ef7\u503c\u7684\u5173\u8054\u89c4\u5219\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5b9e\u73b0\u5173\u8054\u5206\u6790 \u5728Python\u4e2d\uff0c\u5b9e\u73b0\u5173\u8054\u5206\u6790\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u4f8b\u5982Apriori\u7b97\u6cd5\u3001FP [&hellip;]","protected":false},"author":3,"featured_media":1156397,"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\/1156396"}],"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=1156396"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156396\/revisions"}],"predecessor-version":[{"id":1156398,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156396\/revisions\/1156398"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1156397"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1156396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1156396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1156396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}