From d7e3f19474fba83e6d0ef5b0ea0c4aeb742ac04d Mon Sep 17 00:00:00 2001 From: Avvari1830S <76546836+Avvari1830S@users.noreply.github.com> Date: Wed, 6 Sep 2023 19:00:37 +0530 Subject: [PATCH] Update CFPGrowth.py --- .../basic/CFPGrowth.py | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/PAMI/multipleMinimumSupportBasedFrequentPattern/basic/CFPGrowth.py b/PAMI/multipleMinimumSupportBasedFrequentPattern/basic/CFPGrowth.py index 267e300e..e3273f20 100644 --- a/PAMI/multipleMinimumSupportBasedFrequentPattern/basic/CFPGrowth.py +++ b/PAMI/multipleMinimumSupportBasedFrequentPattern/basic/CFPGrowth.py @@ -52,6 +52,8 @@ from PAMI.multipleMinimumSupportBasedFrequentPattern.basic import abstract as _fp +from typing import List, Dict, Tuple, Set, Union, Any, Generator +import pandas as pd _fp._sys.setrecursionlimit(20000) _MIS = {} @@ -79,13 +81,13 @@ class _Node: """ - def __init__(self, item, children): + def __init__(self, item: int, children: list) -> None: self.itemId = item self.counter = 1 self.parent = None self.children = children - def addChild(self, node): + def addChild(self, node) -> None: """ Retrieving the child from the tree @@ -123,12 +125,12 @@ class _Tree: generating the patterns from fp-tree """ - def __init__(self): + def __init__(self) -> None: self.root = _Node(None, {}) self.summaries = {} self.info = {} - def addTransaction(self, transaction, count): + def addTransaction(self, transaction, count) -> None: """adding transaction into tree :param transaction: it represents the one transaction in database @@ -156,7 +158,7 @@ def addTransaction(self, transaction, count): currentNode = currentNode.children[transaction[i]] currentNode.freq += count - def getFinalConditionalPatterns(self, alpha): + def getFinalConditionalPatterns(self, alpha) -> Tuple[List[List[int]], List[int], Dict[int, int]]: """ generates the conditional patterns for a node @@ -185,7 +187,7 @@ def getFinalConditionalPatterns(self, alpha): return finalPatterns, finalFreq, info @staticmethod - def getConditionalTransactions(ConditionalPatterns, conditionalFreq): + def getConditionalTransactions(ConditionalPatterns, conditionalFreq) -> Tuple[List[List[int]], List[int], Dict[int, int]]: """ To calculate the frequency of items in conditional patterns and sorting the patterns Parameters @@ -219,7 +221,7 @@ def getConditionalTransactions(ConditionalPatterns, conditionalFreq): count += 1 return pat, freq, up_dict - def generatePatterns(self, prefix): + def generatePatterns(self, prefix) -> Generator[Tuple[List[int], int], None, None]: """ To generate the frequent patterns Parameters @@ -381,10 +383,10 @@ class CFPGrowth(_fp._frequentPatterns): __rank = {} __rankDup = {} - def __init__(self, iFile, MIS, sep='\t'): + def __init__(self, iFile, MIS, sep='\t') -> None: super().__init__(iFile, MIS, sep) - def __creatingItemSets(self): + def __creatingItemSets(self) -> None: """ Storing the complete transactions of the database/input file in a database variable @@ -421,7 +423,7 @@ def __creatingItemSets(self): print("File Not Found") quit() - def _getMISValues(self): + def _getMISValues(self) -> None: """ Storing the Minimum supports given by the user for each item in the database @@ -462,7 +464,7 @@ def _getMISValues(self): print("File Not Found") quit() - def __convert(self, value): + def __convert(self, value) -> float: """ to convert the type of user specified minSup value @@ -482,7 +484,7 @@ def __convert(self, value): value = int(value) return value - def __frequentOneItem(self): + def __frequentOneItem(self) -> List[str]: """ Generating One frequent items sets @@ -503,7 +505,7 @@ def __frequentOneItem(self): self.__rank = dict([(index, item) for (item, index) in enumerate(genList)]) return genList - def __updateTransactions(self, itemSet): + def __updateTransactions(self, itemSet) -> List[List[int]]: """ Updates the items in transactions with rank of items according to their support @@ -529,7 +531,7 @@ def __updateTransactions(self, itemSet): return list1 @staticmethod - def __buildTree(transactions, info): + def __buildTree(transactions, info) -> _Tree: """ Builds the tree with updated transactions Parameters: @@ -548,7 +550,7 @@ def __buildTree(transactions, info): rootNode.addTransaction(transactions[i], 1) return rootNode - def __savePeriodic(self, itemSet): + def __savePeriodic(self, itemSet) -> str: """ The duplication items and their ranks Parameters: @@ -565,7 +567,7 @@ def __savePeriodic(self, itemSet): temp = temp + self.__rankDup[i] + "\t" return temp - def startMine(self): + def startMine(self) -> None: """ main program to start the operation @@ -597,7 +599,7 @@ def startMine(self): self.__memoryUSS = process.memory_full_info().uss self.__memoryRSS = process.memory_info().rss - def getMemoryUSS(self): + def getMemoryUSS(self) -> float: """Total amount of USS memory consumed by the mining process will be retrieved from this function :return: returning USS memory consumed by the mining process @@ -607,7 +609,7 @@ def getMemoryUSS(self): return self.__memoryUSS - def getMemoryRSS(self): + def getMemoryRSS(self) -> float: """Total amount of RSS memory consumed by the mining process will be retrieved from this function :return: returning RSS memory consumed by the mining process @@ -617,7 +619,7 @@ def getMemoryRSS(self): return self.__memoryRSS - def getRuntime(self): + def getRuntime(self) -> float: """Calculating the total amount of runtime taken by the mining process @@ -628,7 +630,7 @@ def getRuntime(self): return self.__endTime - self.__startTime - def getPatternsAsDataFrame(self): + def getPatternsAsDataFrame(self) -> pd.DataFrame: """Storing final frequent patterns in a dataframe :return: returning frequent patterns in a dataframe @@ -643,7 +645,7 @@ def getPatternsAsDataFrame(self): dataframe = _fp._pd.DataFrame(data, columns=['Patterns', 'Support']) return dataframe - def save(self, outFile): + def save(self, outFile: str) -> None: """Complete set of frequent patterns will be loaded in to an output file :param outFile: name of the output file @@ -656,7 +658,7 @@ def save(self, outFile): s1 = x.strip() + ":" + str(y) writer.write("%s \n" % s1) - def getPatterns(self): + def getPatterns(self) -> Dict[str, int]: """ Function to send the set of frequent patterns after completion of the mining process :return: returning frequent patterns @@ -665,7 +667,7 @@ def getPatterns(self): """ return self.__finalPatterns - def printResults(self): + def printResults(self) -> None: """ this function is used to print the results """ print("Total number of Frequent Patterns:", len(self.getPatterns()))