mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-07 06:15:55 +00:00
changes names
This commit is contained in:
parent
71776e7802
commit
c0470094d0
@ -541,7 +541,7 @@
|
|||||||
* [Dimensionality Reduction](machine_learning/dimensionality_reduction.py)
|
* [Dimensionality Reduction](machine_learning/dimensionality_reduction.py)
|
||||||
* Forecasting
|
* Forecasting
|
||||||
* [Run](machine_learning/forecasting/run.py)
|
* [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)
|
* [Gradient Descent](machine_learning/gradient_descent.py)
|
||||||
* [K Means Clust](machine_learning/k_means_clust.py)
|
* [K Means Clust](machine_learning/k_means_clust.py)
|
||||||
* [K Nearest Neighbours](machine_learning/k_nearest_neighbours.py)
|
* [K Nearest Neighbours](machine_learning/k_nearest_neighbours.py)
|
||||||
|
@ -10,8 +10,10 @@ Examples: https://www.javatpoint.com/fp-growth-algorithm-in-data-mining
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
class TreeNode:
|
class TreeNode:
|
||||||
"""
|
"""
|
||||||
Initialize a TreeNode.
|
Initialize a TreeNode.
|
||||||
@ -30,14 +32,19 @@ class TreeNode:
|
|||||||
2
|
2
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
# def __init__(
|
||||||
self, name_value: str, num_occur: int, parent_node: Optional["TreeNode"] = None
|
# self, name_value: str, num_occur: int, parent_node: Optional["TreeNode"] = None
|
||||||
) -> None:
|
# ) -> None:
|
||||||
self.name = name_value
|
# self.name = name_value
|
||||||
self.count = num_occur
|
# self.count = num_occur
|
||||||
self.node_link = None # Initialize node_link to None
|
# self.node_link = TreeNode | None # Initialize node_link to None
|
||||||
self.parent = parent_node
|
# self.parent = parent_node
|
||||||
self.children: dict[str, TreeNode] = {}
|
# 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:
|
def inc(self, num_occur: int) -> None:
|
||||||
self.count += num_occur
|
self.count += num_occur
|
||||||
@ -50,7 +57,7 @@ class TreeNode:
|
|||||||
|
|
||||||
def create_tree(data_set: list, min_sup: int = 1) -> tuple[TreeNode, dict]:
|
def create_tree(data_set: list, min_sup: int = 1) -> tuple[TreeNode, dict]:
|
||||||
"""
|
"""
|
||||||
Create FP tree
|
Create Frequent Pattern tree
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
data_set (list): A list of transactions, where each transaction
|
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:
|
while node_to_test.node_link is not None:
|
||||||
node_to_test = node_to_test.node_link
|
node_to_test = node_to_test.node_link
|
||||||
if node_to_test.node_link is None:
|
if node_to_test.node_link is None:
|
||||||
node_to_test.node_link = TreeNode(
|
node_to_test.node_link = target_node
|
||||||
target_node.name, target_node.count, node_to_test
|
|
||||||
)
|
|
||||||
# Return the updated node
|
|
||||||
return node_to_test
|
return node_to_test
|
||||||
|
|
||||||
|
|
||||||
@ -298,6 +302,7 @@ def mine_tree(
|
|||||||
>>> all(expected in frequent_itemsets for expected in expe_itm)
|
>>> all(expected in frequent_itemsets for expected in expe_itm)
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
|
new_head: Optional['TreeNode'] = None
|
||||||
sorted_items = sorted(header_table.items(), key=lambda item_info: item_info[1][0])
|
sorted_items = sorted(header_table.items(), key=lambda item_info: item_info[1][0])
|
||||||
big_l = [item[0] for item in sorted_items]
|
big_l = [item[0] for item in sorted_items]
|
||||||
for base_pat in big_l:
|
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] = update_header(
|
||||||
header_table[base_pat][1], my_cond_tree
|
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)
|
mine_tree(my_cond_tree, my_head, min_sup, new_freq_set, freq_item_list)
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user