From 892c6478f864d537624941da525fe0bd3dcf954b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartolom=C3=A9=20Ortiz?= <18428931+thebooort@users.noreply.github.com> Date: Fri, 24 May 2024 12:50:27 +0200 Subject: [PATCH] Update init to fix the cinvert to df method With this change this library is able to export results to dataframe and handle algorthims like Apriori, which couldn't before. Test algorithm PrefixSpan still works well --- spmf/__init__.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/spmf/__init__.py b/spmf/__init__.py index 9eb468d..92ba9ae 100644 --- a/spmf/__init__.py +++ b/spmf/__init__.py @@ -140,18 +140,27 @@ def to_pandas_dataframe(self, pickle=False): if not self.patterns_: self.parse_output() - + patterns_dict_list = [] for pattern_sup in self.patterns_: - pattern = pattern_sup[:-1] - sup = pattern_sup[-1:][0] - sup = sup.strip() - if not sup.startswith("#SUP"): - print("support extraction failed") - sup = sup.split() - sup = sup[1] - - patterns_dict_list.append({'pattern': pattern, 'sup': int(sup)}) + # produce a string will all elements of list pattern_sup + full_pattern = ' '.join(pattern_sup) + + # Divide pattern an support + if "#SUP:" in full_pattern: + pattern_part, sup_part = full_pattern.split("#SUP:") + pattern_part = pattern_part.strip() + sup_part = sup_part.strip() + + support_value = sup_part.split()[0] + + patterns_dict_list.append({ + "pattern": pattern_part, + "support": support_value + }) + else: + print("Support extraction failed for pattern:", full_pattern) + df = pd.DataFrame(patterns_dict_list) self.df_ = df