1

Here are a few lines of code:

import pandas as pd
import numpy as np
from mlxtend.frequent_patterns import apriori 
from mlxtend.frequent_patterns import association_rules

def encode_c(x):
if x <= 0:
    return 0
if x >= 1:
    return 1
return 0


def get_product_consequents():
  all_transactions_df = get_dataframe()
  basket = (all_transactions_df.groupby(['a', 'b'])['c']
    .sum().unstack().reset_index().fillna(0)
    .set_index('a'))
  basket = basket.applymap(encode_c)
  frequent_itemsets = apriori(basket, min_support=0.07, use_colnames=True)
  rules = association_rules(frequent_itemsets, metric="lift", 
    min_threshold=1)
  rules['antecedants_length'] = rules['antecedents'].str.len()
  rules['consequents_length'] = rules['consequents'].str.len()
  rules = rules[(rules['lift'] >= 4) &  # 6
              (rules['confidence'] >= 0.4)]  # 0.8
  rules = rules[(rules['antecedants_length'] == 1) & 
    (rules['consequents_length'] == 1)]
  rules = (rules.groupby(['antecedants'])['consequents'])

IMAGE 1

deprecated error image

IMAGE 2

key error image

When I DO: learning apriori from

rules["antecedant_len"] = rules["antecedents"].apply(lambda x: len(x))

I GET:
IMAGE 1

C:\Program Files\JetBrains\PyCharm 2018.1.4\helpers\pydev\_pydevd_bundle
\pydevd_resolver.py:71:
FutureWarning: Series.strides is deprecated
and will be removed in a future version
  return getattr(var, attribute)

I was doing it from: mlxtend association_rules and apriori docs
As the error says: Series.strides are deprecated.
So how can I find the length of each frozenset in series? i.e. do same as above?

MAIN ERROR

IMAGE 2

rules = (rules.groupby(['antecedants'])['consequents'])

After I evaluate above line:

Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm         
2018.1.4\helpers\pydev\_pydevd_bundle\pydevd_vars.py", line 376, in 
evaluate_expression compiled = compile(expression, '<string>', 'eval')
File "<string>", line 1
rules = (rules.groupby(['antecedants'])['consequents'])
      ^
SyntaxError: invalid syntax  

-----WHY Syntax error, though works fine on JUPYTER NOTEBOOK?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 
2018.1.4\helpers\pydev\_pydevd_bundle\pydevd_comm.py", line 1159, in do_it
result = pydevd_vars.evaluate_expression(self.thread_id, self.frame_id, 
self.expression, self.doExec)
File "C:\Program Files\JetBrains\PyCharm 
2018.1.4\helpers\pydev\_pydevd_bundle\pydevd_vars.py", line 378, in 
evaluate_expression
Exec(expression, updated_globals, frame.f_locals)
File "C:\Program Files\JetBrains\PyCharm 
2018.1.4\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<string>", line 1, in <module>
File "C:\ProjcetPath\venv\lib\site- 
packages\pandas\core\generic.py", line 6659, in groupby
observed=observed, **kwargs)
File "C:\ProjcetPath\venv\lib\site- 
packages\pandas\core\groupby\groupby.py", line 2152, in groupby
return klass(obj, by, **kwds)
File "C:\ProjcetPath\venv\lib\site- 
packages\pandas\core\groupby\groupby.py", line 599, in __init__
mutated=self.mutated)
File "C:\ProjcetPath\venv\lib\site- 
packages\pandas\core\groupby\groupby.py", line 3291, in _get_grouper
raise KeyError(gpr)
KeyError: 'antecedants'

--- At last it says key error, during handling syntax error key error occurred, For sure it is not key error as I can see column in sciView, also am able to access it this was as done in above code lines.

OTHER INFO: I am using Django as well.

Aashish Gahlawat
  • 409
  • 1
  • 7
  • 25
  • How working change `rules["antecedant_len"] = rules["antecedents"].apply(lambda x: len(x))` to `rules["antecedant_len"] = rules["antecedents"].str.len()` ? – jezrael Aug 24 '18 at 11:41
  • This one is for Deprecated `Series.strids`, all my code works great in Jupyter Notebook. But in pycharm it throws error, there are different errors at debug and at console, couldn't get error – Aashish Gahlawat Aug 24 '18 at 11:46
  • 1
    For sanity purposes, please switch to using `antecedEnt` and versions of it instead of the (incorrect) `antecedAnt`. If you mix it up it's hard to tell whether something is a typo or an intentional choice. – DSM Aug 24 '18 at 11:48
  • @AashishGahlawat - Hard question, I never seen before (maybe because use Anaconda, not pycharm) – jezrael Aug 24 '18 at 11:54
  • @DSM coudn't get you sir!, I have just started with pandas. – Aashish Gahlawat Aug 24 '18 at 12:11
  • @jezrael Sir I am using django to build apis for my project, and that whole is in pycharm itself more convenient. – Aashish Gahlawat Aug 24 '18 at 12:14
  • This isn't about pandas. :-) In your `rules['antecedants_length'] = rules['antecedents'].apply(lambda x: len(x))` line, you change your mind about the spelling. – DSM Aug 24 '18 at 12:15
  • @jezrael Thanks for .str.len() it worked fine, and it was pycharm error only as it doesn't load dataframe itself to sciview unliess clicked on see as dataframe, other error was when dubugges, variable rules value changed on click of evalueate and then resulted into error – Aashish Gahlawat Aug 24 '18 at 15:47
  • @DSM and @jazrael updated question sir, as error was with `pycharm` itself – Aashish Gahlawat Aug 24 '18 at 16:17
  • Thanks @DSM it was a typo error only, english isn't my native so it wasn't an international choice. But came to know debug in pycharm brings variables to scope and running them again result different :) – Aashish Gahlawat Aug 25 '18 at 08:09
  • It should be `anteceedEnt` rather than `antecedAnt` i.e. E in place of A, thats why I was getting key error – Aashish Gahlawat Aug 25 '18 at 08:10

0 Answers0