{
  "source_file": "0930",
  "source_platform": "Hashnode",
  "author": "Muhammad Fahad Bashir",
  "title_listed": "Apriori Algorithm — Learning Association Rules the Simple Way",
  "keywords": "",
  "domain": "机器学习",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://ml-diaries-by-fahd.hashnode.dev/apriori-algorithm-learning-association-rules-the-simple-way",
  "publish_date": "2026-01-16",
  "corpus_stage": "",
  "notes": "",
  "id": "27b9c014059b",
  "collected_at": "2026-10-02T14:10:53",
  "title": "Discovering Patterns: Apriori Algorithm Explained",
  "body_markdown": "Apriori Algorithm — Learning Association Rules the Simple Way\nPublished\nJanuary 16, 2026\n•\n4\nmin read\n•\nView as Markdown\nM\nMuhammad Fahad Bashir\nMachine Learning, Artificial Intelligence\nPart of series\nUnsupervised Learning\nAs part of our\nunsupervised learning series\ntoday, we will talk about a famous algorithm of an association type. While exploring\nunsupervised learning\n, I came across the\nApriori Algorithm\n. At first, it sounded complex, but once I broke it down with real-life examples, it started to make sense.\nWhat Is the Apriori Algorithm? (In Simple Words)\nThe\nApriori Algorithm\nis one of the most fundamental techniques in\ndata mining and unsupervised machine learning\n. It is widely used to discover\npatterns, relationships, and associations\nbetween items in large datasets.In simple terms, Apriori helps answer questions like:\n“Which items are frequently bought together?”\nThis makes it extremely valuable in areas such as\nmarket basket analysis\n,\nrecommendation systems\n, and\ncustomer behavior analysis\n.\nThink of it as answering questions like:\nWhat items do people usually buy together?\nWhat actions often happen together?\nWhat combinations appear frequently in data?\nThis idea is known as\nassociation rule mining\n.\nExample:\nImagine a grocery store analyzing customer purchases.They notice:\nCustomers who buy\nbread\noften buy\nbutter,\n& Customers who buy\nrice + chicken masala\nalso buy\nghee.\nThis information can help the store t0 create combo offers, improve recommendations, and arrange shelves smartly This is exactly what\nApriori\nhelps us do, but at scale.\nKey Concepts You Must Know\n1. Transaction\nA single purchase or event containing multiple items.\nExample:\n{Milk, Bread, Butter}\n2. Itemset\nA group of one or more items.\nExamples:\n1-itemset:\n{Milk}\n2-itemset:\n{Milk, Bread}\n3-itemset:\n{Milk, Bread, Butter}\nKey Metrics in Apriori\n1. Support\nMeasures how frequently an itemset appears in the dataset.\n2. Confidence\nMeasures how likely item\nY\nis purchased when item\nX\nis purchased.\n3. Lift\nMeasures how much more likely two items are purchased together compared to random chance.\nLift > 1 → strong positive association\nLift = 1 → independent\nLift < 1 → negative association\nHow the Apriori Algorithm Works (Step by Step)\nStep 1: Set Thresholds\nBefore starting, define:\nMinimum Support and Minimum Confidence.\nThese values control how strict the algorithm is.\nStep 2: Identify Frequent 1-Itemsets\nScan the dataset and count how often each item appears.\nExample Dataset\nTransaction\nItems\nT1\n{Laptop, Mouse, Keyboard}\nT2\n{Laptop, Mouse}\nT3\n{Keyboard, Mouse}\nIf minimum support = 50%:\n{Laptop}\n→ 66.7%\n{Mouse}\n→ 100%\n{Keyboard}\n→ 66.7%\nAll are frequent.\nStep 3: Generate Candidate k-Itemsets\nCombine frequent itemsets to form:\n2-itemsets\n3-itemsets\nand so on\nExample 2-itemsets:\n{Laptop, Mouse}\n{Laptop, Keyboard}\n{Mouse, Keyboard}\nStep 4: Prune Using Apriori Property\nIf any subset of an itemset is\nnot frequent\n, discard the entire itemset.\nExample:\n{Laptop, Keyboard}\nhas low support → removed\nSaves computation time\nStep 5: Repeat for Higher-Order Itemsets\nThe process continues until no more frequent itemsets can be generated.\nStep 6: Generate Association Rules\nFrom frequent itemsets, generate rules of the form:\nExample:\n{Laptop} ⇒ {Mouse}\nStep 7: Filter Rules by Confidence\nOnly keep rules that meet the minimum confidence threshold.\nExample\n{Laptop} ⇒ {Mouse}\nConfidence = 100% → Strong rule\nPractical Example: Apriori Algorithm (Market Basket Analysis)\nWe’ll simulate a\nsmall grocery store dataset\nand discover\nfrequent itemsets and association rules\n.\n# installing libraires\nimport\npandas\nas\npd\nfrom\nmlxtend.preprocessing\nimport\nTransactionEncoder\nfrom\nmlxtend.frequent_patterns\nimport\napriori, association_rules\n# creating simple dataset\ntransactions = [\n    [\n'Milk'\n,\n'Bread'\n,\n'Butter'\n],\n    [\n'Milk'\n,\n'Bread'\n],\n    [\n'Milk'\n,\n'Butter'\n],\n    [\n'Bread'\n,\n'Butter'\n],\n    [\n'Milk'\n,\n'Bread'\n,\n'Butter'\n],\n    [\n'Bread'\n],\n]\n# encoding\nte = TransactionEncoder()\nte_array = te.fit(transactions).transform(transactions)\n\ndf = pd.DataFrame(te_array, columns=te.columns_)\ndf\n# finding frequest itemset\nfrequent_itemsets = apriori(\n    df,\n    min_support=\n0.4\n,\n    use_colnames=\nTrue\n)\n\nfrequent_itemsets\n# generate rules\nrules = association_rules(\n    frequent_itemsets,\n    metric=\n\"confidence\"\n,\n    min_threshold=\n0.6\n)\n\nrules[[\n'antecedents'\n,\n'consequents'\n,\n'support'\n,\n'confidence'\n,\n'lift'\n]]\nSupport tells “how common” . Confidence tells “how reliable”. Lift tells “how meaningful.”\nEach row represents an\nassociation rule\nwritten in the form:\nIF (antecedent) → THEN (consequent).\nFor example\n(Milk) → (Butter) (row 5)\nSupport = 0.5\n→ Bought together in half of all transactions\nConfidence = 0.75\n→ 75% of Milk buyers also buy Butter\nLift = 1.125\n→ Customers are\n12.5% more likely\nto buy Butter when they buy Milk\nThis is a strong and useful association.\nSome Real World Examples\nE-commerce:\nRecommends products that are frequently bought together to increase sales.\nFood Delivery Apps:\nIdentifies popular food combinations to create attractive combo deals.\nStreaming Platforms:\nSuggests movies or shows based on content users often watch together.\nFinance:\nAnalyzes spending patterns to offer personalized credit cards or discounts.\nTravel & Hospitality:\nBuilds travel bundles like flight + hotel based on common bookings.\nFinal Words\nApriori doesn’t predict numbers; it\nreveals hidden buying behavior\n. That’s why it’s widely used in\nrecommendation systems, combo offers, and business strategy\n. This algorithm is a cornerstone of\nassociation rule mining\n. While it may not be the fastest algorithm today, it provides a\nclear conceptual foundation\nfor understanding how machines discover hidden patterns in data.\n#\nunsupervised-learning\n#\nmachine-learning\n#\nalgorithms\n#\nai\n6\nviews",
  "metadata": {
    "title": "Discovering Patterns: Apriori Algorithm Explained",
    "description": "Apriori Algorithm finds patterns and associations in data for market basket analysis and recommendation systems in unsupervised learning",
    "keywords": null,
    "author": null,
    "og:title": "Discovering Patterns: Apriori Algorithm Explained",
    "og:description": "Apriori Algorithm finds patterns and associations in data for market basket analysis and recommendation systems in unsupervised learning",
    "og:url": "https://ml-diaries-by-fahd.hashnode.dev/apriori-algorithm-learning-association-rules-the-simple-way",
    "og:site_name": "ML Diaries by Fahd",
    "og:image": "https://cdn.hashnode.com/res/hashnode/image/upload/v1768535113397/3fe6a599-9482-4b8a-b04b-410c9d24c140.png",
    "og:image:width": "1200",
    "og:image:height": "630",
    "og:type": "article",
    "twitter:card": "summary_large_image",
    "twitter:title": "Discovering Patterns: Apriori Algorithm Explained",
    "twitter:description": "Apriori Algorithm finds patterns and associations in data for market basket analysis and recommendation systems in unsupervised learning",
    "twitter:image": "https://cdn.hashnode.com/res/hashnode/image/upload/v1768535113397/3fe6a599-9482-4b8a-b04b-410c9d24c140.png",
    "article:published_time": "2026-01-16T03:47:23.551Z",
    "article:author": "https://ml-diaries-by-fahd.hashnode.dev/@mfahadbashir"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/27b9c014059b/code/body-000.txt",
      "chars": 827,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1768535113397/3fe6a599-9482-4b8a-b04b-410c9d24c140.png",
      "bytes": 476791,
      "reused": true,
      "local_path": "attachments/27b9c014059b/images/002.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1768535113397/3fe6a599-9482-4b8a-b04b-410c9d24c140.png",
      "alt": "Apriori Algorithm — Learning Association Rules the Simple Way",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://miro.medium.com/1*Uju7aWZpiiqS3wDsz8kxJQ.jpeg",
      "bytes": 61661,
      "reused": true,
      "local_path": "attachments/27b9c014059b/images/004.jpeg",
      "src": "https://miro.medium.com/1*Uju7aWZpiiqS3wDsz8kxJQ.jpeg",
      "alt": "Underrated Machine Learning Algorithms — APRIORI | by Harsha Dannina | TDS  Archive | Medium",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1768534009209/ad1e4aa1-a99d-4f84-95f4-39947be9fcda.png",
      "bytes": 2760,
      "reused": true,
      "local_path": "attachments/27b9c014059b/images/005.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1768534009209/ad1e4aa1-a99d-4f84-95f4-39947be9fcda.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1768534017380/007fd9f3-b0ee-4cfb-91e6-56fdb6238721.png",
      "bytes": 2573,
      "reused": true,
      "local_path": "attachments/27b9c014059b/images/006.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1768534017380/007fd9f3-b0ee-4cfb-91e6-56fdb6238721.png",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://media.geeksforgeeks.org/wp-content/uploads/20250917144621191175/apriori_algorithm_2.webp",
      "bytes": 13006,
      "reused": true,
      "local_path": "attachments/27b9c014059b/images/007.webp",
      "src": "https://media.geeksforgeeks.org/wp-content/uploads/20250917144621191175/apriori_algorithm_2.webp",
      "alt": "Apriori Algorithm - GeeksforGeeks",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://cdn.hashnode.com/res/hashnode/image/upload/v1768534620609/32340422-a1e0-4fb4-bffd-9facdc444aa5.png",
      "bytes": 9279,
      "reused": true,
      "local_path": "attachments/27b9c014059b/images/008.png",
      "src": "https://cdn.hashnode.com/res/hashnode/image/upload/v1768534620609/32340422-a1e0-4fb4-bffd-9facdc444aa5.png",
      "alt": "",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/apriori-algorithm-learning-association-rules-the-simple-way.md",
      "label": "View as Markdown",
      "type": "a"
    },
    {
      "href": "https://hashnode.com/@mfahadbashir",
      "label": "M",
      "type": "a"
    },
    {
      "href": "https://www.linkedin.com/in/muhammad-fahad-bashir-09568216a/",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/series/unsupervisedlearning",
      "label": "Part of series Unsupervised Learning",
      "type": "a"
    },
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/tag/unsupervised-learning",
      "label": "# unsupervised-learning",
      "type": "a"
    },
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/tag/machine-learning",
      "label": "# machine-learning",
      "type": "a"
    },
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/tag/algorithms",
      "label": "# algorithms",
      "type": "a"
    },
    {
      "href": "https://ml-diaries-by-fahd.hashnode.dev/tag/ai",
      "label": "# ai",
      "type": "a"
    }
  ],
  "retry_at": "2026-10-03T08:07:01",
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:50:40Z",
  "source_url": "https://ml-diaries-by-fahd.hashnode.dev/apriori-algorithm-learning-association-rules-the-simple-way",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 5826,
  "expected_body_images": 6,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/27b9c014059b/pages/source.html",
  "local_html": "attachments/27b9c014059b/pages/article.html",
  "body_base_path": "attachments/27b9c014059b/pages/",
  "body_markdown_path": "attachments/27b9c014059b/pages/article.md"
}