From c0470094d01391294617df6a92734b78b470b127 Mon Sep 17 00:00:00 2001 From: Jeel Gajera Date: Sat, 21 Oct 2023 14:48:21 +0530 Subject: [PATCH] changes names --- DIRECTORY.md | 2 +- ...p_growth.py => frequent_pattern_growth.py} | 32 +++++++++++-------- 2 files changed, 20 insertions(+), 14 deletions(-) rename machine_learning/{fp_growth.py => frequent_pattern_growth.py} (92%) diff --git a/DIRECTORY.md b/DIRECTORY.md index df6692fdd..916d993c5 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -541,7 +541,7 @@ * [Dimensionality Reduction](machine_learning/dimensionality_reduction.py) * Forecasting * [Run](machine_learning/forecasting/run.py) - * [FP Growth Algorithm](machine_learning/fp_growth.py) + * [Frequent Pattern Growth Algorithm](machine_learning/frequent_pattern_growth.py) * [Gradient Descent](machine_learning/gradient_descent.py) * [K Means Clust](machine_learning/k_means_clust.py) * [K Nearest Neighbours](machine_learning/k_nearest_neighbours.py) diff --git a/machine_learning/fp_growth.py b/machine_learning/frequent_pattern_growth.py similarity index 92% rename from machine_learning/fp_growth.py rename to machine_learning/frequent_pattern_growth.py index 356e95347..fa37313be 100644 --- a/machine_learning/fp_growth.py +++ b/machine_learning/frequent_pattern_growth.py @@ -10,8 +10,10 @@ Examples: https://www.javatpoint.com/fp-growth-algorithm-in-data-mining """ from typing import Optional +from dataclasses import dataclass, field +@dataclass class TreeNode: """ Initialize a TreeNode. @@ -30,14 +32,19 @@ class TreeNode: 2 """ - def __init__( - self, name_value: str, num_occur: int, parent_node: Optional["TreeNode"] = None - ) -> None: - self.name = name_value - self.count = num_occur - self.node_link = None # Initialize node_link to None - self.parent = parent_node - self.children: dict[str, TreeNode] = {} + # def __init__( + # self, name_value: str, num_occur: int, parent_node: Optional["TreeNode"] = None + # ) -> None: + # self.name = name_value + # self.count = num_occur + # self.node_link = TreeNode | None # Initialize node_link to None + # self.parent = parent_node + # self.children: dict[str, TreeNode] = {} + name: str + count: int + node_link: Optional['TreeNode'] = None # Initialize node_link to None + parent: Optional["TreeNode"] = None + children: dict[str, "TreeNode"] = field(default_factory=dict) def inc(self, num_occur: int) -> None: self.count += num_occur @@ -50,7 +57,7 @@ class TreeNode: def create_tree(data_set: list, min_sup: int = 1) -> tuple[TreeNode, dict]: """ - Create FP tree + Create Frequent Pattern tree Args: data_set (list): A list of transactions, where each transaction @@ -193,10 +200,7 @@ def update_header(node_to_test: TreeNode, target_node: TreeNode) -> TreeNode: while node_to_test.node_link is not None: node_to_test = node_to_test.node_link if node_to_test.node_link is None: - node_to_test.node_link = TreeNode( - target_node.name, target_node.count, node_to_test - ) - # Return the updated node + node_to_test.node_link = target_node return node_to_test @@ -298,6 +302,7 @@ def mine_tree( >>> all(expected in frequent_itemsets for expected in expe_itm) True """ + new_head: Optional['TreeNode'] = None sorted_items = sorted(header_table.items(), key=lambda item_info: item_info[1][0]) big_l = [item[0] for item in sorted_items] for base_pat in big_l: @@ -311,6 +316,7 @@ def mine_tree( header_table[base_pat][1] = update_header( header_table[base_pat][1], my_cond_tree ) + my_head = new_head mine_tree(my_cond_tree, my_head, min_sup, new_freq_set, freq_item_list)